RicoSuter / NSwag

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

Adding SwaggerRoutes "hijacks" site root ASP.NET (non core) WebApi #1797

Open svenerp opened 5 years ago

svenerp commented 5 years ago

By "hijacks" I mean that the site root returns swagger.json - file, instead of hitting a default controller.

Steps to reproduce Using ASP.NET, non core, webapi with OWIN. app.UseSwagger(typeof(Startup).GetTypeInfo().Assembly, config => { config.DocumentPath = documentpath; });

app.UseSwaggerUi3(typeof(Startup).GetTypeInfo().Assembly, config => { config.SwaggerRoutes.Add(new SwaggerUi3Route(versionWithV, documentpath)); });

However, this works, does not "hijack" site root: app.UseSwaggerUi3(typeof(Startup).GetTypeInfo().Assembly, config => { config.DocumentPath = documentpath; });

Using version 12.0.4

RicoSuter commented 5 years ago

Shouldnt it be:

app.UseSwagger(typeof(Startup).GetTypeInfo().Assembly, config =>
{
     config.Path = documentpath;
});
app.UseSwaggerUi3(config =>
{
     config.Path = "/swagger";
     config.SwaggerRoutes.Add(new SwaggerUi3Route(versionWithV, documentpath));
});
svenerp commented 5 years ago

I can not find a property "Path" here: app.UseSwagger(typeof(Startup).GetTypeInfo().Assembly, config => { config.Path = documentpath; });

If I use DocumentPath there, and change the other part to app.UseSwaggerUi3(config => { config.Path = "/swagger"; config.SwaggerRoutes.Add(new SwaggerUi3Route(versionWithV, documentpath)); });

I still get swagger.json when I go to the root.

nallejacobsson commented 4 years ago

I have the exact same problem. I need to use SwaggerRoutes in UseSwaggerUi3() to add multiple documents, but if I do that the site root gets "hijacked" and returns swagger.json instead of hitting the default controller.

@svenerp Did you ever find a solution? @RicoSuter Can you please have look?

Thanks!

nallejacobsson commented 4 years ago

I have the exact same problem. I need to use SwaggerRoutes in UseSwaggerUi3() to add multiple documents, but if I do that the site root gets "hijacked" and returns swagger.json instead of hitting the default controller.

@svenerp Did you ever find a solution? @RicoSuter Can you please have look?

Thanks!

I solved it, the key was to use the correct overload of UseSwaggerUi3() so it ONLY adds the Swagger UI to the OWIN pipeline.

Correct:

app.UseSwaggerUi3(config => { config.Path = "/swagger"; config.SwaggerRoutes.Add(new SwaggerUi3Route(versionWithV, documentpath)); });

Not correct:

app.UseSwaggerUi3(typeof(Startup).Assembly, config => { config.Path = "/swagger"; config.SwaggerRoutes.Add(new SwaggerUi3Route(versionWithV, documentpath)); });

RicoSuter commented 4 years ago

UseSwaggerUi3 with assembly is marked deprecated, right?

nallejacobsson commented 4 years ago

UseSwaggerUi3 with assembly is marked deprecated, right?

No, it's not. But it should be :)