-
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] Subclass with attribute allOf issue #2096
Comments
Yes, this is an issue in the composition aspect of the codegen. The generator doesn't really know if it's extending a base class or if it should be implementing an interface. For example: messageEvent:
allOf:
- $ref: '#/definitions/ModelA'
- $ref: '#/definitions/ModelB' could in fact be an messageEvent:
allOf:
- $ref: '#/definitions/ModelA'
- $ref: '#/definitions/ModelB'
- $ref: '#/definitions/ModelC' The generator isn't capable of knowing that it cannot extend more than one class and therefore must use interfaces. I think the right thing to do is to first decide how we can detect these situations. I think it's entirely fair to use vendor extensions to hint to the codegen that "all should be interfaces", or ModelA is a class. Next, fixing the issue is actually quite simple. There is a mechanism inside |
@fehguy Is there any way to iterate over all the models of my swagger spec in the postProcessModels ? This method is called for each model and only with 1 model in the 'models' object. |
@hiveship it looks like it's not available. If you'd like to send a PR we can review and merge it. |
I was wrong, my fix was not correct. I don't tink I would be able to do something if I can't access all the models at the same time in the |
@hiveship I'll give it a try tomorrow to see if I can give you a method for processing all models. |
Yes, It's exactly what I need :) |
Merged into master. Please give it a try by pulling the latest master. |
I'm on something else now, I'll do it as fast as possible :) If it's OK for me, I'll send a PR to fix that in Java, JAXRS (jersey) & JAXRS-CXF codegen :) |
I think there is 2 points: Plus, if we a model A extends model B and also duplicate all the model A attributes, then we can get Java issues at runtime (calling setters method would modify model B attribute, but if we cast it at model A, the commons properties won't have the good values). The current behaviour is always extending the parent of the allOf attribute. I think the first step could be to remove all the duplicated properties like I suggested in my previous mail ? And in a second time, when we'll have a good way to decide if we want or not extends the parent, then implement it. Also, if you have an idea to indicate that we want to generate an abstract class, I can implement it (Java). |
I really think this has to be a vendor extension. There is literally no way to know if we want an interface, an abstract class, a base class, etc. I do think that with the code that @wing328 added--the ability to iterate over all models, we can take the extensions into account and remove variables as needed. If you need help on this, please make a test case with a sample spec and let's work together on it. |
@fehguy Ok, so I don't really understand what I first have to do. Having duplicated properties between a parent and a subclass can lead to Java runtime issues. This behaviour can change if we add some vendor extension (let's do it in a special method that can be override to use interface, abstract class, etc.) |
Note : duplicate properties between model and its parent causes errors with gson : |
Any news on this ? I still can't use allOf because of the failures with Gson ... |
@lukashaertel it works if you import only one model but not if you import several models |
A lot of issues are related to this one (#1120, #1348, #1050, ...). So I will try to sum up and give my opinion:
So IMO, the only thing to do is to remove the parent/child relationship when using allOf without discriminator and all should work well. The only case where we should use class inheritance is when one (and no more than one or else we should throw an error) of the imported models has a discriminator field. |
Actually we should set |
@cbornet thanks for the summary. I'll review and share my view this weekend... (sorry very busy these days) |
Thanks @cbornet for your description. It seems like that approach may work. Any more thoughts from the community on how to implement this? This is a major issue for my project. |
If you agree with this, you can support swagger-api/swagger-parser#246 |
If a composed model (allOf) doesn't have any parent and one of its interface has a discriminator field, then set this interface as parent. See swagger-api#2096 See swagger-api/swagger-parser#246
If a composed model (allOf) doesn't have any parent and one of its interface has a discriminator field, then set this interface as parent. See swagger-api#2096 See swagger-api/swagger-parser#246
If a composed model (allOf) doesn't have any parent and one of its interface has a discriminator field, then set this interface as parent. See #2096 See swagger-api/swagger-parser#246
i'm facing a strange behaviour with Swagger Codegen while generating Java code (JAXRS / JAXRS-CXF and Java).
Considering the Swagger definitions:
I exepect to get a class "Event":
and a subclass "MessageEvent" like this:
But with the current Swagger codegen, I got:
This behaviour can cause issue. MessageEvent is a subclass of Event, so in my code I can do:
And then I will got 'null' !
I can help to solve this issue but I need some help to point me where I need to do changes.
What we want is "don't put the parent attributes in a sub class".
In the .mustache template file I can found:
So with that, we can know if the class 'classname' has a parent or not. We now need something to get the list of all attributes of the parent. And then, don't generate them.
The text was updated successfully, but these errors were encountered: