Issue with Minimal API OData Results respecting the configured JsonOptions.
Assemblies affected
Microsoft.AspNetCore.OData 9.4.0-preview
Describe the bug
I'm currently exploring the preview release version listed above and noticed that when using the .WithODataResult() on a minimal api endpoint, the Microsoft.AspNetCore.Http.Json.JsonOptions configured in the DI container don't seem to be applied when serializing the result.
Reproduce steps
I used the 'ODataMiniApi' project in the sample folder to test the new Minimal API feature.
- Update the JsonOptions by adding the two serializer options as shown below:
builder.Services.ConfigureHttpJsonOptions(options => {
options.SerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
options.SerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
...
});
- Call the
/giveschools2endpoint:
GET http://localhost:5177/giveschools2?$filter=schoolId le 2
Response
{
"@odata.context": "http://localhost:5177/$metadata#School",
"value": [
{
"SchoolId": 1,
"SchoolName": "Mercury Middle School"
},
{
"SchoolId": 2,
"SchoolName": "Venus High School"
}
]
}
Expected behavior
This example doesn't show any null properties in the result, but if the entity has nullable properties, they will be included in the result, ignoring the json configuration to skip writing properties with null values.
@andy-clymer I think that's expected, WithODataResult takes the ODL to do the serialization, ODL hasn't taken the JsonOptions yet.
I remember there are several similar issues created for controller based API also. You can provide the Edm model using WithODataModel() as property name camel case, or Please provide a IODataModelConfiguration to enable the camel case for your endpoint.
Of course, this issue helps us to design more about how to take the JsonOptions into consideration.
var modelBuilder = new ODataConventionModelBuilder(); modelBuilder.EnableLowerCamelCase(); // Add this line