Skip to content

[Java] Replace if with ? in Generated toIndentedString-methods#23209

Open
Chrimle wants to merge 22 commits intoOpenAPITools:masterfrom
Chrimle:toIndentedString-refactor
Open

[Java] Replace if with ? in Generated toIndentedString-methods#23209
Chrimle wants to merge 22 commits intoOpenAPITools:masterfrom
Chrimle:toIndentedString-refactor

Conversation

@Chrimle
Copy link
Contributor

@Chrimle Chrimle commented Mar 11, 2026

Refactors all generated Java classes' toIndentedString-method to internally use Ternary Operator for null-checking, instead of a conventional if-statement.
NOTE: This change should make the method smaller, but will be equivalent in terms of functionality and performance.

Example:

Before:
private String toIndentedString(Object o) {
    if (o == null) {
        return "null";
    }
    return o.toString().replace("\n", "\n    ");
}
After:
private String toIndentedString(Object o) {
    return o == null ? "null" : o.toString().replace("\n", "\n    ");
}

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 || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in WSL)
    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.x.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR solves a reported issue, reference it using GitHub's linking syntax (e.g., having "fixes #123" present in the PR description)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

Summary by cubic

Refactor toIndentedString across all Java templates to use a single-line ternary null check and regenerate samples across Java generators. Behavior is unchanged ("null" for null; toString() with newline indentation otherwise).

Written for commit 2909915. Summary will update on new commits.

Chrimle added 16 commits March 12, 2026 00:01
Signed-off-by: Chrimle <28791817+Chrimle@users.noreply.github.com>
Signed-off-by: Chrimle <28791817+Chrimle@users.noreply.github.com>
Signed-off-by: Chrimle <28791817+Chrimle@users.noreply.github.com>
Signed-off-by: Chrimle <28791817+Chrimle@users.noreply.github.com>
Signed-off-by: Chrimle <28791817+Chrimle@users.noreply.github.com>
Signed-off-by: Chrimle <28791817+Chrimle@users.noreply.github.com>
Signed-off-by: Chrimle <28791817+Chrimle@users.noreply.github.com>
Signed-off-by: Chrimle <28791817+Chrimle@users.noreply.github.com>
Signed-off-by: Chrimle <28791817+Chrimle@users.noreply.github.com>
Signed-off-by: Chrimle <28791817+Chrimle@users.noreply.github.com>
Signed-off-by: Chrimle <28791817+Chrimle@users.noreply.github.com>
Signed-off-by: Chrimle <28791817+Chrimle@users.noreply.github.com>
Signed-off-by: Chrimle <28791817+Chrimle@users.noreply.github.com>
Signed-off-by: Chrimle <28791817+Chrimle@users.noreply.github.com>
Signed-off-by: Chrimle <28791817+Chrimle@users.noreply.github.com>
Signed-off-by: Chrimle <28791817+Chrimle@users.noreply.github.com>
@Chrimle
Copy link
Contributor Author

Chrimle commented Mar 11, 2026

Only ran j* samples so far, will update tomorrow 👍

@Chrimle
Copy link
Contributor Author

Chrimle commented Mar 13, 2026

On second thought, wouldn't it just be simpler with ?: syntax instead?

…String`-methods

Signed-off-by: Chrimle <28791817+Chrimle@users.noreply.github.com>
@Chrimle
Copy link
Contributor Author

Chrimle commented Mar 13, 2026

To be completely honest, I don't know if these methods should even be kept at all... that's a lot of boilerplate just to make the output prettier...

Chrimle added 2 commits March 13, 2026 18:42
Signed-off-by: Chrimle <28791817+Chrimle@users.noreply.github.com>
Signed-off-by: Chrimle <28791817+Chrimle@users.noreply.github.com>
@Chrimle Chrimle marked this pull request as ready for review March 13, 2026 17:52
@Chrimle Chrimle changed the title [Java] Refactor toIndentedString-method to use String::valueOf [Java] Generate toIndentedString-methods to Use Ternary Operators Instead of ifs Mar 13, 2026
@Chrimle Chrimle changed the title [Java] Generate toIndentedString-methods to Use Ternary Operators Instead of ifs [Java] Replace if with ? in Generated toIndentedString-methods Mar 13, 2026
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

No issues found across 3000 files

Note: This PR contains a large number of files. cubic only reviews up to 75 files per PR, so some files may not have been reviewed.

Chrimle added 3 commits March 14, 2026 00:26
Signed-off-by: Chrimle <28791817+Chrimle@users.noreply.github.com>
Signed-off-by: Chrimle <28791817+Chrimle@users.noreply.github.com>
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.

1 participant