Sort by inheritance first and java all arguments constructor#18219
Sort by inheritance first and java all arguments constructor#18219jpfinne wants to merge 31 commits intoOpenAPITools:masterfrom
Conversation
# Conflicts: # modules/openapi-generator-cli/pom.xml # modules/openapi-generator-core/pom.xml # modules/openapi-generator-gradle-plugin/pom.xml # modules/openapi-generator-maven-plugin/pom.xml # modules/openapi-generator-online/pom.xml # modules/openapi-generator/pom.xml # modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java # pom.xml
# Conflicts: # pom.xml
# Conflicts: # modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java
|
please put this on hold before we discuss it this week |
@wing328 Ok. I've created a slack account (jean-paulfinne.slack.com) |
|
can you please join https://join.slack.com/t/openapi-generator/shared_invite/zt-12jxxd7p2-XUeQM~4pzsU9x~eGLQqX2g and PM me ? |
|
|
||
| // Whether to automatically hardcode params that are considered Constants by OpenAPI Spec | ||
| protected boolean autosetConstants = false; | ||
| // protected SortByInheritanceFirstBuilder sortByInheritanceFirstBuilder; |
There was a problem hiding this comment.
Intentionally left commented out?
| } | ||
| } | ||
|
|
||
| if (sortModelPropertiesByInheritanceFirst) { |
There was a problem hiding this comment.
Should these options be exclusive?
There was a problem hiding this comment.
Good question. I'll add a comment to clarify the behaviour.
The current behaviour is the following:
- sort by properties first
- then optionally sort again by required flag (and hopefully keeping the above order for the required properties)
I have a doubt about the validity of the comparator. It should have a secondary order, otherwise there is no guarantee for the final order when there are more than 32 values (See java.util.ComparableTimSort.MIN_MERGE)
if (one.required == another.required) return 0;
Anyway,
In my implementation,
- sort by properties last
- then optionally sort again by required flag (and hopefully keeping the above order for the required properties)
# Conflicts: # modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java # modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java # samples/server/petstore/java-play-framework-api-package-override/public/openapi.json # samples/server/petstore/java-play-framework-async/public/openapi.json # samples/server/petstore/java-play-framework-controller-only/public/openapi.json # samples/server/petstore/java-play-framework-fake-endpoints-with-security/public/openapi.json # samples/server/petstore/java-play-framework-fake-endpoints/public/openapi.json # samples/server/petstore/java-play-framework-no-bean-validation/public/openapi.json # samples/server/petstore/java-play-framework-no-exception-handling/public/openapi.json # samples/server/petstore/java-play-framework-no-interface/public/openapi.json # samples/server/petstore/java-play-framework-no-nullable/public/openapi.json # samples/server/petstore/java-play-framework-no-wrap-calls/public/openapi.json # samples/server/petstore/java-play-framework/public/openapi.json # samples/server/petstore/java-undertow/src/main/java/org/openapitools/handler/PathHandlerInterface.java # samples/server/petstore/java-undertow/src/main/resources/config/openapi.json
|
i wonder if you can file a PR just for the following: we will get that merge first. |
Generally I like the new option but will take some more time to review and test. |
| import java.util.*; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class SortByInheritanceFirstBuilder { |
There was a problem hiding this comment.
would it be possible to add some unit tests for this new class, especially the reorder method?
There was a problem hiding this comment.
There is SpringCodegenTest.testAllArgsConstructor_inheritanceFirst
But you're right. As this class is (nearly) a standalone class, we can unit test it.
I will do it.
There was a problem hiding this comment.
There is SpringCodegenTest.testAllArgsConstructor_inheritanceFirst
I saw that too 👍
for every new class, we want some unit tests to make sure things are good as a starting point before further modifying it down the road.
| */ | ||
| protected void handleGenerateConstructorWithAllArgs(CodegenModel codegenModel) { | ||
| if (this.generatedConstructorWithAllArgs && !codegenModel.vars.isEmpty()) { | ||
| codegenModel.vendorExtensions.put("generatedConstructorWithAllArgs", true); |
There was a problem hiding this comment.
why we need this extension?
shouldn't generatedConstructorWithAllArgs set via additionalProperties be enough?
There was a problem hiding this comment.
why we need this extension?
shouldn't
generatedConstructorWithAllArgsset via additionalProperties be enough?
We don't want two no arg constructors.
(one generated by the default template and one generated by the "all arg" constructor, the arg count being 0)
There was a problem hiding this comment.
what about checking allVars to see if it has zero argument or not?
There was a problem hiding this comment.
Maybe.
I had an issue with allVars and supportsInheritance.
allVars did not contain the parent vars. I don't reproduce it anymore on master.
Is it something that was fixed recently?
I need to investigate more.
There was a problem hiding this comment.
allvars should contain parent vars
don't recall a recent fix.
There was a problem hiding this comment.
allVars was invalid for the contract issue_16797.yaml
See #18340
There was a problem hiding this comment.
replied to that. works for me in the latest master
Fix #17542 by adding the additionalProperty sortModelByInheritanceFirst. This is available for all generators.
For the java generators (spring and client), add the option to generate a constructor for all vars: generatedConstructorWithAllArgs (disabled by default).
It can replace the lombok allArgsConstructor annotation (found only in the spring generator)
This PR implements both features because it makes sense to have a predicatable order for all the arguments in the new constructor.
PR checklist
Commit all changed files.
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example
./bin/generate-samples.sh bin/configs/java*.IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
master(upcoming 7.1.0 minor release - breaking changes with fallbacks),8.0.x(breaking changes without fallbacks)Dear technical committee, please review