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

Does .NET 9's OpenApi support render this obsolete? #1115

Open
armanossiloko opened this issue Nov 16, 2024 · 7 comments
Open

Does .NET 9's OpenApi support render this obsolete? #1115

armanossiloko opened this issue Nov 16, 2024 · 7 comments

Comments

@armanossiloko
Copy link

In the recently released .NET 9, there's built in support for generating openapi documents. However, these documents have to be predefined by specifying services.AddOpenApi();.

This means that all API document names have to be defined in advance before the IServiceProvider is even built. This seems to totally "break" the flow of the Asp.Versioning.Mvc.ApiExplorer package. If I am not wrong, it was Microsoft themselves who delegated the original Microsoft.AspNetCore.Mvc.Versioning to the community which ended up turning into this aspnet-api-versioning.

Since documents have to be known in advance now and this package was basically providing an IApiVersionDescriptionProvider in runtime, does .NET 9's default OpenApi document generation render aspnet-api-versioning obsolete? None of what Micorosft provided with .NET 9 seems to display proper versioning support at all, just some different ways to generate documents at build time. Is there a way to integrate this with .NET 9's default OpenApi support now or am I missing something here?

@armanossiloko armanossiloko changed the title Does .NET 9' OpenApi support render this obsolete? Does .NET 9's OpenApi support render this obsolete? Nov 17, 2024
@captainsafia
Copy link
Member

@armanossiloko There was a little bit of conversation around this issue when I took a stab at updating our eShop sample app, which uses ASP.NET versioning, to use the new OpenAPI package in dotnet/eShop#360 (comment). Ultimately, the (ugly) resolution that we landed on in that PR is to declare the versions up front as you mentioned.

At the time, @commonsensesoftware mentioned having some ideas around API changes to make in Asp.Versioning to support easier resolution of documents. I think I also recall some ideas thrown around for an AddVersionedOpenApi API that would encapsulate the document registration and the versioning pattern in one.

@commonsensesoftware
Copy link
Collaborator

@captainsafia Thanks for digging that up and refreshing my memory.

@armanossiloko at this very moment I don't have a working example I can show. What you should need to do is use a IConfigureOptions or IPostConfigureOptions on OpenApiOptions where you inject IApiVersionDescriptorProvider and append the necessary entries. This is effectively the same process that is used with Swashbuckle (which you can see here). I haven't tried it myself, but that should work. I'm actively working on getting a release ready for .NET 9. Thanks for bringing this up. I will look into providing an out-of-the-box integrated solution or, at least, provide an end-to-end example that shows all of the pieces working with the new library. If you find any other sharp edges with what's possible right now, feel free to share or post an issue.

"If I am not wrong, it was Microsoft..."

Actually, you'd be wrong, but that's ok. It's common misunderstanding. I've always been the sole owner, maintainer, documenter, etc of API Versioning. I was once a Microsoft employee so in that sense it was Microsoft. When I left, the transition was less than smooth. There was a lot of things that had to change such as moving the project under the .NET Foundation and renaming packages. All that being said, I've had, and continue to have, a good working relationship with the ASP.NET team, but I've never been part of the team and they've never been co-owners of API Versioning - despite the name. I hope that clears that up. 😉

@robertcoltheart
Copy link

What you should need to do is use a IConfigureOptions or IPostConfigureOptions on OpenApiOptions where you inject IApiVersionDescriptorProvider and append the necessary entries.

Looking at the source code for OpenApi, I'm pretty certain you can't do this. AspNetCore adds keyed singleton services keyed by the API name, and registers multiple OpenApiOptions instances, again keyed by API name. Happy to be proven wrong, but I don't think the above will work. See https://github.com/dotnet/aspnetcore/blob/main/src/OpenApi/src/Extensions/OpenApiServiceCollectionExtensions.cs#L55

@mikekistler
Copy link

The eShop demo app uses Asp.Versioning to implement versioning for its Catalog API and I just updated it to add a new API version. There is the small disconnect that AddOpenApi wants the document names to be supplied "up front", but I worked around that by just ensuring that I used names that match the names expected by Asp.Versioning. Not perfect but it does work and I think the failure cases would be immediately apparent.

@armanossiloko
Copy link
Author

The eShop demo app uses Asp.Versioning to implement versioning for its Catalog API and I just updated it to add a new API version. There is the small disconnect that AddOpenApi wants the document names to be supplied "up front", but I worked around that by just ensuring that I used names that match the names expected by Asp.Versioning. Not perfect but it does work and I think the failure cases would be immediately apparent.

Thanks! It still sucks a lot that we have to go some hacky route to get it working, but it is what it is.

@elibroftw
Copy link

elibroftw commented Feb 3, 2025

Image

Jesus Christ it took 2 hours to migrate to .NET 9 while maintaining versioning support and upgrading to the latest bells and whistles (Scalar).

@armanossiloko
Copy link
Author

Image

Jesus Christ it took 2 hours to migrate to .NET 9 while maintaining versioning support and upgrading to the latest bells and whistles (Scalar).

:D It takes quite some effort. Not a fan of it myself either. Sadly, the "workflow" has become just harder and worse this way.

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

6 participants