[kotlin] Set required fields to optional when multiple anyOf are used
Currently, for Kotlin generators, when an object has multiple anyOf with differing required fields, it generates an empty data class which causes a Kotlin compiler error.
This change makes the set of required fields between each anyOf branch optional.
This fixes #18626
PR checklist
- Read the contribution guidelines.
- Run the following to build the project and update samples:
(For Windows users, please run the script in WSL)./mvnw clean package || exit ./bin/generate-samples.sh ./bin/configs/*.yaml || exit ./bin/utils/export_docs_generators.sh || exit
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. - If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.
@karismann @Zomzog @andrewemery @4brunu @yutaka0m @stefankoppier @e5l
Summary by cubic
Fixes #18626, where Kotlin models for anyOf schemas with differing required fields could generate empty data classes and fail compilation. Properties required in only some branches are now optional and nullable, while properties required across all branches stay required; constructor properties preserve schema order. This changes affected model APIs by adding nullable types and default values to branch-specific properties.
- Requiredness is resolved through refs,
allOf, and nestedanyOf/oneOfbranches, and discriminator-sealed parents use the adjusted constructor. - Applies to kotlin-server, ktor2, kotlin-spring, and kotlin-vertx-server templates.