feat: add support for oneOf in sttp4 client#22916
feat: add support for oneOf in sttp4 client#22916plaflamme wants to merge 20 commits intoOpenAPITools:masterfrom
oneOf in sttp4 client#22916Conversation
There was a problem hiding this comment.
4 issues found across 8 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/resources/scala-sttp4/api.mustache">
<violation number="1" location="modules/openapi-generator/src/main/resources/scala-sttp4/api.mustache:38">
P2: Request bodies are now always JSON‑serialized with `asJson(...)`, which breaks operations that consume non‑JSON media types by sending JSON instead of the raw body.</violation>
</file>
<file name="modules/openapi-generator/src/main/resources/scala-sttp4/model.mustache">
<violation number="1" location="modules/openapi-generator/src/main/resources/scala-sttp4/model.mustache:52">
P2: json4s oneOf deserialization aborts on first mismatch because `Extraction.extract` throws, so later oneOf types are never tried.</violation>
<violation number="2" location="modules/openapi-generator/src/main/resources/scala-sttp4/model.mustache:74">
P2: Discriminator handling hardcodes the Scala class name as the wire value, ignoring OpenAPI discriminator mappings, so specs with mapped discriminator values will fail to deserialize/serialize correctly.</violation>
<violation number="3" location="modules/openapi-generator/src/main/resources/scala-sttp4/model.mustache:172">
P2: Inline oneOf member case classes are generated inside the parent model file, but the parent model’s import list isn’t augmented with the child models’ imports. If a oneOf member uses a type not referenced by the parent, the inline case class will compile without the necessary import.</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
- 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.
| {{/vendorExtensions.x-isEnum}} | ||
| {{#vendorExtensions.x-isRegularModel}} | ||
| {{^isEnum}} | ||
| case class {{classname}}( |
There was a problem hiding this comment.
P2: Inline oneOf member case classes are generated inside the parent model file, but the parent model’s import list isn’t augmented with the child models’ imports. If a oneOf member uses a type not referenced by the parent, the inline case class will compile without the necessary import.
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-sttp4/model.mustache, line 172:
<comment>Inline oneOf member case classes are generated inside the parent model file, but the parent model’s import list isn’t augmented with the child models’ imports. If a oneOf member uses a type not referenced by the parent, the inline case class will compile without the necessary import.</comment>
<file context>
@@ -15,46 +20,218 @@ import {{import}}
+{{/vendorExtensions.x-isEnum}}
+{{#vendorExtensions.x-isRegularModel}}
+{{^isEnum}}
+case class {{classname}}(
+ {{#vars}}
+ {{#description}}
</file context>
1ec344d to
c56b433
Compare
|
thanks for the PR cc @clasnake (2017/07), @shijinkui (2018/01), @ramzimaalej (2018/03), @chameleon82 (2020/03), @Bouillie (2020/04) @Fish86 (2023/06) |
|
https://github.com/OpenAPITools/openapi-generator/actions/runs/21774387501/job/62832048821?pr=22916 please follow step 3 to update the samples |
c56b433 to
4437a9e
Compare
There was a problem hiding this comment.
1 issue found across 3 files (changes from recent commits).
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/resources/scala-sttp4/model.mustache">
<violation number="1" location="modules/openapi-generator/src/main/resources/scala-sttp4/model.mustache:107">
P2: `transformConstructorNames` is defined as a partial match over only `x-oneOfMembers`. Because `x-oneOfMembers` excludes shared oneOf subtypes, the function is not total and will throw `MatchError` when encoding/decoding those subtypes. Add a default case (identity) so unmapped constructor names don’t crash serialization.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
There was a problem hiding this comment.
1 issue found across 1 file (changes from recent commits).
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/resources/scala-sttp4/model.mustache">
<violation number="1" location="modules/openapi-generator/src/main/resources/scala-sttp4/model.mustache:110">
P2: Wildcard `case other` is emitted inside the oneOfMembers loop, so the first wildcard matches everything and makes subsequent member cases unreachable; only the first oneOf member can be encoded/decoded.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
modules/openapi-generator/src/main/resources/scala-sttp4/model.mustache
Outdated
Show resolved
Hide resolved
d05761d to
7aba995
Compare
On large APIs this can dramatically speed up compilation.
|
https://github.com/OpenAPITools/openapi-generator/actions/runs/21850815146/job/63223036192?pr=22916 please replace tabs with 4-space in the java file. |
|
@wing328 should be all good now |
|
please review the build failure when you've time. let us know if you need any help |
|
@wing328 I think this is good to go now? |
# Conflicts: # modules/openapi-generator/src/test/java/org/openapitools/codegen/scala/Sttp4CodegenTest.java
…f Enumeration-based EnumNameSerializer The json4s variant of JsonSupport.scala used EnumNameSerializer which expects Scala Enumeration types, but sttp4 model templates generate sealed traits with case objects for enums. This caused compilation errors since the types are incompatible. Replace EnumNameSerializer with direct references to the implicit Serializer objects already defined in each enum's companion object.
|
@wing328 updated this on top of the latest master branch, anything else needed to get this in? |
…ropagate parent props When a schema has both its own properties and a oneOf composition, the sttp4 generator previously had three issues: 1. Empty oneOf children (no type, no properties) were silently dropped because DefaultCodegen's unaliasSchema treated them as type aliases, replacing the model name with the language type (e.g. io.circe.Json). 2. Parent-owned properties (e.g. cancellation_datetime) were lost entirely - they didn't appear on the sealed trait or any child. 3. Sibling properties from other oneOf children could contaminate each child's vars list via DefaultCodegen's property merging. This fix: - Resolves aliased oneOf children by looking at the original OpenAPI schema's $refs to restore correct model names - Identifies parent-owned properties and propagates them to each child model's allVars so they appear in generated case classes - Renders parent properties as abstract def members on the sealed trait for compile-time enforcement - Adds a test covering the CancellationInfo pattern (parent with properties + oneOf with empty and non-empty children)
|
will try to run some tests tomorrow and merge if no questions/feedback from anyone. |
Address #19891 for sttp4 clients.
@Fish86
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
Adds oneOf support to the Scala sttp4 client, aligns JSON/request/response handling with sttp4, and updates samples, docs, and dependencies. Also fixes oneOf parents with own properties by propagating parent fields to children and resolving aliased children; adds tests. Fixes #19891.
New Features
scala-sttp4-circesample + CI; sttp client 4.0.15; Scala 2.13.18; addcirce-genericandcirce-generic-extras; semiauto derivation; docs updated (circeVersion/circeExtrasVersion).Migration
Written for commit ac1b84e. Summary will update on new commits.