Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Ensures a JsonCreator annotated constructor is present if there are required fields, ensure the getters and setters are properly annotated for jaxrs-spec generator. #19578

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

kevinferrare
Copy link

Fixes #19577

image

image

Actual change is modules/openapi-generator/src/main/resources/JavaJaxRS/spec/pojo.mustache

Other files diff is the result of updating the samples

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.6.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.

@chameleon82 @wing328 @bbdouglas @sreeshas @jfiala @lukoyanov @cbornet @jeff9finger @karismann @Zomzog @lwlee2608 @martin-mfg @cachescrubber @welshm @MelleD @borsch

@kevinferrare kevinferrare changed the title Ensures a JsonCreator annotated constructor is present if there are required fields, and ensure the getters and setters are properly annotated with required=true / required=false for jaxrs-spec generator. Ensures a JsonCreator annotated constructor is present if there are required fields, ensure the getters and setters are properly annotated for jaxrs-spec generator. Sep 13, 2024
@wing328 wing328 modified the milestones: 7.8.0, 7.9.0 Sep 19, 2024
@@ -71,12 +93,12 @@ public class {{classname}} {{#parent}}extends {{{.}}}{{/parent}} {{#vendorExtens
{{#vendorExtensions.x-extra-annotation}}{{{vendorExtensions.x-extra-annotation}}}{{/vendorExtensions.x-extra-annotation}}{{#useSwaggerAnnotations}}
@ApiModelProperty({{#example}}example = "{{{.}}}", {{/example}}{{#required}}required = {{required}}, {{/required}}value = "{{{description}}}"){{/useSwaggerAnnotations}}{{#useMicroProfileOpenAPIAnnotations}}
@org.eclipse.microprofile.openapi.annotations.media.Schema({{#example}}example = "{{{.}}}", {{/example}}{{#required}}required = {{required}}, {{/required}}description = "{{{description}}}"){{/useMicroProfileOpenAPIAnnotations}}
@JsonProperty("{{baseName}}")
@JsonProperty({{#required}}required = {{required}}, {{/required}}value = "{{baseName}}")
Copy link
Contributor

Choose a reason for hiding this comment

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

Moving the close of the required check could eliminate adding value in case where it is redundant.

Suggested change
@JsonProperty({{#required}}required = {{required}}, {{/required}}value = "{{baseName}}")
@JsonProperty({{#required}}required = {{required}}, value = {{/required}}"{{baseName}}")

Copy link
Contributor

Choose a reason for hiding this comment

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

Would also significantly shrink the changeset of this PR 😄

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for the review!
Doing as you suggested is indeed better, diff is "only" 64 files modified now 😃
(Had to change a bit more than your suggestion to get rid of an unwanted line break)

Copy link
Author

@kevinferrare kevinferrare Sep 20, 2024

Choose a reason for hiding this comment

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

Something is not right, in case #required but no #generateBuilders, we wouldn't get an empty constructor and this could break some existing code.
I updated the PR to always generate an empty constructor without any condition, so diff is less small 🥲

@kevinferrare kevinferrare force-pushed the jaxrs-spec-required-field branch 2 times, most recently from a1b84a5 to e1898e0 Compare September 20, 2024 18:06
…equired fields, and ensure the getters and setters are properly annotated with required=true / required=false for jaxrs-spec generator.
{{/additionalProperties}}
{{/generateBuilders}}
public {{classname}}() {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not familiar with the JavaJaxRS code, but should this have a ^hasRequired around it? Or are both constructors valid?

Copy link
Author

@kevinferrare kevinferrare Sep 23, 2024

Choose a reason for hiding this comment

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

We need a default constructor for cases where there is a constructor with arguments in order not to break existing code.
This can happen with hasRequired (new case) or with generateBuilders (existing case), but as far as I know it is not possible to do a OR in mustache.
So I took the simple route and put the default constructor unconditionally, thinking that if in the future a new case with another constructor with arguments arises, it will not be forgotten.

See my comment here #19578 (comment)

@kevinferrare
Copy link
Author

I think I answered all the remarks, is there something else to do on my side?

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] Generated code does not fail when required fields not present (generator jaxrs-spec, library jackson)
4 participants