-
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
Add support of model inheritance as a standard java single class extension without requirement to add discriminator (see #6127 ) #7593
Conversation
…nsion. Exclude incorrect references from inheritance.
@cbornet
|
@@ -1394,7 +1394,7 @@ public CodegenModel fromModel(String name, Model model, Map<String, Model> allDe | |||
} | |||
// set first interface with discriminator found as parent | |||
if (parent == null | |||
&& (supportsModelExtension | |||
&& ((supportsModelExtension && (interfaceModel instanceof ModelImpl || interfaceModel instanceof ComposedModel)) |
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.
Added validation of parent model similar to the existing one for discriminator case.
…nsion. Remove tab char from source.
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.
A few questions.
- It appears that this option makes all uses of "allOf" create a sub class. This may not be desirable in all cases. So, if the spec needs to use either composition or the existing use of discriminator, can this option be enabled per definition by use of a vendor extension or some other mechanism?
- How will this work with existing uses of discriminator?
- What happens when more than 2 elements exist in the "allOf" declaration?
- What happens if there are at least 2 inline objects in the "allOf" declaration
First of all: #1. The following definition is single-parent model (object never inherits):
#2. The following definition is multi-parent (two-parent) model:
Probably I do not understand such requirements. But in this case users should not use this option.
For #1 this will work as previously.
One model will be extended. For second model - fields will be inherited. |
I am not too concerned about the multi parent model as it is not a realistic model for Java. But if the behavior is different than before when the supportsModelExtension = true, we need to ensure that it is documented clearly. What I am concerned about is a case where the API design calls for both inheritance* and composition. It appears that when supportsModelExtension = true, there is no option for composition and vise versa. Composition still needs to be allowed when supportsModelExtension = true. I will try to illustrate my point with an example. The specification below has 3 different hierarchies. The one with a discriminator will work the same way as it has always worked. But let's say that I want inheritance for
but I want composition for
If I understand correctly, there is not a way to allow for both of these options when supportsModelExtension=true. I would like to specify which hierarchy to apply the supportsModelExtension option to.
What if there was an option (vendor extension ?) on the parent or on the child that specifies whether to use inheritance or composition? |
If we add a vendor extension, would we than validate it in codegen code, or use in mustache templates? |
For the vendor extension, I think it would need changes to both the codegen code and the mustache templates. |
@jeff9finger |
PR checklist
[+] Read the contribution guidelines.
[+] Ran the shell script under
./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). Windows batch files can be found in.\bin\windows\
.[+] Filed the PR against the correct branch:
master
.[+] Copied the technical committee to review the pull request if your PR is targeting a particular programming language.
@wing328 @bbdouglas @JFCote @sreeshas @jfiala @lukoyanov @cbornet @jeff9finger
Description of the PR
This change adds new codegen cli option supportsModelExtension=true|false which allows generation of class extension without defining discriminator.
This is fully backward compatible and does not break any previous logic.
Implemented on DefaultCodegen level. Allow Java-style single class inheritance for any language which doesn't support mixin.