Skip to content

Error or Success Depending on Order of JSON Properties #142

@graham200472

Description

@graham200472

Great library. Doesn't look like it is still under active development but posting this nonetheless.

I have been working with a third party API that was providing json:api responses that were failing in my client deserialization, I thought due to duplicates in the "included" data. i.e. in the example below "otherThing1" was coming twice in the "included" data.

{
    "included": [
        {
            "id": "otherThing1",
            "type": "otherThing"
        },
        {
            "attributes": {
                "otherThingProperty": "otherThingProperty value"
            },
            "id": "otherThing1",
            "type": "otherThing"
        }
    ],
    "data": [
        {
            "attributes": {
                "thingProperty": "thingProperty value"
            },
            "id": "thing1",
            "relationships": {
                "otherThings": {
                    "data": [
                        {
                            "id": "otherThing1",
                            "type": "otherThing"
                        }
                    ]
                }
            },
            "type": "thing"
        }
    ]
}

The third party system was having trouble removing the dupes but in one of their fix attempts they provided a response where the order of "data" and "included" was switched, as below.

{
    "data": [
        {
            "attributes": {
                "thingProperty": "thingProperty value"
            },
            "id": "thing1",
            "relationships": {
                "otherThings": {
                    "data": [
                        {
                            "id": "otherThing1",
                            "type": "otherThing"
                        }
                    ]
                }
            },
            "type": "thing"
        }
    ],
    "included": [
        {
            "id": "otherThing1",
            "type": "otherThing"
        },
        {
            "attributes": {
                "otherThingProperty": "otherThingProperty value"
            },
            "id": "otherThing1",
            "type": "otherThing"
        }
    ]
}

Although the json was effectively the same, with the dupes still present, this deserialised fine. The same JsonConvert.DeserializeObject<DocumentRoot<Thing[]>>(inputJson, new JsonApiSerializerSettings()); statement was used for both.

I am using JsonApiSerializer 1.7.4 which is pulling in Newtonsoft.Json 11.0.2.

It would make sense to me if duplicates in "included" data was considered invalid, but I'm not sure why deserialisation works for one response and not the other.

It appears the library is order-sensitive in terms of the json provided, which doesn't seem right.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions