[feat][scala-sttp] Add oneOf and allOf discriminator support with sealed traits for circe generator#23510
Conversation
|
cc: @wing328 |
There was a problem hiding this comment.
3 issues found across 21 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="modules/openapi-generator/src/main/resources/scala-sttp/model.mustache">
<violation number="1" location="modules/openapi-generator/src/main/resources/scala-sttp/model.mustache:67">
P2: Regular-trait encoders are non-exhaustive; unknown implementations will throw MatchError during encoding.</violation>
</file>
<file name="samples/client/petstore/scala-sttp-circe/src/main/scala/org/openapitools/client/model/Animal.scala">
<violation number="1" location="samples/client/petstore/scala-sttp-circe/src/main/scala/org/openapitools/client/model/Animal.scala:45">
P2: Subtype codecs use unconstrained `className` strings while the polymorphic codec expects canonical discriminator constants, causing inconsistent serialization and potential wrong subtype decoding.</violation>
</file>
<file name="modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/ScalaSttpClientCodegen.java">
<violation number="1" location="modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/ScalaSttpClientCodegen.java:363">
P2: Inlining decision only counts oneOf parents; a child referenced elsewhere can still be marked inlineable and removed, leading to missing model classes for non-oneOf references.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
...s/client/petstore/scala-sttp-circe/src/main/scala/org/openapitools/client/model/Animal.scala
Outdated
Show resolved
Hide resolved
...enapi-generator/src/main/java/org/openapitools/codegen/languages/ScalaSttpClientCodegen.java
Show resolved
Hide resolved
There was a problem hiding this comment.
1 issue found across 1 file (changes from recent commits).
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/ScalaSttpClientCodegen.java">
<violation number="1" location="modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/ScalaSttpClientCodegen.java:316">
P2: Model reference counting uses exact `dataType` matching, missing container-wrapped model references and causing incorrect inlining decisions.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
...enapi-generator/src/main/java/org/openapitools/codegen/languages/ScalaSttpClientCodegen.java
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
2 issues found across 6 files (changes from recent commits).
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="modules/openapi-generator/src/test/java/org/openapitools/codegen/scala/ScalaSttpCirceCodegenTest.java">
<violation number="1" location="modules/openapi-generator/src/test/java/org/openapitools/codegen/scala/ScalaSttpCirceCodegenTest.java:158">
P2: Test assertion does not verify non-sealed trait and will pass for `sealed trait Animal` too.</violation>
</file>
<file name="modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/ScalaSttpClientCodegen.java">
<violation number="1" location="modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/ScalaSttpClientCodegen.java:367">
P1: Shared oneOf child models can be rebound to the last processed parent because parent vendor extensions are overwritten unconditionally.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
| if (child == null) continue; | ||
|
|
||
| // All children extend the parent trait | ||
| child.getVendorExtensions().put("x-oneOfParent", parent.classname); |
There was a problem hiding this comment.
P1: Shared oneOf child models can be rebound to the last processed parent because parent vendor extensions are overwritten unconditionally.
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/ScalaSttpClientCodegen.java, line 367:
<comment>Shared oneOf child models can be rebound to the last processed parent because parent vendor extensions are overwritten unconditionally.</comment>
<file context>
@@ -345,16 +352,26 @@ private void markOneOfTraits(Map<String, ModelsMap> modelsMap,
+ if (child == null) continue;
+
+ // All children extend the parent trait
+ child.getVendorExtensions().put("x-oneOfParent", parent.classname);
+ if (parent.discriminator != null) {
+ child.getVendorExtensions().put("x-parentDiscriminatorName",
</file context>
...penapi-generator/src/test/java/org/openapitools/codegen/scala/ScalaSttpCirceCodegenTest.java
Show resolved
Hide resolved
There was a problem hiding this comment.
1 issue found across 5 files (changes from recent commits).
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="samples/client/petstore/scala-sttp-circe/src/main/scala/org/openapitools/client/model/Animal.scala">
<violation number="1" location="samples/client/petstore/scala-sttp-circe/src/main/scala/org/openapitools/client/model/Animal.scala:18">
P2: `Animal` was made non-sealed, but `encoderAnimal` still matches only `Cat`/`Dog`, leaving a non-exhaustive runtime match for `Animal` values.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
...s/client/petstore/scala-sttp-circe/src/main/scala/org/openapitools/client/model/Animal.scala
Show resolved
Hide resolved
|
#22916 adds oneOf support for scala-sttp4-circe generator. This one is for sttp3 |
@clasnake (2017/07), @shijinkui (2018/01), @ramzimaalej (2018/03), @chameleon82 (2020/03), @Bouillie (2020/04) @Fish86 (2023/06)
Adds discriminator-based polymorphism support to the
scala-sttpgenerator when usingcirceas the JSON library. This enables correct deserialization of polymorphic types.Implementation
Three discriminator patterns are supported:
Animal→Cat/Dogwith mapped valuesFalls back to regular (non-sealed) traits for edge cases like nested oneOf or shared members.
PR checklist
and update samples:
Commit all changed files.
mastermembers, so they are more likely to review the pull request.
Summary by cubic
Adds discriminator-based polymorphism to the
scala-sttpgenerator withcirce, supporting bothoneOfandallOf. Generates sealed traits and discriminator-aware codecs for correct deserialization; updates docs, tests, and samples.New Features
oneOf/allOf; synthesizesoneOffromallOfdiscriminator mappings for uniform handling.oneOf/allOfas supported; config now points toscala-sttp-circe/petstore.yaml. Samples showAnimal(regular trait due toSeq[Dog]),Collar(mapped), andTreat(schema-name dispatch). Tests cover baseName field mapping, allOf/oneOf discriminator flows, and no-inlining when children are reused.Bug Fixes
DecodingFailureerrors; added missing imports.Written for commit 821db67. Summary will update on new commits.