Mauro Bilotti
Mauro Bilotti

Reputation: 6252

OData is returning Upper-case properties when filtering

I'm working on a project which has a front-end made in Reactjs + axios, and a backend with .NET Core 3.1 Web API which is configured to allow OData filtering.

Everything is working fine, but I only have one small problem.

If I consume, e.g: GET /api/questions without any OData filtering, I receive:

[
    {
        "id": "005b46c6-1811-42f2-b917-00178e916d2e",
        "value": "List the requirements for Schedule III, IV, & V Prescriptions.",
        "required": true,
        "sequence": 0,            
        "questionType": {
            "id": "4227e559-3b14-4e07-b90e-73151f723698",
            "name": "Rating",
            "value": "rating-question",
            "description": "a question in where the user rates the answer"
        },
        "ratingSetting": {
            "id": "5e6a883a-084d-4400-bdf2-78b97e2eba18",
            "name": "Poor to Excellent",
            "minValueText": "Poor",
            "maxValueText": "Excellent",
            "optionCount": 5
        },    
        "answers": []
    },
    ...
]

But if I perform some filtering, for example: GET /api/admin/questions?$select=id, value&$expand=questionType($select=name)&$expand=ratingSetting($select=name) the result contains an array where every property has changed to Upper-case:

[
    {
        "QuestionType": {
            "Name": "Rating"
        },
        "RatingSetting": {
            "Name": "Poor to Excellent"
        },
        "Id": "005b46c6-1811-42f2-b917-00178e916d2e",
        "Value": "List the requirements for Schedule III, IV, & V Prescriptions."
    },
    ...
]

As you can see, every property is upper case. This is causing me trouble when I try to bind the result object, because it's not the same question.id against question.Id, so I'm having an undefined exception in the front-end.

Is there any configuration that I have to perform in my backend to solve this?

Upvotes: 1

Views: 759

Answers (2)

Alex
Alex

Reputation: 2299

Install these libraries:

<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Microsoft.AspNetCore.OData.NewtonsoftJson" Version="8.0.4" />

Then add this in startup.cs or program.cs:

services.AddControllers().AddOData(options =>
        {
            options.EnableQueryFeatures(maxTopValue: 500);
        })
            .AddODataNewtonsoftJson()
            .AddNewtonsoftJson(options =>
        {
            options.SerializerSettings.ContractResolver =
                new CamelCasePropertyNamesContractResolver();

            options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
        });

Upvotes: 0

Lance
Lance

Reputation: 632

Make sure you are using a contract resolver by specifying the appropriate NewtonSoftJsonOptions:

services.AddControllers(options =>
    options.EnableEndpointRouting = false)
.AddNewtonsoftJson(options =>
{
    options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
});

Upvotes: 1

Related Questions