Skip to content

SubClass annotations are missing from the base class #2449

Closed

Description

@yaelah

In order for jackson ObjectMapper to deserialize correctly a base class to its subclasses, we need to include annotations defining all expected subclasses in the parent class.
The codegen currently does not generate those annotations.
For this example YAML:

definitions:
  Base:
    type: object
    discriminator: disc
    properties:
      disc:
        type: string
    required:
      - disc
  SubClass:
    allOf:
      - $ref: '#/definitions/Base'
        required:
          - field
        properties:
          field:
            type: string

Jackson expects these annotations to be produced, but they are not:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "disc")
@JsonSubTypes({ @type(value = SubClass.class, name = "SubClass") })
public class Base {
}

Activity

  1. added this to the Future milestone on Mar 28, 2016
  2. wing328 commented on Mar 28, 2016

    @wing328
    Contributor
  3. wing328 commented on Mar 28, 2016

    @wing328
    Contributor

    Similar ticket: #1253

  4. yaelah commented on Mar 29, 2016

    @yaelah
    Author

    I have a fix for this. What is the process for submitting patches? I have never submitted anything to this project before.
    thanks.

  5. added a commit that references this issue on Mar 29, 2016
  6. wing328 commented on Mar 30, 2016

    @wing328
    Contributor
  7. added 2 commits that reference this issue on Mar 30, 2016
  8. yaelah commented on Mar 30, 2016

    @yaelah
    Author

    Thanks for noticing my wrong email.

  9. added 3 commits that reference this issue on Apr 9, 2016
  10. 13 remaining items

  11. szakrewsky commented on Oct 27, 2016

    @szakrewsky
    Contributor

    I modified @yaelah post to work on the latest master branch. I have been using this for a while now, hope it helps. One issue I did notice is that it won't apply to sub-sub classes.

    Update: the sub-sub class issue has now been fixed in my latest pr commit.

  12. added 2 commits that reference this issue on Nov 1, 2016
  13. wing328 commented on Nov 11, 2016

    @wing328
    Contributor

    @szakrewsky I added a comment to the PR #4085. Please take a look when you've time.

  14. added a commit that references this issue on Nov 15, 2016
  15. jeff9finger commented on Nov 16, 2016

    @jeff9finger

    +1

  16. jeff9finger commented on Nov 16, 2016

    @jeff9finger

    How is the discriminator value determined for the name property of the@JsonSubTypes.@Type annotation. It looks like the best that can be done without more information is to use the class name.

    But if a discriminator property value for a sub class is not the name of the class, but another string, how can that be determined? This may need to be addressed in a new issue, but simply wanted to raise it here to see if anyone has ideas.

    My idea is to add a codegen vendor extension that can be used to state the discriminator value in a subclass - maybe x-java-discriminator-value

    definitions:
      Base:
        type: object
        discriminator: disc
        properties:
          disc:
            type: string
        required:
          - disc
      SubClass:
        x-java-discriminator-value: sub-class
        allOf:
          - $ref: '#/definitions/Base'
            required:
              - field
            properties:
              field:
                type: string 
    
  17. szakrewsky commented on Nov 17, 2016

    @szakrewsky
    Contributor

    @jeff9finger I have seen a similar issue. The spec (http://swagger.io/specification/) for the discriminator field says

    When used, the value MUST be the name of this schema or any schema that inherits it.

    And since the class name is the schema name its all good.

  18. added a commit that references this issue on Nov 18, 2016
  19. jeff9finger commented on Nov 18, 2016

    @jeff9finger

    Using the classname is a good default (in fact, it looks to be the only thing that makes sense). But in some cases, the discriminator value needs to be something other than the class name.

    I have used a vendor extension "x-discriminator-value" on each subclass. This value contains the value from the enumeration defined for that subclass. Changed the template to handle that value if it exists (otherwise, use name as before). This works perfectly for my. I'd like to see it in the master branch so I don't have to have my own templates.... Anyone else having this issue?

  20. added a commit that references this issue on Nov 21, 2016
  21. boldtrn commented on Jan 23, 2017

    @boldtrn

    Isn't this fixed by #4085?

  22. wing328 commented on Feb 16, 2017

    @wing328
    Contributor

    Isn't this fixed by #4085?

    I think so.

  23. added a commit that references this issue on May 11, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions