AspNetCoreOData icon indicating copy to clipboard operation
AspNetCoreOData copied to clipboard

Change PageResult<T> property names on serialization

Open devuros opened this issue 2 years ago • 5 comments

Hi,

I have a .NET 5 web API with OData 8.0.6 installed. Currently I'm hitting an endpoint and I get the result as:

{
  "items": [ ... ],
  "count": 10
}

I want the the JSON response to be Uppercased like "Items" and "Count". I see there is an extension method EnableLowerCamelCase but I don't see any other method witch does the opposite like pascal casing.

ConfigureServices goes like:

services.AddControllers(options =>
{
    options.Filters.Add(typeof(MyCustomFilter));
})
.AddJsonOptions(options =>
{
    // I also applied pascal casing for API
    options.JsonSerializerOptions.PropertyNamingPolicy = null;
})
.AddOData(opt =>
{
    var builder = new ODataConventionModelBuilder();

    // this is the data source for PagingResult I'm returning
    builder.Function("Myfunction").ReturnsCollection<MyfunctionResult>();

    opt.AddRouteComponents(builder.GetEdmModel());
});

How can I achieve a paging result response to look like this?

{
  "Items": [ ... ],
  "Count": 10
}

devuros avatar Apr 27 '22 13:04 devuros

Looks like the values for these properties are completely hardcoded: https://github.com/OData/AspNetCoreOData/blob/69eec03c7003fe12d92cdc619efdc16781683694/src/Microsoft.AspNetCore.OData/Results/PageResultValueConverter.cs#L60-L71

julealgon avatar Apr 27 '22 21:04 julealgon

@julealgon Thanks, much appreciated! Anyway do you think its a good idea to request change to support pascal casing for paging result?

devuros avatar Apr 27 '22 21:04 devuros

Anyway do you think its a good idea to request change to support pascal casing for paging result?

I definitely do, hence my upvote on your post. This should definitely be configurable in a way, if not only for consistency purposes.

julealgon avatar Apr 28 '22 12:04 julealgon

@julealgon How does it work though? This is the first issue I've ever created. Not sure how to proceed

devuros avatar Apr 28 '22 12:04 devuros

@julealgon How does it work though? This is the first issue I've ever created. Not sure how to proceed

Having this issue open here should be enough. People on the team will eventually look into it.

julealgon avatar Apr 28 '22 14:04 julealgon

Any progress with this issue? Is there a plan to fix it in the near future? Its a pinpoint - If the entire API is PascalCase, it looks ugly that only the page result contains camelCase properties.

mirgil avatar Jul 02 '23 09:07 mirgil