In the Swashbuckle v5 beta, SwaggerDefaultValues : IOperationFilter has changed the interface. It now uses Microsoft.OpenApi.Models and looks like this:
using Microsoft.OpenApi.Models;
namespace Swashbuckle.AspNetCore.SwaggerGen
{
public interface IOperationFilter
{
void Apply(OpenApiOperation operation, OperationFilterContext context);
}
}
This also means that the parameter.Default = routeInfo.DefaultValue; does not work as it does not exist.
Maby this needs a new sample.
I would also like a quick hint to how I can resolve the Default value please :)
I only use Swashbuckle as one possible way to integrate Swagger. It's certainly not the only one and there are no dependencies on it here. There are plenty of other Swagger generators (ex: NSwag) and tools that make use of the API Explorer. In general, I do not support unreleased flavors of other libraries. It's simply too much for me to maintain and it's confusing for customers that only use released packages.
Your question is probably better answered in the Swashbuckle or OpenAPI.NET repos, but it appears you can set the default value via OpenApiParameter.Schema.Default or OpenApiParameter.Schema.Enum.
In previous discussions with Swashbuckle, the meaning of _default_ is a value that can be used when no value is provided by the user (ex: query parameter). A path parameter, on the other hand, is typically required, but can still have a default value. In Swagger parlance, that would be modeled as an enumeration with a single value. I'm not sure which of these apply to you, but that's how you should be able to make it work.
Thank you for the info :)
@ThomasCronholm did you find any more elegant solution than
if (parameter.Schema.Default == null)
{
parameter.Schema.Default = new OpenApiString(description.DefaultValue.ToString());
}
Or is that the way to do it with the OpenApiString?
@natelaff, I _believe_ that is the correct way. The v4.0 Swagger Sample does it that way and it seems to work.
We still don't have the support from Swashbuckle to intrinsically support default values that I'm aware of. I'm hopeful that will happen sometime after the alignment to the new OpenAPI libraries.
For everyone's edification, there was some resistance to the meaning of routeInfo.DefaultValue. I had a discussion with the Swashbuckle team and we agreed on the Swagger consumption of a default value and how it should be modeled. After careful examination of the API Explorer APIs, the ASP.NET team agreed this meant something different as well, but a true default parameter value was missing and needed. There is now ApiParameterDescription.DefaultValue, which didn't exist prior to ASP.NET Core 2.1/2.2 (can't remember which one now). This should be the value that is used. The new examples in API Versioning illustrate this.
I hope that helps.
Thanks @commonsensesoftware!
@natelaff Sorry for the late reply, no I did not find a more elegant solution.
@commonsensesoftware
I noticed the suggested solution assumes the default value is always a string, is that safe?
I found a factory that appears to create the correct type based on the schema and object.
Just curious if you think that would be a safer option?
if (parameter.Schema.Default == null && description.DefaultValue != null)
{
parameter.Schema.Default = OpenApiAnyFactory.CreateFor(parameter.Schema, description.DefaultValue);
}
Most helpful comment
@commonsensesoftware
I noticed the suggested solution assumes the default value is always a string, is that safe?
I found a factory that appears to create the correct type based on the schema and object.
Just curious if you think that would be a safer option?