-
Notifications
You must be signed in to change notification settings - Fork 6k
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
[Java] Fix SubClass annotations missing from the base class #4085
Conversation
Thanks for the PR but your commit (as shown in the Commits tab) is not linked to your Github account, which means this PR won't count as your contribution in https://github.com/swagger-api/swagger-codegen/graphs/contributors. Let me know if you need help fixing it. |
Please also run |
@wing328 I did run |
@szakrewsky please commit the update Java Petstore files as well so that CIs can test the change. |
@wing328 Thanks. I think I found some issues, this commit only effects jersey but it seems that the other frameworks are adding the annotations as well. I will fix first. |
7bd38fd
to
2e61550
Compare
@wing328 I believe this is ready now. Let me know if there is anything more I need to do. |
@szakrewsky I'll review and let you know if I've any questions. |
|
||
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "className" ) | ||
@JsonSubTypes({ | ||
@JsonSubTypes.Type(value = Dog.class, name = "Dog"),@JsonSubTypes.Type(value = Cat.class, name = "Cat"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@szakrewsky Can we use 2-space instead of 2 tabs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
2e61550
to
a0bd0ee
Compare
The PR looks to only place the annotations on the first level of inheritance. For example:
In this case, the Jackson annotations are not placed on the SubObj class |
Adding the following to DefaultCodeGen seems to fix the issue for me. However, this assumes that allOf: can have no more than one inline element that contains a discriminator. (My experience also shows that allOf: understands no more than 2 inline elements. If more than to exist, only last one is recognized).
|
I'm interested in this same feature but for generated JAX-RS server code as well as Java clients. It looks like that works by applying the same changes to the templates in the JavaJaxRS resource directory. Would it be possible to add that here or should I submit a separate PR after this is merged? |
911cb9f
to
1b47ca7
Compare
@jeff9finger I just fixed one issue which I had already documented before, #2449 (comment) In your example the following code would have been generated, @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "object_type")
@JsonSubTypes({ @Type(value = SubObj.class, name = "SubObj") })
public class BaseObj {
} now you will get @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "object_type")
@JsonSubTypes({ @Type(value = SubObj.class, name = "SubObj"), @Type(value = DailySubObj.class, name = "DailySubObj")})
public class BaseObj {
} The annotations will be added as long as the discriminator value is set, why that only happens for ModelImpl and not ComposedModel I don't know, but I would probably say that is a different issue. |
The generated code above is not correct. It might work, but does not represent the actual structure of the inheritance. I need the annotations to be on the SubObj class. Like so:
Notice that there is a different discriminator on SubObj - "sub_obj_type". This is completely lost unless the "discriminator" is only available on ModelImpl, so until the swagger spec is updated to handle discriminator in more than one object, it has to live there. That is why the discriminator field needs to be populated for any object that has it declared in the swagger spec. Without the code above, or something similar, any sub objects that declare a discriminator, will not have the discriminator set in the ModelImpl object in the code that represents the swagger definition. And this is why the @willgorman You should be able to simply add the mustache templates to the other Java based templates to get these annotations. I'll need them eventually in a jaxrs server and in inflector. |
@szakrewosky (I'm new to git) How can I merge your code into my local master repository? I don't see a branch that the commits for this issue are in. Thanks |
@jeff9finger the PR has been merged into master. Please pull the latest to give it a try. @szakrewsky thanks for your contribution. |
The implementation of the sub-sub class does not work in my case. See my comment above: #4085 (comment) In my data model (and probably other people's as well), this approach is wrong. I don't want to have all the I think my comment referenced explains this. It seems to me that making an assumption about sub-sub (and deeper) classes must have more options on the approach than simply assuming that all the sub-sub classes will be serialized as one type of object. |
@jeff9finger As far as I know, if Jackson is deserializing BaseObj and you want it to instantiate the proper subclass to have working polymorphism, "My" solution is correct. DailySubObj is indeed a subclass of BaseObj even if it is a few levels down in the hierarchy. If you also want Jackson to deserialize the SubObj with proper polymorphism, then "Your" solution is also correct, however at the current time I think it depends on the separate issue of the discriminator not being set #4226. Fix that, and you will get what you want, if I understand your problem correctly. |
@szakrewsky Thanks for your comments. Let me amend my statement from above. So, I agree with you, that the solution is correct, but with a caveat. In my case, I have the annotation regarding The annotation for the sub-sub class is not desired because in some cases (mine to be specific) there are many sub-sub classes and I prefer that they not pollute the base class when they are not strictly required. I am suggesting that there be an option to specifying the |
@szakrewsky What does the spec look like when there are sub sub classes in your case? Doesn't there always need to be a discriminator for inheritance? Just wondering why the cm is always added to the parent.parent.children. The parent would need to have a discriminator defined right? In other words, how can a sub sub class be defined in a swagger wprc without the sub class specifying a discriminator? (The only way I can think that it would make sense is if the sub class defined the same discriminator property as the base class. |
@jeff9finger The spec that exhibits this problem is the same as your spec just without the discriminator on the SubObj. Now what does this mean? It could mean that DailySubObj is actually composed of SubObj and therefore SubObj doesn't exist as a class at all and there is only one level of inheritance. However, it appears that swagger-code-gen will generate a class for SubObj that extends BaseObj, and a class for DailySubObj that extends SubObj. I did a little more research and it appears that the JsonSubTypes annotation is recursive. So you could put the JsonTypeInfo annotation at the top level of the inheritance hierarchy, and have the JsonSubTypes annotation only include direct subclasses. The JsonSubType annotation would then be included recursively down the inheritance tree for subclasses that themselves have subclasses. Would this solve your problem? What exactly is your problem by the way? Is it just a preference over annotation placement or do you get a stacktrace while deserializing something? |
The open api spec specifies that Here is the spec that shows SubObj without a
You state "... it appears that swagger-code-gen will generate a class for SubObj that extends BaseObj, and a class for DailySubObj that extends SubObj." This indeed appears to be the case. However, this is not the behavior that I am specifying in my original spec. I've included it below for clarity.
The annotations on SubObj require a different Notice that the So, if all the annotations for DailySubObj are placed on BaseObj (using the While Jackson handles the annotations recursively, doing so in this case does not accurately represent the semantics of the open api spec. See issue #4226 for more detail. My suggestion there is to only add the children of an object to the parent.parent.children if there is no discriminator. |
@jeff9finger this PR was merged into master back on Nov 21, 2016 via the commit 7696559 |
OK. I have another change required to handle the issues discussed here. Will open a pr for that. Is a new issue created? |
… class (swagger-api#4085) * petstore up to latest * Issue swagger-api#2449 SubClass annotations are missing from the base class * include child in all its super types
PR checklist
./bin/
to update Petstore sample so that CIs can verify the change. (For instance, only need to run./bin/{LANG}-petstore.sh
and./bin/security/{LANG}-petstore.sh
if updating the {LANG} (e.g. php, ruby, python, etc) code generator or {LANG} client's mustache templates)2.3.0
branch for breaking (non-backward compatible) changes.Description of the PR
#2449