Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swashbuckle.AspNetCore: 'No operations defined in spec!' problem after update of 'Microsoft.AspNetCore.Mvc.ApiExplorer' package to 2.2.0

we have .net core 2.1 mvc webapi project which uses Swagger. we use following packages:

<PackageReference Include="Swashbuckle.AspNetCore" Version="3.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="3.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Filters" Version="4.3.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.ApiExplorer" Version="2.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.1.1" />

swashbuckle is configured in following way:

services.AddMvcCore().AddVersionedApiExplorer();
services.AddApiVersioning();
services.AddSwaggerGen();

everything woks in such setup (/swagger/v1/swagger.json has all operations and definitions and UI is rendered properly -> having all controllers and actions, etc)

we are trying to migrate our .net core project from 2.1 to 2.2 .net core.

In order to do that (without warnings) we need upgrade Microsoft.AspNetCore.Mvc.ApiExplorer nuget from 2.1.2 to 2.2.0.

After this nuget update swagger.json (/swagger/v1/swagger.json) doesn't contain any "paths": {} and "definitions": {} and this results in swagger UI showing no controllers/actions (it renders: No operations defined in spec!

after upgrade package these package versions is updated:

<PackageReference Include="Microsoft.AspNetCore.Mvc.ApiExplorer" Version="2.2.0" /> //was 2.1.2
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.2.0" /> //was 2.1.1

I've tried to upgrade swashbuckle version to v4.0.0 but it did not resolved the issue.

what im missing which packages needs to be updated also so swagger.json would be generated properly when using Microsoft.AspNetCore.Mvc.ApiExplorer 2.2.0 package ? or i'm missing something else ?

like image 291
Erikas Neverdauskas Avatar asked May 30 '19 17:05

Erikas Neverdauskas


2 Answers

Just had the same issue, thought I'd share my solution too. Version 6.0.4.

I had to add the [ApiController] attribute to my controllers. Swagger must be using that for discovery.

like image 141
gavsmith Avatar answered Oct 16 '22 13:10

gavsmith


  • change public methods in controllers to [NoAction] Attribute.

  • Also, Change all actions with explicit action Methods to [HttpGet("api/get-customer")], [HttpPost("api/save-customer")] instead of [Route("api/get-customer")].

like image 20
Isanka Thalagala Avatar answered Oct 16 '22 13:10

Isanka Thalagala