Skip to content

Consider to return camelCase error messages #195

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

Closed
hikalkan opened this issue Sep 28, 2017 · 3 comments
Closed

Consider to return camelCase error messages #195

hikalkan opened this issue Sep 28, 2017 · 3 comments
Assignees
Milestone

Comments

@hikalkan
Copy link

hikalkan commented Sep 28, 2017

Instead of

{
  "Error": {
    "Code": "UnsupportedApiVersion",
    "Message": "The HTTP resource that matches the request URI..."
  }
}

return this:

{
  "error": {
    "code": "UnsupportedApiVersion",
    "message": "The HTTP resource that matches the request URI..."
  }
}

Because it's more natural for javascript clients and ASP.NET Core team also use camelCase.

Or is there any configuration?

@hikalkan
Copy link
Author

hikalkan commented Sep 28, 2017

I suppose we can override default behavior:

services.AddApiVersioning(o =>
{
    o.ErrorResponses = //create a class implements IErrorResponseProvider
});

@commonsensesoftware
Copy link
Collaborator

Thanks for bringing this to my attention. I didn't realize that the default JSON settings in ASP.NET Core had changed. In ASP.NET Web API, the error messages are based on the HttpError class. Ultimately, this class was just a basic extension of Dictionary<string, object>. As a result, the ASP.NET Core DefaultErrorResponseProvider uses a simple dictionary to generate the error response content. Unfortunately, the new, default JSON settings in ASP.NET Core does not apply to dictionaries (intentionally). There are number of issues and discussions in the ASP.NET repo about this.

To achieve your desired result, you just need to change your setup like this:

services.AddMvc()
        .AddJsonOptions( o => 
            o.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver() );

This should work as is today. The error responses, however, are still inconsistent with the default JSON serialization behavior so it's worth changing the way responses are generated. I'll update the implementation to return an object instead of a dictionary to provide the content. This will achieve parity with the default serialization behavior.

One final note. The enhancement to align serialization behavior will result in one small side effect. Null values are returned in JSON with the default settings. To suppress this, you'll still need to change the default JSON settings, but it will apply to the entire ASP.NET Core application.

services.AddMvc()
        .AddJsonOptions( o => o.SerializerSettings.NullValueHandling = NullValueHandling.Ignore );

I find this to be a negligible compromise.

@hikalkan
Copy link
Author

I'll update the implementation to return an object instead of a dictionary to provide the content

That would be great. Thanks.

commonsensesoftware pushed a commit that referenced this issue Sep 30, 2017
…ich will result in matching default the JSON serialization settings. Resolves #195
commonsensesoftware pushed a commit that referenced this issue Oct 1, 2017
…ich will result in matching default the JSON serialization settings. Resolves #195
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants