Skip to content
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

[Spring] OneOf Polymorphism support for spring boot #5596

Closed
wants to merge 2 commits into from

Conversation

alexsuperdev
Copy link
Contributor

@alexsuperdev alexsuperdev commented Mar 15, 2020

Fix 5381
Implemented OneOf Polymorphism for java spring library.

PR checklist

  • Read the contribution guidelines.
  • If contributing template-only or documentation-only changes which will change sample output, build the project before.
  • Run the shell script(s) under ./bin/ (or Windows batch scripts under.\bin\windows) to update Petstore samples related to your fix. This is important, as CI jobs will verify all generator outputs of your HEAD commit, and these must match the expectations made by your contribution. You only need to run ./bin/{LANG}-petstore.sh, ./bin/openapi3/{LANG}-petstore.sh if updating the code or mustache templates for a language ({LANG}) (e.g. php, ruby, python, etc).
  • File the PR against the correct branch: master, 4.3.x, 5.0.x. Default: master.
  • Copy the technical committee to review the pull request if your PR is targeting a particular programming language.

@bbdouglas (2017/07) @sreeshas (2017/08) @jfiala (2017/08) @lukoyanov (2017/09) @cbornet (2017/09) @jeff9finger (2018/01) @karismann (2019/03) @Zomzog (2019/04) @lwlee2608 (2019/10) @bkabrda (2020/01)

copy methods from OpenAPITools#5120 (oneOf support for jackson clients) to implement same in spring
generate oneOf Class that has all properties from inherited classes
fix property name of inherited model for oneOf fix imports for oneOf
create oneOf stuff only if useOneOfInterfaces is setted
fixed format
Copy link
Contributor

@bkabrda bkabrda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey 👋 IIUC a lot of code added is a copy-paste from the old version of the oneOf implementation that I wrote for JavaClientCodegen. Since then, most of the functionality was moved to DefaultCodegen, so you really only need to make sure the overloading methods properly call their parent's implementation (specifically postProcessOpenAPI and postProcessAllModels). Then it should be matter of setting useOneOfInterfaces = true and perhaps overriding the addImportsToOneOfInterface method (see how this is done in JavaClientCodegen).

I hope this makes sense.

@@ -579,6 +588,251 @@ public void preprocessOpenAPI(OpenAPI openAPI) {
}
}
}

if (useOneOfInterfaces && openAPI.getComponents() != null){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this method calls super.preprocessOpenAPI, I don't think you should need to include this block at all. Is there a specific reason why you added it here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added it because SpringCodegenTest.testAdditionalPropertiesPutForConfigValues and
SpringCodegenTest.testInitialConfigValues fails with NPE otherway.

}
}

public void addOneOfNameExtension(Schema s, String name) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAICS, this is the same as DefaultCodegen.addOneOfNameExtension and so I think it can be safely omitted.

}
}

public void addOneOfInterfaceModel(ComposedSchema cs, String name) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above - this is already implemented in DefaultCodegen.addOneOfInterfaceModel, so let's omit it here (unless there's a specific reason to override it).

return null;
}

private class OneOfImplementorAdditionalData {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above, I don't think this is really necessary here.

@alexsuperdev
Copy link
Contributor Author

alexsuperdev commented Mar 16, 2020

Hey 👋 IIUC a lot of code added is a copy-paste from the old version of the oneOf implementation that I wrote for JavaClientCodegen. Since then, most of the functionality was moved to DefaultCodegen, so you really only need to make sure the overloading methods properly call their parent's implementation (specifically postProcessOpenAPI and postProcessAllModels). Then it should be matter of setting useOneOfInterfaces = true and perhaps overriding the addImportsToOneOfInterface method (see how this is done in JavaClientCodegen).

I hope this makes sense.

Hey @bkabrda! Thank you for your codereview of my pullrequest.
You are totally right, I copy-paste your code from JavaClientCodegen.
I founded your commits in JavaClientCodegen and didn't know that you implemented that already in DefaultCodegen class, from where i can extend this stuff in SpringCodegen class.
Do you think it will be okay if i cancel this pullrequest and make a new one to master, where i will reuse your methods ?

And i have one question to your implementation.
How your implementation works without overriding fromRequestBody?
Without this one i had import of UNKNOWN_BASE_TYPE in the generated OneOf Model and
UNKNOWN_BASE_TYPE as Type Parameter in API class

@bkabrda
Copy link
Contributor

bkabrda commented Mar 17, 2020

Do you think it will be okay if i cancel this pullrequest and make a new one to master, where i will reuse your methods ?

Sure, I think that's perfectly fine.

How your implementation works without overriding fromRequestBody?
Without this one i had import of UNKNOWN_BASE_TYPE in the generated OneOf Model and
UNKNOWN_BASE_TYPE as Type Parameter in API class

TBH I don't have a good answer. I can definitely take a closer look when time permits, but I have tons of work right now and can't promise anything.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants