RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.78k stars 1.29k forks source link

TransformToExternalPath is necessary when using SwaggerRoutes with OWIN #2115

Open ideafixxxer opened 5 years ago

ideafixxxer commented 5 years ago

Related: #1914

When using multiple Swagger Documents with UI3 on ASP.NET OWIN, the default implementation of TransformToExternalPath results in all references to swagger.json to be unresolved.

For example:

settings.MiddlewareBasePath = "/swagger";
settings.SwaggerRoutes.Add(new SwaggerUi3Route("v1", "/swagger/v1/swagger.json"));

My app is hosted at https://localhost/services/api/, however the web browser is searching the file at https://localhost/v1/swagger.json. Adding the following line fixes the issue:

settings.TransformToExternalPath = (url, request) => request.PathBase + url;

My strong opinion is that it should the default implementation.

RicoSuter commented 5 years ago

My strong opinion is that it should the default implementation.

I think you are right with this... I'm a bit hesitant as this might be a breaking change...

ideafixxxer commented 5 years ago

I think you are right with this... I'm a bit hesitant as this might be a breaking change...

I agree, it is used in many places, and in fact it does break RedirectToIndexMiddleware! I have to figure out how to fix it now...

RicoSuter commented 5 years ago

Indeed, if you find a way which just works i’m more then happy to merge it 🙂 but i doubt that this can be automatically fixed...

ideafixxxer commented 5 years ago

So far I worked around it the consumer code

settings.TransformToExternalPath = (url, request) => url.EndsWith(".json", System.StringComparison.OrdinalIgnoreCase) 
  ? request.PathBase + url 
  : url;

Maybe TransformToExternalPath should be defined on the SwaggerUi3Route level?

angusbreno commented 4 months ago

image

I've to use the fix, because when using external specs the default imp adds '/' on the url start.