Skip to content

allArgConstructor for java#18405

Closed
jpfinne wants to merge 6 commits into
OpenAPITools:masterfrom
jpfinne:featAddJavaAllArgsConstructor
Closed

allArgConstructor for java#18405
jpfinne wants to merge 6 commits into
OpenAPITools:masterfrom
jpfinne:featAddJavaAllArgsConstructor

Conversation

@jpfinne

@jpfinne jpfinne commented Apr 16, 2024

Copy link
Copy Markdown
Contributor

All Argument constructor for java client and spring

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 correct branch: master (upcoming 7.5.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.

@jpfinne jpfinne requested a review from jimschubert as a code owner April 16, 2024 21:08
run: |
bash bin/generate-samples.sh
# when a sample is deleted, you have to generate it twice for all files to get created
bash bin/generate-samples.sh

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this PR includes lots of changes not belonging to your original PR

can you please submit a new one with just the change for just "allArgConstruct for java"?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

sorry. I screwed up the merges.

Is it better now?

jpfinne added 2 commits April 23, 2024 11:12
…onstructor

# Conflicts:
#	modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java
#	modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/ScalaCaskServerCodegen.java
#	modules/openapi-generator/src/main/resources/scala-cask/build.sbt.mustache
#	modules/openapi-generator/src/main/resources/scala-cask/build.sc.mustache
#	modules/openapi-generator/src/main/resources/scala-cask/project/plugins.sbt
#	samples/client/echo_api/java/okhttp-gson-user-defined-templates/docs/PetApiDocumentation.md
#	samples/client/echo_api/java/okhttp-gson-user-defined-templates/docs/StoreApiDocumentation.md
#	samples/client/echo_api/java/okhttp-gson-user-defined-templates/docs/UserApiDocumentation.md
#	samples/client/echo_api/java/okhttp-gson-user-defined-templates/src/main/java/org/openapitools/client/api/PetApi.java
#	samples/client/echo_api/java/okhttp-gson-user-defined-templates/src/main/java/org/openapitools/client/api/StoreApi.java
#	samples/client/echo_api/java/okhttp-gson-user-defined-templates/src/main/java/org/openapitools/client/api/UserApi.java
#	samples/client/echo_api/java/okhttp-gson-user-defined-templates/src/test/java/org/openapitools/client/api/PetApiTest.java
#	samples/client/echo_api/java/okhttp-gson-user-defined-templates/src/test/java/org/openapitools/client/api/StoreApiTest.java
#	samples/client/echo_api/java/okhttp-gson-user-defined-templates/src/test/java/org/openapitools/client/api/UserApiTest.java
#	samples/client/petstore/java/microprofile-rest-client-3.0-jackson-with-xml/src/main/java/org/openapitools/client/RFC3339DateFormat.java
#	samples/client/petstore/java/microprofile-rest-client-3.0-jackson/src/main/java/org/openapitools/client/RFC3339DateFormat.java
#	samples/documentation/html2/.openapi-generator/VERSION
#	samples/server/petstore/scala-cask/.openapi-generator/VERSION
#	samples/server/petstore/scala-cask/build.sbt
#	samples/server/petstore/scala-cask/build.sc
#	samples/server/petstore/scala-cask/project/plugins.sbt
if (cm.vars.size() + cm.parentVars.size() != cm.allVars.size()) {
once(LOGGER).warn("Unexpected allVars for {} expecting:{} vars. actual:{} vars", cm.name, cm.vars.size() + cm.parentVars.size(), cm.allVars.size());
}
cm.vendorExtensions.put("allArgsConstructorVars", constructorArgs);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

for extensions, please add x- as the prefix and use dash case

e.g. x-java-all-args-constructor-vars

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

// vendorExtensions.allArgsConstructorVars should be equivalent to allVars
// but it is not reliable if openapiNormalizer.REFACTOR_ALLOF_WITH_PROPERTIES_ONLY is disabled
if (cm.vars.size() + cm.parentVars.size() != cm.allVars.size()) {
once(LOGGER).warn("Unexpected allVars for {} expecting:{} vars. actual:{} vars", cm.name, cm.vars.size() + cm.parentVars.size(), cm.allVars.size());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

not sure if we need this warning as vars+ parentVars may not be equal to allVars when vars and parentVars have overlayed properties (e.g. both child and parent model has a property named id defined)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Do you have a sample? I don't reach line 789 when I generate all arg constructors using allOfDuplicatedProperties.yaml or allOfMappingDuplicatedProperties.yaml

Even when I add x-parent: true, allVars are still ok (but the code is uncompilable because the overriden getter)

I reach it from SpringCodegenTest.generateAllArgsConstructor() for Cat.
It might be something similar to #18340

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

if (cm.vars.size() + cm.parentVars.size() != cm.allVars.size()) {

let's step back for a moment. did you encounter such condition before? if yes, can you please share the spec?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

You can find more evidences in #18340

So the merge of parentVars and allVars is needed to ensure that the constructors are correctly generated

modelsAttrs.getImports().add(toAdd);
}
}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

fyi above code block refactored into abstract java codegen.

@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)

@wing328

wing328 commented Apr 30, 2024

Copy link
Copy Markdown
Member

fyi. i've filed #18538 with resolved merge conflicts and minor change (removing EOL in pojo)

@wing328

wing328 commented Apr 30, 2024

Copy link
Copy Markdown
Member

cc
@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)

@wing328

wing328 commented May 1, 2024

Copy link
Copy Markdown
Member

closed via #18538

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