-
-
Notifications
You must be signed in to change notification settings - Fork 158
Allow for multiple naming conventions (camelCase vs kebab-case) #555
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This seems the way to go, with the option to turn on kebab-case if need be |
You can define a newtonsoft Let me know if you figure it out |
I've changed the ContractResolver using and this is my using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Humanizer;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace Serialization
{
public class CamelCaseContractResolver : DefaultContractResolver
{
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
JsonProperty property = base.CreateProperty(member, memberSerialization);
property.PropertyName = property.PropertyName.Camelize();
return property;
}
}
} but I'm getting kebab-case result on my api |
I've added this nameformatter: using System;
using System.Reflection;
using Humanizer;
using JsonApiDotNetCore.Graph;
using JsonApiDotNetCore.Models;
namespace Serialization
{
public class CamelCaseResourceNameFormatter : IResourceNameFormatter
{
public string FormatResourceName(Type resourceType)
{
try
{
// check the class definition first
// [Resource("models"] public class Model : Identifiable { /* ... */ }
if (resourceType.GetCustomAttribute(typeof(ResourceAttribute)) is ResourceAttribute attribute)
return attribute.ResourceName;
return ApplyCasingConvention(resourceType.Name.Pluralize());
}
catch (InvalidOperationException e)
{
throw new InvalidOperationException($"Cannot define multiple {nameof(ResourceAttribute)}s on type '{resourceType}'.", e);
}
}
public string FormatPropertyName(PropertyInfo property) => ApplyCasingConvention(property.Name);
public string ApplyCasingConvention(string properName) => properName.Camelize();
}
} and I registered it on and now all of my resources are camelized except for the route names and pagination links are referring to wrong path |
What exactly is the output you're having and what is it you're expecting? Do you mean they're still in kebab-case and you had expected the links to be pascalCase as well? Good catch by the way, a
|
the routes remain kebab-case but the pagination links will use camelCase resource names which results in 404 error |
The routing convention is applied during service registration here by the JsonApiDotNetCore/src/JsonApiDotNetCore/Internal/DasherizedRoutingConvention.cs Lines 35 to 37 in 4481147
Otherwise, you can inject your own
|
I agree. But I think we should generalise this further by adding the following rules:
So that would imply the following document with corresponding URLs:
Additionally, to prevent inconsistent URLs like Currently the official recommendation for $ATTRIBUTE and $RELATIONSHIP is I would propose to have this as the default configuration, and on top of that allow for an option to use The specs recommendations for URLs are ambiguous in this matter, see json-api/json-api#1431 (comment) Thoughts are welcome |
New insight. This is relevant: json-api/json-api#1091 (comment). For sparse field selection, if we stick to We currently already support sparse field selection on relationships and if we wish to keep on supporting this (which I think we should) we cannot go with Hence, updated proposal
Note that this changes nothing with respect to normalizing the casing for |
Description
json:api documentation recommends using camelCase instead of kebab case here and is discussed on json-api/json-api#1255.
Is it possible to change naming convention to camelCase or adding configuration for naming convention?
The text was updated successfully, but these errors were encountered: