You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
SubClass annotations are missing from the base class #2449
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:
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 {
}
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.
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
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?
Activity
wing328 commentedon Mar 28, 2016
@yaelah thanks for the detailed above. Would you have cycle to contribute a fix?
Here is a good starting point: https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/resources/Java/model.mustache
wing328 commentedon Mar 28, 2016
Similar ticket: #1253
yaelah commentedon Mar 29, 2016
I have a fix for this. What is the process for submitting patches? I have never submitted anything to this project before.
thanks.
Fix issue swagger-api#2449: SubClass annotations are missing from the…
wing328 commentedon Mar 30, 2016
Please refer to https://github.com/swagger-api/swagger-codegen/blob/master/CONTRIBUTING.md for contributing guidelines.
If you need help, just reply to let us know.
Revert "Fix issue swagger-api#2449: SubClass annotations are missing …
Fix issue swagger-api#2449: SubClass annotations are missing from the…
yaelah commentedon Mar 30, 2016
Thanks for noticing my wrong email.
Fix issue swagger-api#2449: SubClass annotations are missing from the…
Revert "Fix issue swagger-api#2449: SubClass annotations are missing …
Fix issue swagger-api#2449: SubClass annotations are missing from the…
13 remaining items
szakrewsky commentedon Oct 27, 2016
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.
Issue swagger-api#2449 SubClass annotations are missing from the base…
Issue swagger-api#2449 SubClass annotations are missing from the base…
wing328 commentedon Nov 11, 2016
@szakrewsky I added a comment to the PR #4085. Please take a look when you've time.
Issue swagger-api#2449 SubClass annotations are missing from the base…
jeff9finger commentedon Nov 16, 2016
+1
jeff9finger commentedon Nov 16, 2016
How is the discriminator value determined for the name property of the
@JsonSubTypes.@Typeannotation. 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-valueszakrewsky commentedon Nov 17, 2016
@jeff9finger I have seen a similar issue. The spec (http://swagger.io/specification/) for the discriminator field says
And since the class name is the schema name its all good.
Issue swagger-api#2449 SubClass annotations are missing from the base…
jeff9finger commentedon Nov 18, 2016
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?
Issue #2449 SubClass annotations are missing from the base class (#4085)
boldtrn commentedon Jan 23, 2017
Isn't this fixed by #4085?
wing328 commentedon Feb 16, 2017
I think so.
Issue swagger-api#2449 SubClass annotations are missing from the base…