cosullivan / Hypermedia

Hypermedia library for .NET
http://cainosullivan.com/Hypermedia
MIT License
47 stars 12 forks source link

Hypermedia.JsonApi.AspNetCore is not formatting class attributes for .NET Core 2.2 #18

Open gabrielbutron opened 5 years ago

gabrielbutron commented 5 years ago

Hi, I have problems formatting attribute names in .NET Core 2.2 using Hypermedia. Below is an example of the result of a formatted object:

{ "data":{ "type":"products", "id":"1", "attributes":{ "Name":"ProductA", "Price":10, "Description":"descripcion 1", "OwnerName": "OwnerA" } } }

As you see we have the attribute names with Capital letter ("Name" instead of "name"). Composite words are not dashed and also start with Capital letter ("OwnerName" instead of "owner-name").

I'm missing something? Thanks in advance.

Versions: .NET Core 2.2 Hypermedia 1.1.0 Hypermedia.JsonApi.AspNetCore 1.3.6

cosullivan commented 5 years ago

Hi,

Are you calling the AddHypermediaFormatters extension method to configure? If so this uses the DasherizedFieldNamingStrategy as its default;

https://github.com/cosullivan/Hypermedia/blob/master/Src/Hypermedia.JsonApi.AspNetCore/MvcBuilderExtensions.cs#L32

But you can call one of the other methods that takes the IFieldNamingStrategy and pass in the DefaultFieldNamingStrategy.Instance instead.

Thanks, Cain.

gabrielbutron commented 5 years ago

Looking at your code, gave me the idea to use options.JsonApiSerializerOptions.FieldNamingStrategy instead of options.FieldNamingStrategy and it worked.

services .AddMvc() .AddHypermediaFormatters( options => { options.ContractResolver = contractResolver; options.JsonApiSerializerOptions.FieldNamingStrategy = DasherizedFieldNamingStrategy.Instance; }) .SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

I was confused because in the example code it is using options.FieldNamingStrategy: https://github.com/cosullivan/Hypermedia/blob/master/Src/Hypermedia.Sample.AspNetCore/Startup.cs#L31

Not sure if this was intended and we want to change the code base or the example :)

cosullivan commented 5 years ago

I just had a look over that configuration code and yes that its quite confusing.

The IFieldNamingStrategy and IContractResolver can be set in two places and I can't remember why that is the case now. I will have a look and see if I can refactor that so its cleaner.

Thanks, Cain.