I’m having a problem with .NET9.
In particular, this class represent the input of the endpoint where I am having the problem.
public class RequirementForAdd {
public string AttributeName { get; set; }
public RequirementOperator Operator { get; set; }
public string Value { get; set; }
public bool IsMustHave { get; set; }
}
In this class RequirementOperator is a simple Enum:
public enum RequirementOperator {
Equals,
GreaterThan,
LessThan,
GreaterThanOrEqual,
LessThanOrEqual,
}
I want that this Enum is accepted from the REST API as a string. Instead, even if I add the converter in this way:
// Add controllers services to the service collection, and configure JSON options.
builder.Services.AddControllers(options => {
options.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes =
true;// Suppress implicit required attribute for non-nullable reference types.
})
.AddJsonOptions(options => {
// Add JSON converter for enum strings and configure naming policy.
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
});
The API turns out to accept an integer as you can see in the openapi file generated (with the new system that use MapOpenApi()) :
"RequirementForAdd": {
"type": "object",
"properties": {
"attributeName": {
"type": "string",
"nullable": true
},
"operator": {
"$ref": "#/components/schemas/RequirementOperator"
},
"value": {
"type": "string",
"nullable": true
},
"isMustHave": {
"type": "boolean"
}
}
},
"RequirementOperator": {
"type": "integer"
},
I’m pretty sure that when I used .NET 8 it worked effortless and the API accepted strings for the enums.
Someone can support me and tell me how can I do to obtain that RequirementOperator is a string?
Thanks!