Skip to content

[BUG][DART] fix Optional<T> fromJson wrapping#23811

Open
timonrieger wants to merge 4 commits into
OpenAPITools:masterfrom
timonrieger:fix/add-x-is-optional-checks
Open

[BUG][DART] fix Optional<T> fromJson wrapping#23811
timonrieger wants to merge 4 commits into
OpenAPITools:masterfrom
timonrieger:fix/add-x-is-optional-checks

Conversation

@timonrieger
Copy link
Copy Markdown
Contributor

@timonrieger timonrieger commented May 18, 2026

Summary

When useOptional=true (added in #22257) is enabled, the x-is-optional vendor extension correctly wraps scalar fields in Optional.present() / Optional.absent() during fromJson deserialization. However, six collection branches and the numeric branch in native_class.mustache are missing this check entirely, causing generated fromJson methods to return raw List<T>, Map<K,V>, or call the non-existent Optional<num?>.parse() constructor where Optional<T> is expected.

This produces compile errors in any Dart project using useOptional=true with optional array, map, or numeric fields.

Scalar fields (non-collection, non-numeric) already had correct x-is-optional handling at lines 290–304 of the template. This PR extends the same pattern to the remaining branches.

Relation to #23365

This PR conflicts with #23365 on two lines inside the {{^vendorExtensions.x-is-optional}} branch of the nested-array hunk (the listFromJson(json[r'...']) and .cast<T>() fixes). #23365 should be merged first, then those two conflict lines become a no-op resolution. Thus I stacked this PR on top of #23365. The two PRs are otherwise independent and fix non-overlapping issues.

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.

Copy link
Copy Markdown
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.

1 issue found across 15 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

@timonrieger timonrieger changed the title [dart] fix Optional<T> fromJson wrapping for collection and numeric fields [BUG][DART] fix Optional<T> fromJson wrapping for collection and numeric fields May 18, 2026
@timonrieger
Copy link
Copy Markdown
Contributor Author

cc @jaumard (2018/09) @josh-burton (2019/12) @amondnet (2019/12) @sbu-WBT (2020/12) @kuhnroyal (2020/12) @agilob (2020/12) @ahmednfwela (2021/08)

@timonrieger timonrieger changed the title [BUG][DART] fix Optional<T> fromJson wrapping for collection and numeric fields [BUG][DART] fix Optional<T> fromJson wrapping May 18, 2026
@wing328
Copy link
Copy Markdown
Member

wing328 commented May 18, 2026

thanks for the PR

can you please review the build failure when you've time? https://github.com/OpenAPITools/openapi-generator/actions/runs/26029167650/job/76510300895?pr=23811

Copy link
Copy Markdown
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 16 files

Re-trigger cubic

@timonrieger
Copy link
Copy Markdown
Contributor Author

@wing328 addressed both the review comment and the build failure. Just a notice: #23365 should be merged beforehand ideally (as per PR description)

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