Closed Yoztastic closed 6 months ago
i believe this is the same thing as https://github.com/FastEndpoints/FastEndpoints/issues/388 so the solution is to do this:
.SwaggerDocument(o => o.DocumentSettings = d => d.MarkNonNullablePropsAsRequired());
which results in output like this:
"components": {
"schemas": {
"CompoundInstructionResponse": {
"type": "object",
"additionalProperties": false,
"required": [
"requestId",
"results"
],
"properties": { },
"results": { }
}
}
},
"InstructionRecord": {
"type": "object",
"additionalProperties": false,
"required": [
"instruction"
],
"properties": { }
}
if that's still not constrained enough, you can just copy this nswag processor and add what you need.
as for infering the nullability/required info from validators, that's only supported for request DTOs as we've never seen anyone use validators for response DTOs before.
Awesome!
So I think I need something like
public class MarkNonNullableArrayPropsAsRequired : ISchemaProcessor
{
public void Process(SchemaProcessorContext context)
{
foreach (var (_, prop) in context.Schema.ActualProperties)
{
if (!prop.IsNullable(SchemaType.OpenApi3) && prop.IsArray)
{
prop.IsRequired = true;
prop.IsNullableRaw = false;
}
}
}
}
and just
builder.Services.SwaggerDocument(options =>
{
options.DocumentSettings = s =>
{
s.MarkNonNullablePropsAsRequired();
s.SchemaSettings.SchemaProcessors.Add(new MarkNonNullableArrayPropsAsRequired());
};
I have a request type for and Endpoint like
and an associated validator
the Swagger document generated reflect this constraint
However, the response type for this endpoint
Does not generate the desired swagger
note there is no required array including
results
and"nullable": false,
is absent.It seems to me that
FastEndpoints.EndpointDefinition
has bothType requestDtoType, Type responseDtoType
but only one propertypublic Type? ValidatorType { get; internal set; }
And that
FastEndpoints.Swagger.ValidationSchemaProcessor
only gets a list of the validators associated with the Request Type hence the swagger generated for Response Type is under constrained.It is quite important to include the fully constrained Swagger Schema since clients automate the JavaScript type definitions from this schema and they therefore get bloated with null checks that are redundant.
Is there a known work around to this?