Open michael-wolfenden opened 2 weeks ago
@michael-wolfenden Thanks for sharing this idea! I definitely see the value of a MapType
API, however, I don't think that's something we'll include out-of-the-box in the Microsoft.AspNetCore.OpenApi package.
It's possible to model the behavior of a MapType
method using schema transformers, like in the code sample below. This also gives you some extra abilities, like being able to modify what schemaFunc
looks like.
I'll mark this is a docs-related issue with the goal of implementing a MapType
implementation in a sample. In the future, you're welcome to use its source in your own code or ship a helper package for it.
Let me know if you have any questions about this!
public static OpenApiOptions MapType<T>(this OpenApiOptions options, Action<OpenApiSchema> schemaFunc)
{
return options.UseSchemaTransformer((schema, context, ct) =>
{
if (context.Type == typeof(T))
{
var targetSchema = schemaFunc();
schema.Type = targetSchema.Type;
}
return Task.CompletedTask;
});
}
Is there an existing issue for this?
Is your feature request related to a problem? Please describe the problem.
Using
Microsoft.AspNetCore.OpenApi -Version 9.0.0-preview.5.24306.11
The following code:
correctly generates an OpenAPI specification with an
int64
parameter and anint64
responseIf I want to use a strongly typed Id for
OrderId
instead of a primitive,The generated OpenAPI specification has a with an
int64
/object
hybrid parameter (I assume due thelong
constraint) and anobject
response.I would prefer to map
OrderId
as a primitiveint64
as per the first example.Describe the solution you'd like
Squashbuckle has the ability to override the schema for specific types, for example
I would like to see the same functionality in
Microsoft.AspNetCore.OpenApi
Additional context
No response