feat: Add oneOf for scala-http4s client#22969
feat: Add oneOf for scala-http4s client#22969bartosz822 wants to merge 2 commits intoOpenAPITools:masterfrom
Conversation
Implement oneOf schema support for scala-http4s client generator using sealed traits with inlined members. Falls back to regular traits for edge cases (nested oneOf, mixed members). - Add postProcessAllModels to detect and mark oneOf models - Update model.mustache for sealed/regular trait generation - Support all discriminator modes (none, implicit, mapping) - Fix Scala 3 syntax (wildcard imports with *) - Handle shared members and import management - Add test and regenerate samples Common oneOf schemas generate sealed traits for exhaustive pattern matching. Edge cases use regular traits and emit warnings. All generated code compiles.
There was a problem hiding this comment.
2 issues found across 13 files
Prompt for AI agents (all issues)
Check if these issues are valid — if so, understand the root cause of each and fix them.
<file name="modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/ScalaHttp4sClientCodegen.java">
<violation number="1" location="modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/ScalaHttp4sClientCodegen.java:393">
P2: Nested oneOf models can be incorrectly inlined as case classes because the inlining condition doesn’t exclude child models that are oneOf containers, causing nested oneOf traits to be dropped from output and generating incorrect models.</violation>
</file>
<file name="modules/openapi-generator/src/main/resources/scala-http4s/model.mustache">
<violation number="1" location="modules/openapi-generator/src/main/resources/scala-http4s/model.mustache:47">
P2: Discriminator handling without mapping uses generated class names rather than schema names, which can break decoding/encoding when schema names differ from class names.</violation>
</file>
Since this is your first cubic review, here's how it works:
- cubic automatically reviews your code and comments on bugs and improvements
- Teach cubic by replying to its comments. cubic learns from your replies and gets better over time
- Add one-off context when rerunning by tagging
@cubic-dev-aiwith guidance or docs links (includingllms.txt) - Ask questions if you need clarification on any suggestion
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
| Set<String> additionalImports = new HashSet<>(); | ||
| for (String childName : cModel.oneOf) { | ||
| CodegenModel childModel = allModels.get(childName); | ||
| if (childModel != null && oneOfMemberCount.getOrDefault(childName, 0) == 1) { |
There was a problem hiding this comment.
P2: Nested oneOf models can be incorrectly inlined as case classes because the inlining condition doesn’t exclude child models that are oneOf containers, causing nested oneOf traits to be dropped from output and generating incorrect models.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/ScalaHttp4sClientCodegen.java, line 393:
<comment>Nested oneOf models can be incorrectly inlined as case classes because the inlining condition doesn’t exclude child models that are oneOf containers, causing nested oneOf traits to be dropped from output and generating incorrect models.</comment>
<file context>
@@ -354,6 +357,162 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
+ Set<String> additionalImports = new HashSet<>();
+ for (String childName : cModel.oneOf) {
+ CodegenModel childModel = allModels.get(childName);
+ if (childModel != null && oneOfMemberCount.getOrDefault(childName, 0) == 1) {
+ // Mark for inlining (only used by this one parent)
+ childModel.getVendorExtensions().put("x-isOneOfMember", true);
</file context>
| if (childModel != null && oneOfMemberCount.getOrDefault(childName, 0) == 1) { | |
| if (childModel != null | |
| + && (childModel.oneOf == null || childModel.oneOf.isEmpty()) | |
| + && oneOfMemberCount.getOrDefault(childName, 0) == 1) { |
| // with discriminator, no mapping | ||
| given encoder{{classname}}: Encoder[{{classname}}] = Encoder.instance { | ||
| {{#oneOf}} | ||
| case obj: {{.}} => obj.asJson.mapObject(("{{discriminator.propertyName}}" -> "{{.}}".asJson) +: _) |
There was a problem hiding this comment.
P2: Discriminator handling without mapping uses generated class names rather than schema names, which can break decoding/encoding when schema names differ from class names.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At modules/openapi-generator/src/main/resources/scala-http4s/model.mustache, line 47:
<comment>Discriminator handling without mapping uses generated class names rather than schema names, which can break decoding/encoding when schema names differ from class names.</comment>
<file context>
@@ -16,6 +17,173 @@ import {{import}}
+// with discriminator, no mapping
+ given encoder{{classname}}: Encoder[{{classname}}] = Encoder.instance {
+{{#oneOf}}
+ case obj: {{.}} => obj.asJson.mapObject(("{{discriminator.propertyName}}" -> "{{.}}".asJson) +: _)
+{{/oneOf}}
+ }
</file context>
Add detailed assertions for sealed traits, discriminators, Scala 3 syntax, edge cases, and verify inlined members are not in separate files
|
@bartosz822 thanks for the PR is the implementation same as the one in #22916 authored by @plaflamme ? |
Implement oneOf schema support for scala-http4s client generator using sealed traits with inlined members. Falls back to regular traits for edge cases (nested oneOf, mixed members).
Common oneOf schemas generate sealed traits for exhaustive pattern matching. Edge cases use regular traits and emit warnings. All generated code compiles.
PR checklist
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.
master(upcoming7.x.0minor release - breaking changes with fallbacks),8.0.x(breaking changes without fallbacks)"fixes #123"present in the PR description)Summary by cubic
Add oneOf support to the scala-http4s client generator. Generates sealed traits with inlined members, handles all discriminator modes, and falls back to regular traits for edge cases.
New Features
Bug Fixes
Written for commit 00184fd. Summary will update on new commits.