Skip to content

Sort by inheritance first and java all arguments constructor#18219

Open
jpfinne wants to merge 31 commits intoOpenAPITools:masterfrom
jpfinne:sortByInheritanceFirst
Open

Sort by inheritance first and java all arguments constructor#18219
jpfinne wants to merge 31 commits intoOpenAPITools:masterfrom
jpfinne:sortByInheritanceFirst

Conversation

@jpfinne
Copy link
Contributor

@jpfinne jpfinne commented Mar 24, 2024

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

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package 
    ./bin/generate-samples.sh ./bin/configs/*.yaml
    ./bin/utils/export_docs_generators.sh
    
    (For Windows users, please run the script in Git BASH)
    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.
  • File the PR against the [7.5.0] (https://github.com/OpenAPITools/openapi-generator/wiki/Git-Branches): master (upcoming 7.1.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

Dear technical committee, please review

Java @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) @martin-mfg (2023/08)
Java Spring @cachescrubber (2022/02) @welshm (2022/02) @MelleD (2022/02) @atextor (2022/02) @manedev79 (2022/02) @javisst (2022/02) @borsch (2022/02) @banlevente (2022/02) @Zomzog (2022/09) @martin-mfg (2023/08)

Jean-Paul Finne and others added 26 commits December 13, 2023 10:22
# 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:
#	modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java
@jpfinne jpfinne requested a review from jimschubert as a code owner March 25, 2024 17:45
@wing328
Copy link
Member

wing328 commented Mar 25, 2024

please put this on hold before we discuss it this week

@jpfinne
Copy link
Contributor Author

jpfinne commented Mar 26, 2024

please put this on hold before we discuss it this week

@wing328 Ok. I've created a slack account (jean-paulfinne.slack.com)

@wing328
Copy link
Member

wing328 commented Mar 26, 2024


// Whether to automatically hardcode params that are considered Constants by OpenAPI Spec
protected boolean autosetConstants = false;
// protected SortByInheritanceFirstBuilder sortByInheritanceFirstBuilder;
Copy link
Contributor

Choose a reason for hiding this comment

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

Intentionally left commented out?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, my mistake

}
}

if (sortModelPropertiesByInheritanceFirst) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should these options be exclusive?

Copy link
Contributor Author

@jpfinne jpfinne Mar 27, 2024

Choose a reason for hiding this comment

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

Good question. I'll add a comment to clarify the behaviour.

The current behaviour is the following:

  1. sort by properties first
  2. 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,

  1. sort by properties last
  2. then optionally sort again by required flag (and hopefully keeping the above order for the required properties)

jpfinne added 4 commits April 1, 2024 09:59
# 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
@wing328
Copy link
Member

wing328 commented Apr 5, 2024

i wonder if you can file a PR just for the following:

For the java generators (spring and client), add the option to generate a constructor for all vars: generatedConstructorWithAllArgs (disabled by default).

we will get that merge first.

@wing328
Copy link
Member

wing328 commented Apr 5, 2024

Fix #17542 by adding the additionalProperty sortModelByInheritanceFirst. This is available for all generators.

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 {
Copy link
Member

Choose a reason for hiding this comment

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

would it be possible to add some unit tests for this new class, especially the reorder method?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

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.

Copy link
Member

Choose a reason for hiding this comment

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

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);
Copy link
Member

Choose a reason for hiding this comment

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

why we need this extension?

shouldn't generatedConstructorWithAllArgs set via additionalProperties be enough?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

why we need this extension?

shouldn't generatedConstructorWithAllArgs set 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)

Copy link
Member

Choose a reason for hiding this comment

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

what about checking allVars to see if it has zero argument or not?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

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.

Copy link
Member

Choose a reason for hiding this comment

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

allvars should contain parent vars

don't recall a recent fix.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

allVars was invalid for the contract issue_16797.yaml

See #18340

Copy link
Member

Choose a reason for hiding this comment

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

replied to that. works for me in the latest master

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.

[BUG][JAVA] Java generator puts parent properties from allOf last instead of first and skips properties entirely for "middle" references

3 participants