Skip to content
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

JsonApi Ignoring MissingMemberHandling Serializer Setting #122

Open
ShawetaKumar opened this issue Jul 14, 2020 · 1 comment
Open

JsonApi Ignoring MissingMemberHandling Serializer Setting #122

ShawetaKumar opened this issue Jul 14, 2020 · 1 comment

Comments

@ShawetaKumar
Copy link

ShawetaKumar commented Jul 14, 2020

Hi,

I am setting the MissingMemberHandling to Error in the JsonApiSerializerSettings so that if there are any unknown/non-declared property in the json, I get deserialization error. However the setting is getting ignored.

          `var json = "{ \"data\": {  \"id\": \"1\",  \"type\" : \"product\",  \"attributes\" : {    \"name\": \"name\",    \"summary\": \"summary\"  } }}";

            var jsonApiSerializerSettings = new JsonApiSerializerSettings
            {
                MissingMemberHandling = MissingMemberHandling.Error,
            };

            var product = JsonConvert.DeserializeObject<Product>(json, jsonApiSerializerSettings);`

Whereas when using the same setting with JsonSerializerSettings, the error do get generated.

           `var json = "{  \"id\": \"1\",  \"type\" : \"product\",  \"name\": \"name\", \"summary\": \"summary\" }";

            var jsonSerializerSettings = new JsonSerializerSettings
            {
                MissingMemberHandling = MissingMemberHandling.Error,
            };

            var product = JsonConvert.DeserializeObject<Product>(json, jsonSerializerSettings);`

Below error gets generated at the deserialization:
Could not find member '{property}' on object of type '{type}'

I have also created a sample application for reference:
https://github.com/ShawetaKumar/JsonDeserialization

I have also added a failing test to the code to verify the same
When_passed_unknown_property_should_throw_exception_when_missing_member_handling_set_to_error
https://github.com/ShawetaKumar/JsonApiSerializer/blob/bug/missing-member-handling/tests/JsonApiSerializer.Test/DeserializationTests/DeserializationExceptionTests.cs

@alastairtree
Copy link

Support for MissingMemebrHandling.Error is required to implement this particular part of the JSON:API spec where you want the API to reject unsupported updates which contain attributes which are not recognised:

A server MUST return 403 Forbidden in response to an unsupported request to update a resource or relationship.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants