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

How to deserialize a class with a relationship that is a derived class? #696

Closed
marioGomezDev opened this issue Feb 19, 2020 · 3 comments · Fixed by #833
Closed

How to deserialize a class with a relationship that is a derived class? #696

marioGomezDev opened this issue Feb 19, 2020 · 3 comments · Fixed by #833
Assignees
Labels

Comments

@marioGomezDev
Copy link

Description

My domain are set up like this:

abstact class Person {
	...
}

class Student {
	...
}

class Teacher{
	...
}

class Article {
	Person Author
}

When I post an Article, it fails to deserialize the specific Teacher or Student class, how can I fix it?
...

Environment

  • JsonApiDotNetCore Version: 4.0.0.0
@maurei
Copy link
Member

maurei commented Feb 20, 2020

Thanks for reporting this issue. There might be some issues with the (de)serialization of inherited properties, but to be sure what is going wrong and to see if we need to patch anything in the internals, we would need some more details. Preferably if you could open up a reproduction repository for us to step into and debug, or even better would be a PR with a few tests that reproduce the issue in the JsonApiExample project

@marioGomezDev
Copy link
Author

@bart-degreed
Copy link
Contributor

Hi @marioGomezDev, The repro repository your provided targets JADNC v3, so I upgraded it to .NET Core 3.1 and JADNC 4-beta1. After that, I was able to reproduce your issue using the next request:

POST https://localhost:44308/articles HTTP/1.1

{
  "data": {
    "type": "articles",
    "attributes": {
      "article-prop": "AP-new"
    },
    "relationships": {
      "author": {
        "data": {
          "type": "students",
          "id": "1"
        }
      }
    }
  }
}

This crashes in the deserialization process, because it tries to create an instance of abstract Person class. Instead it should look at the actual type ("students" in this case) and so create an instance of Student. I have a preliminary fix at #828.

For context, the next request shows the sample data:

GET https://localhost:44308/articles?include=author HTTP/1.1
{
  "links": {
    "self": "https://localhost:44308/articles?include=author"
  },
  "data": [
    {
      "type": "articles",
      "id": "1",
      "attributes": {
        "article-prop": "ArticleProp 1"
      },
      "relationships": {
        "author": {
          "links": {
            "self": "https://localhost:44308/articles/1/relationships/author",
            "related": "https://localhost:44308/articles/1/author"
          },
          "data": {
            "type": "students",
            "id": "1"
          }
        }
      },
      "links": {
        "self": "https://localhost:44308/articles/1"
      }
    },
    {
      "type": "articles",
      "id": "2",
      "attributes": {
        "article-prop": "ArticleProp 2"
      },
      "relationships": {
        "author": {
          "links": {
            "self": "https://localhost:44308/articles/2/relationships/author",
            "related": "https://localhost:44308/articles/2/author"
          },
          "data": {
            "type": "teachers",
            "id": "2"
          }
        }
      },
      "links": {
        "self": "https://localhost:44308/articles/2"
      }
    }
  ],
  "included": [
    {
      "type": "students",
      "id": "1",
      "attributes": {
        "student-prop": "StudentProp 1",
        "person-prop": "PersonProp Student 1"
      },
      "links": {
        "self": "https://localhost:44308/students/1"
      }
    },
    {
      "type": "teachers",
      "id": "2",
      "attributes": {
        "teacher-prop": "TeacherProp 2",
        "person-prop": "PersonProp Teacher 1"
      },
      "links": {
        "self": "https://localhost:44308/teachers/2"
      }
    }
  ]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3 participants