Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'No operations defined in spec!' problem after update of 'Microsoft.AspNetCore.Mvc.ApiExplorer' nuget #1166

Closed
erinev opened this issue May 30, 2019 · 13 comments

Comments

@erinev
Copy link

erinev commented May 30, 2019

Hi,

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.

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

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 ?

@mikebeaton
Copy link
Contributor

Is it worth trying a newer version of Swashbuckle.AspNetCore? https://www.nuget.org/packages/Swashbuckle.AspNetCore

@erinev
Copy link
Author

erinev commented May 31, 2019

@mikebeaton hi,
i forgot to mention that i tried to update Swashbuckle.AspNetCore to v4.0.0 but it was still not working at the same while having ApiExplorer with v2.2.0

i will try to create a mockup repo in public github with reproduced scenario

@erinev
Copy link
Author

erinev commented May 31, 2019

i created demo repository with 100% reproducible problem.

Master branch: https://github.com/erinev/net-core-api-for-digging-swashbuckle-issue/tree/master
uses following packages:

<PackageReference Include="Microsoft.AspNetCore.Mvc.ApiExplorer" Version="2.1.3" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.2.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="4.0.1" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="4.0.1" />
<PackageReference Include="Swashbuckle.AspNetCore.Filters" Version="4.5.5" />

**note: "Microsoft.AspNetCore.Mvc.ApiExplorer" Version="2.1.3" & "Swashbuckle.AspNetCore" Version="4.0.1"

everything works after running solution and opening http://localhost:5000/swagger -> swagger UI displays ad relations controller and requests can be executed

i updated just "Microsoft.AspNetCore.Mvc.ApiExplorer" to 2.2.0 in branch: ApiExplorer_2.2.0
PR: https://github.com/erinev/net-core-api-for-digging-swashbuckle-issue/pull/1/files

everything compiles but when i open http://localhost:5000/swagger i see empty list of operations:
image
http://localhost:5000/swagger/v1/swagger.json outputs just:

{
  "swagger": "2.0",
  "info": {
    "version": "v1",
    "title": "Ad Relations Reader",
    "description": "Ad Relations Reader."
  },
  "paths": {},
  "definitions": {}
}

cc: @mikebeaton

@mikebeaton
Copy link
Contributor

mikebeaton commented Jun 1, 2019

I do get the symptoms you describe, and I am not quite sure what is going on but I don't think the problem is with Swashbuckle.AspNetCore.

If I run your example with Microsoft.AspNetCore.Mvc.ApiExplorer version 2.1.3 then at line 48 of src/Swashbuckle.AspNetCore.SwaggerGen/Generator/SwaggerGenerator.cs _apiDescriptionsProvider.ApiDescriptionGroups.Items contains 1 item for your GetByAdUuid. But if I run the example after upgrading to Microsoft.AspNetCore.Mvc.ApiExplorer version 2.2.0 then _apiDescriptionsProvider.ApiDescriptionGroups.Items contains no items. Which is where the problem lies.

I saw this by cloning Swashbuckle.AspNetCore locally, checking out v4.0.1 and then compiling your project directly with the required Swashbuckle.AspNetCore projects instead of the NuGet packages. Doing that, everything runs exactly the same (including that Microsoft.AspNetCore.Mvc.ApiExplorer 2.1.3 works but 2.2.0 doesn't), but now you can step in to and put breakpoints on the Swashbuckle.AspNetCore code and see what is happening.

Since the value of _apiDescriptionsProvider at that point is an instance of the Microsoft class Microsoft.AspNetCore.Mvc.ApiExplorer.ApiDescriptionGroupCollectionProvider, which has just been injected by Microsoft code (and which I don't think you have even configured?), then I think the problem lies outside Swashbuckle.AspNetCore.

@erinev
Copy link
Author

erinev commented Jun 1, 2019

@mikebeaton thanks for deep investigation.

i liked the way you checked the issue.

Maybe you still have compiling code with Swashbuckle.AspNetCore added as project instead of nuget ? if yes maybe you can fork my demo repository and push changes i would create issue to Microsoft.AspNetCore.Mvc.ApiExplorer repository (by providing them debuggable Swashbuckle example) maybe they will help to understand why this is happening
if no i will try to add Swashbuckle locally my self (i want to save time if you still have the code)

@mikebeaton
Copy link
Contributor

mikebeaton commented Jun 2, 2019

Hi @erinev, I'm glad it helped.

You should reproduce my steps yourself, at this point.

  • git clone the Swashbuckle.AspNetCore project, so that you have your own local copy
    • It's optional to make a fork first, though if you might end up trying out making changes to Swashbuckle.AspNetCore then it might be easier to make your own fork first, and clone from that
  • cd into the Swashbuckle.AspNetCore project directory and git checkout v4.0.1
  • Remove the two Swashbuckle.AspNetCore packages from your project
    • At this point, do not remove the Swashbuckle.AspNetCore.Filters project, since it's in a separate repo
  • Right-click your own solution and Add existing project... twice, for the two .csproj files in the Swashbuckle.AspNetCore repo corresponding to the two NuGet packages which you just removed
    • (This works, projects in a VS solution do not have to be in subdirectories of the solution)
  • Add two project dependencies from your own project to the two Swashbuckle projects you have just included (so that your project knows where to find its code dependencies now that they are not coming from NuGet any more)
  • Try to clean (ALT+B ALT+C) and rebuild (CTRL+SHIFT+B) your solution; but it won't build yet, because the two Swashbuckle projects you have just added depend on three other Swashbuckle projects in the Swashbuckle repo
  • Right-click your solution and Add existing project... for those three as well (no need to add any more project dependencies at this point)
  • Clean and rebuild, it will work! ;)

Okay, what I would do next:

  • Add a new, simple, normal JSON controller to your project, one which accepts an injected instance of IApiDescriptionGroupCollectionProvider (which you will see in the code is what the Swashbuckle Swagger generator is accepting), and which maybe just returns an integer for how many items are in its .ApiDescriptionGroups.Items
    • You shouldn't need to change your config for this to work, with your current setup .NET Core should already be able to inject an item of that type for you
  • Play around with looking at what is in this - you should find that in your setup, when using Microsoft.AspNetCore.Mvc.ApiExplorer version 2.1.3 it contains metadata about all your controllers, and on 2.2.0 it contains empty metadata (I don't know why, but that is the symptom you are seeing)
  • If so, start to strip your example right back:
    • Completely remove all the Swagger and Swashbuckle stuff, and check that the problem replicates (i.e. has data in 2.1.3 but is empty in 2.2.0)
    • Take everything back to one single project of your own, not two, and check that the problem still replicates
    • Remove absolutely everything which is (apparently...) non-essential to your problem from your startup.cs and NuGet dependencies, and check that the problem still replicates

By this point, you will either have a very minimal example of something which works fine with Microsoft.AspNetCore.Mvc.ApiExplorer version 2.1.3 and which doesn't work but should with Microsoft.AspNetCore.Mvc.ApiExplorer version 2.2.0 (and not involving Swashbuckle.AspNetCore at all), or else you will maybe have realised what it was about your specific config setup which was causing the problem.

Obviously, report back if after stripping things back like this, you still find that you think that the problem lies in Swashbuckle.AspNetCore and not the Microsoft project.

HTH ;)

@erinev
Copy link
Author

erinev commented Jun 2, 2019

Thanks for detailed instructions and hints what to do next, i will try it and i will update you with results

@erinev
Copy link
Author

erinev commented Jun 4, 2019

Update on the issue:

In master branch: https://github.com/erinev/net-core-api-for-digging-swashbuckle-issue/tree/master
i left one project (Host) and also i removed all code which is not essential for swashbuckle to work

everything works with ApiExplorer 2.1.3.

i created a branch: https://github.com/erinev/net-core-api-for-digging-swashbuckle-issue/pull/2/files where i update ApiExplorer to 2.2.0.

and ofcourse swagger is not rendered.
but what i noticed that not just swagger is no longer works actually all mvc routing stops working.
it is not possible to perform GET request from browser (it returns 404)
after running project f5 and opening url: http://localhost:5000/v1/demo/resource in master branch ir works (returns string) in branch it returns 404 even though routing stayed the same

so problem is really not in the swashbuckle, but our project setup (or some missing nuget packages)

@erinev
Copy link
Author

erinev commented Jun 4, 2019

i also created branch with removed swashbuckle: https://github.com/erinev/net-core-api-for-digging-swashbuckle-issue/tree/remove_swashbuckle

And after update of ApiExplorer to 2.2.0: erinev/net-core-api-for-digging-swashbuckle-issue#3

routings stops working and all requests returns 404

so problem really lies somewhere else

@mikebeaton
Copy link
Contributor

Hi. That's great. If you're happy that this issue doesn't lie with this repo, then you should probably close off this issue, if that's okay?

Just in the spirit of continuing to help, what I would try next is...

  • Create a brand new project - either directly from, or as close as possible to, a .NET Core project template - using ApiExplorer 2.2.0, just to prove to yourself that it does work sometimes

  • Modify the working ApiExplorer 2.2.0 project and your non-working project step by step until they are the same - at which point you will have found out how to make yours work, or what step stops the other one from working!

Keep me posted if you like but also, as I say, if the issue isn't here then probably close off this issue? Thanks!

@erinev
Copy link
Author

erinev commented Jun 5, 2019

ok i will close this issue, thanks for another notes.

@erinev erinev closed this as completed Jun 5, 2019
@erinev
Copy link
Author

erinev commented Jun 5, 2019

@mikebeaton your suggestion (comparing with default API template by MS) worked! :)

i found and issue:

in order to make ApiExplorer v2.2.0 version work one line needs to be changed in .csproj of HOST project: from <Project Sdk="Microsoft.NET.Sdk"> to <Project Sdk="Microsoft.NET.Sdk.Web">

just i don't know why,
but in default MS API template if i change Sdk to Microsoft.NET.Sdk it instantly stops working

i will go to google it what are the differences and is it safe to change to .Web skd

@DanielCaspers
Copy link

@mikebeaton your suggestion (comparing with default API template by MS) worked! :)

i found and issue:

in order to make ApiExplorer v2.2.0 version work one line needs to be changed in .csproj of HOST project: from <Project Sdk="Microsoft.NET.Sdk"> to <Project Sdk="Microsoft.NET.Sdk.Web">

just i don't know why,
but in default MS API template if i change Sdk to Microsoft.NET.Sdk it instantly stops working

i will go to google it what are the differences and is it safe to change to .Web skd

For others attempting the fix quoted, you may have to recreate your publish profile(s) to be of the Web project type, since the shape and parameters in them are different. After that, it worked perfectly :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants