-
Notifications
You must be signed in to change notification settings - Fork 712
Using ApiExplorerSettingsAttribute together with ApiVersionAttribute produces unexpected number of ApiVersionDescriptions #1066
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
Comments
First, a point of clarity. The issue indicates you are targeting .NET 7, but the text says ASP.NET Core 6, which would be .NET 6. .NET 8 is the current version. Which version are you actually using? I need to make sure I know which code I'm looking at. Without additional context, it would appear that you have at least one other controller that that is Neither the API Explorer (a la I suspect you must have more controllers to observe this behavior. If not, then the world's simplest repo would go a lot way to recreating the conditions you have. I attempted to simulate the behavior from the information you have shared and the issue did not reproduce. |
Thank you for your reply. No, I don't have more controllers than one. I've created the repository which reproduces the problem. https://github.com/folbo/ApiVersioningBehaviorRepro/blob/master/ReproVersioningBug/Program.cs#L64C8-L64C8 I noticed that when I call the EDIT: You are correct, executing |
Thanks for the repro. It has been useful. It turns out that I had a repro, but I kept missing the scenario. This is a bug. The reason this happens is that collation needs to occur for All APIs ultimately register an If, and only if, you define a group name, the Minimal API logic still runs. It doesn't know how to distinguish a controller from any other 'ol This was easily missed because I don't believe there are any examples or tests that setup this specific combination. I also noticed that even though there are multiple versions reported, you still see the expected results in the UI. This will certainly be fixed. I already have some thoughts on how to do it. I have a couple of follow up questions:
The fix is simple, but supporting multiple TFMs is non-trivial and time consuming. Historically, I don't service older versions except for the rarest of scenarios (ex: security). Now that we're in a LTS/STS model for .NET, my policy (though not officially stated) will be primarily servicing the LTS releases. It's not advertised explicitly because there's always a chance I'll make the fix. As a virtual one man show, it's quite a bit of extra work. I can backport the fix to .NET 6, but I want to confirm that it's really necessary. It even seems that while there certainly is an issue, you might be able to live with it on .NET 6 because there are no visible side effects (in the UI). There might be another use case that is blocking you that I'm not privy to. The ideal scenario would be to only patch the .NET 8 release. If it is a blocking issue, there are some other immediate workarounds that you can do right now to unblock yourself. If you remove the collation for Minimal APIs, the issue is resolved. If you're stuck on .NET 6, this might be the compromise so you get the results you expect, you don't have to wait on me, and I don't have to backport. The behavior will still be correct after the fix when you update to .NET 8+, but it won't be required anymore. The following will remove Minimal API version collation: var services = builder.Services;
for (var i = 0; i < services.Count; i++)
{
var service = services[i];
if (service.ServiceType == typeof(IApiVersionMetadataCollationProvider)
&& service.ImplementationType == typeof(EndpointApiVersionMetadataCollationProvider))
{
services.RemoveAt(i);
break;
}
} |
Thank you for this broad explanation. I'm glad that what I've found out might be useful for further fixes. I think I can live with workarounds. To be honest I might not need to do that in .net6 since I think what I try to achieve is not feasible. Also I plan to eventually migrate to .net 8. In my usecase, which I didn't present in the example, I register a list of api definitions that contain api name, description, version - everything that is needed for Swashbuckle to generate a swagger document - in app startup code (DI registration phase). I wanted to make this configuration the only source of truth about what api do I host in which version.
If I only knew group names beforehand... Is it possible to configure Asp.Versioning with explicit group names? If not then I doubt I could make it my way without changing the core of this library. |
You're supposed to be able to. I've noticed another bug. 😞 I over-optimized the support for grouping and I now realize that if you only use a group name the There is an escape hatch here without a fix because you can replace the implementation. The fastest path to victory would be to subclass Line 59 in ef0aa08
Unfortunately, a lot of the critical bits that you'd want are internal, so you'll have to copy that part. It will be unchanged. The part that needs to change is here: Line 136 in ef0aa08
The logic should be: var formattedGroupName = groupName;
if ( string.IsNullOrEmpty( formattedGroupName ) )
{
formattedGroupName = formattedVersion;
}
else if ( formatGroupName is not null )
{
formattedGroupName = formatGroupName( formattedGroupName, formattedVersion );
} Then you can add (before builder.Services.AddSingleton<IApiVersionDescriptionProvider, MyApiVersionDescriptionProvider>();
It's little bit of work on your part, but that should get you on your way. |
…processed by min api endpoint collators. Related #1066
…plicit group names can only be determined at runtime. Related #1066
…processed by min api endpoint collators. Related #1066
…plicit group names can only be determined at runtime. Related #1066
Is there an existing issue for this?
Describe the bug
In my application I'm grouping some controllers into ApiExplorer groups to produce multiple swagger files. For each group I'm trying to implement independent versioning using Asp.Versioning.
When I mix
[ApiExplorer(GroupName = "WeatherForecastGroupName")]
with[ApiVersion(1)]
attributes myIApiVersionDescriptionProvider
creates two ApiVersionDescriptions, both with version 1. Provider's output attached below:Environment: AspNetCore 6 application with controllers.
My code used to reproduce the issue:
Expected Behavior
I'd expect that Asp.Versioning.ApiExplorer would use options.FormatGroupName as the only source of ApiVersionDescriptions if provided.
Steps To Reproduce
[AspVersion(1)]
and[ApiExplorerSettings(GroupName = "name")]
attributesIApiVersionDescriptionProvider
Exceptions (if any)
No response
.NET Version
7.0.403
Anything else?
No response
The text was updated successfully, but these errors were encountered: