Is it psossible to make version selection a dropdown instead of text entry in Swagger? #1088
-
I am curious whether it's possible to make it a fixed list of avialable versions. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Or maybe it can have one fixed value defined by the selection: |
Beta Was this translation helpful? Give feedback.
-
Yes - it is possible. In the example project, change the line: to parameter.Schema.Enum = [OpenApiAnyFactory.CreateFromJson( json )]; This will yield the API version as an enumeration. It should be noted that the documented API may not be representative of all API versions. The model, parameters, and so on may be different across API versions, even if the URL is the same (which it should be). You can use the supported API versions to produce a full list, but realize that this is for the entire API (ex: the Orders API) and not a specific endpoint. It is possible that an endpoint may not exist in a supported API version. If that doesn't apply to you or you don't care about that limitation, you can list all supported API versions like so: var metadata = apiDescription.ActionDescriptor.GetApiVersionMetadata();
var model = metadata.Map( ApiVersionMapping.Explicit | ApiVersionMapping.Implicit );
parameter.Schema.Enum =
model.SupportedApiVersions.Select(
version => OpenApiAnyFactory.CreateFromJson(
JsonSerializer.Serialize(
version.ToString(),
modelMetadata.ModelType ) ) ).ToImmutableArray();
parameter.Schema.Default =
OpenApiAnyFactory.CreateFromJson(
JsonSerializer.Serialize(
description.DefaultValue,
modelMetadata.ModelType ) ); This will also continue to set the default value so that the default selection is whichever API version you are currently viewing. I hope that helps. |
Beta Was this translation helpful? Give feedback.
Yes - it is possible. In the example project, change the line:
aspnet-api-versioning/examples/AspNetCore/WebApi/OpenApiExample/SwaggerDefaultValues.cs
Line 59 in 3fc0719
to
This will yield the API version as an enumeration.
It should be noted that the documented API may not be representative of all API versions. The model, parameters, and so on may be different across API versions, even if the URL is the same (which it should be). You can use the supported API versions to produce a full list, but realize that this is for the entire API…