Skip to content

feat: Add oneOf for scala-http4s client#22969

Open
bartosz822 wants to merge 2 commits intoOpenAPITools:masterfrom
bartosz822:feat/scala-http4s-oneof-support
Open

feat: Add oneOf for scala-http4s client#22969
bartosz822 wants to merge 2 commits intoOpenAPITools:masterfrom
bartosz822:feat/scala-http4s-oneof-support

Conversation

@bartosz822
Copy link

@bartosz822 bartosz822 commented Feb 13, 2026

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.

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

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

    • Detect oneOf in postProcessAllModels and emit sealed traits; nested/mixed oneOf use regular traits with warnings.
    • Support discriminator modes: none, implicit, and mapping, with Circe encoders/decoders, Cats widen, and DecodingFailure on unknown values.
    • Inline member imports, skip separate files for inlined variants, prevent extending multiple sealed traits, and split X-implements into extends/with.
    • Update template and docs; expand tests to assert sealed trait generation, discriminator handling, Scala 3 syntax, and that inlined members don't generate files.
  • Bug Fixes

    • Fix Scala 3 wildcard import syntax (*) in generated models.

Written for commit 00184fd. Summary will update on new commits.

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.
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.

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-ai with guidance or docs links (including llms.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) {
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot Feb 13, 2026

Choose a reason for hiding this comment

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

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>
Suggested change
if (childModel != null && oneOfMemberCount.getOrDefault(childName, 0) == 1) {
if (childModel != null
+ && (childModel.oneOf == null || childModel.oneOf.isEmpty())
+ && oneOfMemberCount.getOrDefault(childName, 0) == 1) {
Fix with Cubic

// with discriminator, no mapping
given encoder{{classname}}: Encoder[{{classname}}] = Encoder.instance {
{{#oneOf}}
case obj: {{.}} => obj.asJson.mapObject(("{{discriminator.propertyName}}" -> "{{.}}".asJson) +: _)
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot Feb 13, 2026

Choose a reason for hiding this comment

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

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>
Fix with Cubic

Add detailed assertions for sealed traits, discriminators, Scala 3 syntax,
edge cases, and verify inlined members are not in separate files
@wing328
Copy link
Member

wing328 commented Feb 13, 2026

@bartosz822 thanks for the PR

is the implementation same as the one in #22916 authored by @plaflamme ?

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