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] fix default value for nullable containers #14959

Merged
merged 9 commits into from
Mar 17, 2023
Merged

Conversation

wing328
Copy link
Member

@wing328 wing328 commented Mar 15, 2023

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/utils/export_docs_generators.sh
    
    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*.
    For Windows users, please run the script in Git BASH.
  • In case you are adding a new generator, run the following additional script :
    ./bin/utils/ensure-up-to-date.sh
    
    Commit all changed files.
  • File the PR against the correct branch: master (6.3.0) (minor release - breaking changes with fallbacks), 7.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.

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

@wing328 wing328 marked this pull request as ready for review March 15, 2023 07:45
if (this.{{name}} == null) {
this.{{name}} = {{{defaultValue}}};
}
{{/defaultValue}}
Copy link
Member

Choose a reason for hiding this comment

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

what if openApiNullable=false & no default value?

Copy link
Member Author

@wing328 wing328 Mar 15, 2023

Choose a reason for hiding this comment

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

good catch, i just spotted the same issue with #14961.

i think we will need to create an empty container (array or set) if it's null.

Copy link
Member Author

Choose a reason for hiding this comment

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

pushed a fix. please have another look when you've time. thanks.

Copy link
Contributor

@welshm welshm left a comment

Choose a reason for hiding this comment

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

Code change looks fine. I think you have unrelated changes here

Just one question about error cases.

@@ -17,7 +17,7 @@
* SpecialModelName
*/

@JsonTypeName("$special[model.name]")
@JsonTypeName("_special_model.name_")
Copy link
Contributor

Choose a reason for hiding this comment

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

I wouldn't expect some of these changes from your mustache changes - bad branch?

Copy link
Member Author

@wing328 wing328 Mar 16, 2023

Choose a reason for hiding this comment

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

in 3.x spec, the schema name must conform to

	-components.schemas.Schema name $special[model.name] doesn't adhere to regular expression ^[a-zA-Z0-9\.\-_]+$

(this PR also migrates test spec used by spring from 2.0 to 3.0)

{{/openApiNullable}}
{{^openApiNullable}}
private {{>nullableDataType}} {{name}} = {{#required}}{{{defaultValue}}}{{/required}}{{^required}}null{{/required}};
Copy link
Contributor

Choose a reason for hiding this comment

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

If the value is removed when required is true - does Jackson or something else validate it is set at some point?

Otherwise, will responses to clients will fail because a required field is missing?

I do realize that required means that a default value shouldn't be used. Just trying to think of how the error scenario is detected.

Copy link
Member Author

Choose a reason for hiding this comment

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

looks like @NotNull should be used instead per https://stackoverflow.com/a/45877203/677735

(I think it's out of scope of this PR which aims to fix the default value)

@wing328 wing328 merged commit d0f7bd1 into master Mar 17, 2023
@wing328 wing328 deleted the fix-spring-default branch March 17, 2023 02:16
@wing328
Copy link
Member Author

wing328 commented Mar 17, 2023

FYI. Filed #14981 to migrate remaining config files related to spring generators to use 3.x spec.

@noshua
Copy link

noshua commented May 13, 2024

Something in the 7.5.0 release toggled the behavior for me. On 7.4.0 all collections where default to null now with 7.5.0 all collections are initialized empty. Since you seem to be familiar with this topic @wing328 any idea what changed in the last release?

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

Successfully merging this pull request may close these issues.

[BUG][Java] Nullable array creates assignments without value
4 participants