2

I have an endpoint that takes a custom type (in this particular case a NodaTime.LocalDate). The type has a custom model binder and is mapped in Swagger using MapType<>.

When the type is used in the route as a path parameter, the endpoint doesn't show up in Swagger/Swashbuckle. However, if I remove it (so that it's included as a URL parameter instead), it shows just fine.

A simple version of the endpoint:

public class MyController : System.Web.Http.ApiController
{
    [Route( "my/route/{date}" )] // Doesn't show up
    [Route( "my/route" )]        // Does show up
    [HttpPut]
    public Task<IHttpActionResult> MyEndpoint( LocalDate date, InputModel inputModel ) {
        // Do stuff...
    }
}

Note that both endpoints actually work, one just doesn't show up. I can also get the both endpoints to show up, if I change the type to DateTime- but I don't want that, since that would change the valid input set.

Helder Sepulveda
  • 15,500
  • 4
  • 29
  • 56
Mikkel R. Lund
  • 2,336
  • 1
  • 31
  • 44
  • That does not look like a valid path, LocalDate is an object it can not be represented in the URL route... – Helder Sepulveda Oct 17 '17 at 17:37
  • So the type has a model binder that allows the struct (it is not an object) to be parsed from a string - in our case in the ISO8601 format. The endpoint works just fine, i.e. you can call it with my/route/2017-10-17 without any problems. It just doesn't show up in Swagger/Swashbuckle. – Mikkel R. Lund Oct 17 '17 at 17:42
  • 1
    To troubleshoot you can add a controller/action showing all the endpoint from the GetApiExplorer: https://github.com/heldersepu/SwashbuckleTest/blob/master/Swagger_Test/Controllers/ApiExplorerController.cs – Helder Sepulveda Oct 17 '17 at 17:49
  • That looks great! I'll try that when I get to work tomorrow. – Mikkel R. Lund Oct 17 '17 at 17:52
  • @HelderSepu, how it can help? Mikkel, do you resolved this problem? – razon Oct 22 '17 at 17:22
  • 1
    @razon swashbuckle uses GetApiExplorer, if when you use GetApiExplorer directly it does not show the endpoint, there is nothing that swashbuckle can do, you might want to report your issue directly to Microsoft. – Helder Sepulveda Oct 22 '17 at 17:25
  • @MikkelR.Lund I did a few more tests with nodatime and there are a lot of issue with swashbuckle, even when is not on the route, it does not get the correct type, I had to add a MapType: ```c.MapType(() => new Schema { type="string", format="date-time" });``` – Helder Sepulveda Oct 26 '17 at 19:19
  • @HelderSepu So I have already mapped all the NodaTime types correctly - both using modelbinders and by mapping the various types. This works well as long as the NodaTime parameters are not part of the route. They show up and can be called. When the NodaTime types are part of the route, the endpoint works, it just doesn't show up in Swashbuckle. – Mikkel R. Lund Oct 26 '17 at 19:33
  • @MikkelR.Lund also the DateTimes has issues when they are part of the route, look at these two both should resolve correctly but only one is: http://swashbuckletest.azurewebsites.net/NodaTime/my/route5/01-01-2001 _ http://swashbuckletest.azurewebsites.net/NodaTime/my/route5/01%2F01%2F2001 – Helder Sepulveda Oct 26 '17 at 20:01
  • @HelderSepu I'm not quite sure, what you are trying to show me with the two endpoints. Both formats are "bad", since it is much preferred to use the ISO-8601 format, e.g. 2017-10-26. As said in the original question, I have no problems getting either of the two endpoints ("my/route/2017-10-26" and "my/route?date=2017-10-26") to work, the former just doesn't show up in Swashbuckle, when `date` is a `LocalDate`. It will, however, work if I change the type to `DateTime`, but that is not an acceptable solution. – Mikkel R. Lund Oct 26 '17 at 20:12
  • What I was trying to convey is stay away from dates in the URL (as path or params) if you can get them on the body all your problems (with Swashbuckle) will go away – Helder Sepulveda Oct 26 '17 at 20:18
  • Nah, that's not an option :) We already use them with great success in the parameters. I think I'll file a bug report with Swashbuckle instead. Appreciate your help a lot, thanks. – Mikkel R. Lund Oct 26 '17 at 20:27
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/157619/discussion-between-heldersepu-and-mikkel-r-lund). – Helder Sepulveda Oct 26 '17 at 20:37
  • @HelderSepu there are no issue in ApiExplorer with it. I resolved problem :) See my answer – razon Nov 05 '17 at 16:46

1 Answers1

0

I solved this problem by adding custom binding and custom converter from string. These actions are needed so that ApiExplorer resolve the route. After that Swagger resolves the route too.

See my example here: https://stackoverflow.com/a/47123965/908936

razon
  • 3,882
  • 2
  • 33
  • 46