-
Notifications
You must be signed in to change notification settings - Fork 280
Description
I reduced my schema filter to this minimal example:
public class TestFilter : ISchemaFilter
{
public void Apply(OpenApiSchema schema, SchemaFilterContext context)
{
if(context.Type.Namespace == "MyProject.Services.Models")
{
foreach (var modelProp in context.Type.GetProperties())
{
var key = System.Text.Json.JsonNamingPolicy.CamelCase.ConvertName(modelProp.Name);
var schemaProp = schema.Properties[key];
var type = modelProp.PropertyType;
var underlyingType = Nullable.GetUnderlyingType(type);
if (underlyingType != null)
{
schemaProp.Example = new OpenApiNull();
if (underlyingType.IsEnum)
{
; // breakpoint
}
}
}
}
}
}This works as expected for properties with types like int? and bool?. The example JSON will contain properties like "someProperty": null. The breakpoint is hit, proving that this code is running for nullable enum properties. But the example JSON for a nullable enum property is still "someProperty": 0 or "someProperty": "EnumValue" depending on JsonStringEnumConverter.
As an experiment, I changed the body of Apply to this:
var underlyingType = Nullable.GetUnderlyingType(context.Type);
if (underlyingType != null)
{
schema.Example = new OpenApiNull();
if(underlyingType.IsEnum)
{
; // breakpoint
}
}This also works as expected for properties with types like int? and bool?. Unexpectedly, the breakpoint here is never hit.
Then I changed Apply to this:
if (context.Type.IsEnum)
{
schema.Example = new OpenApiNull();
}Now all enum properties have an example value of null.
It seems that for most property types, the example JSON generator looks at OpenApiSchema.Example on the property schema as expected. But when the property is an enum type, it uses the example value for the property type instead of the property.
Incidentally, I also noticed that OpenApiSchema.Nullable is true for nullable primitives but false for nullable enums.