AspNetCoreOData icon indicating copy to clipboard operation
AspNetCoreOData copied to clipboard

Issue with Minimal API OData Results respecting the configured JsonOptions.

Open andy-clymer opened this issue 6 months ago • 2 comments

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.

  1. 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;
    ...
});
  1. Call the /giveschools2 endpoint:

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

Image

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 avatar May 30 '25 22:05 andy-clymer

@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.

xuzhg avatar Jun 02 '25 19:06 xuzhg

var modelBuilder = new ODataConventionModelBuilder(); modelBuilder.EnableLowerCamelCase(); // Add this line

gimmickj avatar Jun 25 '25 07:06 gimmickj