question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Deserializer only links attributes and not included relationships

See original GitHub issue

For a simple model like below

    public class Customer : Identifiable
    {
        [HasMany] public List<Order> Orders { get; set; }
    }

    public class Order : Identifiable
    {
        [HasMany] public List<Invoice> Invoices { get; set; }
    }

    public class Invoice : Identifiable { }

Serializer works fine and get customer with included relationship returns the desired response /api/v1/customers?included=orders.invoices

{
    "data": {
        "attributes": {},
        "relationships": {
            "orders": {
                "links": {
                    "self": "https://localhost:44351/customers/1234/relationships/orders",
                    "related": "https://localhost:44351/customers/1234/orders"
                },
                "data": [
                    {
                        "type": "orders",
                        "id": "4567"
                    }
                ]
            }
        },
        "type": "customers",
        "id": "1234"
    },
    "included": [
        {
            "attributes": {},
            "relationships": {
                "invoices": {
                    "links": {
                        "self": "https://localhost:44351/orders/4567/relationships/invoices",
                        "related": "https://localhost:44351/orders/4567/invoices"
                    },
                    "data": [
                        {
                            "type": "invoices",
                            "id": "7890"
                        }
                    ]
                }
            },
            "type": "orders",
            "id": "4567"
        },
        {
            "attributes": {},
            "type": "invoices",
            "id": "7890"
        }
    ]
}

However on using the same json as input for post/put operation and trying to deserialize into customer instance with all fields/relationships populated, it does not load included relationship. In this instance, customer has orders attributes populated but invoices is appearing blank.

image

Is it because we are only setting attributes and not relationship. Can we please modify it to add second level or more relationship be included as well. https://github.com/json-api-dotnet/JsonApiDotNetCore/blob/9059560ffdaa9cc263fd352bb7dc1106255d6148/src/JsonApiDotNetCore/Serialization/JsonApiDeSerializer.cs#L323

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
maureicommented, Apr 25, 2019

I think it should be deserializing whatever is available in json input and not create/update nested relationships

I totally agree, I’m in!

Work that will be involved:

  1. Serialization layer needs to be decoupled (related to #263, #253): this will affect JsonApiContext , (in particular, I think, the exposure of HttpContextAccessor) so that we can decouple it from query params.
  2. Need to prevent nested relationship create/update in default implementation of repository/service layer
  3. Need to investigate impact on other components of the framework
  4. Lots of rewriting of tests of serializers where mocks of JsonApiContext are used

This will surely involve breaking changes in the API. I hope I can make a proposal where to start on soon. If you feel like getting involved, we would greatly encourage that!

0reactions
maureicommented, Oct 10, 2019

resolved in #558

Read more comments on GitHub >

github_iconTop Results From Across the Web

Deserializing relationships to an object · Issue #65
The default behaviour of deserialize() seems to be to return an array of relationships keys. If the top-level key "included" is set, it...
Read more >
Serialize but not deserialize fields/properties in XML, JSON ...
I have tested and applied this only to XML serialization, but it works for me: When I want a property to be serialized,...
Read more >
Customizing Serializers - Models - Ember Guides
In Ember Data the convention is to camelize attribute names on a model. ... JSON payload but only include the relationship's id when...
Read more >
Serializer relations
A serializer with a field spanning an orm relation through its source attribute could require an additional database hit to fetch related objects...
Read more >
Bidirectional Relationship Support in JSON
This article provides a robust working approach to creating JSON structures that include a bidirectional relationship without resulting in these errors.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found