Skip to content

fix(dart-dio): use enum type for discriminator when property is enum#24153

Open
wing328 wants to merge 3 commits into
masterfrom
Lutra-Fs-fix/dart-dio-enum-discriminator-21570
Open

fix(dart-dio): use enum type for discriminator when property is enum#24153
wing328 wants to merge 3 commits into
masterfrom
Lutra-Fs-fix/dart-dio-enum-discriminator-21570

Conversation

@wing328

@wing328 wing328 commented Jun 29, 2026

Copy link
Copy Markdown
Member

based on #22612 with resolved merge conflicts

PR checklist

  • Read the contribution guidelines.
  • 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.
  • 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

Fixes dart-dio built_value discriminator generation when the discriminator is an enum. The generated discriminatorValue now uses the enum type instead of String for type safety and correct enum handling.

  • Bug Fixes
    • Generate enum-typed discriminatorValue getters in class_discriminator.mustache when the discriminator is an enum; non-enum behavior is unchanged.
    • Use <Enum>.valueOf('...') for mappings and throw UnsupportedError instead of returning null.
    • Added a test (verifyEnumDiscriminatorUsesEnumType) with a new enum-discriminator spec (enum-discriminator.yaml) to enforce the behavior.
    • Regenerated the sample (fruit.dart) and applied a minor template cleanup.

Written for commit 67087e5. Summary will update on new commits.

Review in cubic

Lutra-Fs and others added 3 commits January 1, 2026 22:10
When discriminator property references an enum type, use that enum type
instead of String for discriminatorValue getter/extension. This ensures
type safety and proper enum handling in generated Dart code.

Changes:
- Updated class_discriminator.mustache to handle enum discriminators
- Use enum.valueOf() for enum discriminators, throw error instead of returning null
- Added test case for enum discriminator scenario
- regenerated sample client with correct enum discriminator handling

Fixes #21570
@wing328 wing328 marked this pull request as ready for review June 29, 2026 08:55

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

3 issues found across 5 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/dart/libraries/dio/serialization/built_value/class_discriminator.mustache">

<violation number="1" location="modules/openapi-generator/src/main/resources/dart/libraries/dio/serialization/built_value/class_discriminator.mustache:29">
P1: Passing the wire discriminator value (`mappingName`) to built_value `EnumClass.valueOf()` is incorrect because `valueOf` expects the generated Dart constant name, not the wire name. When the wire value differs from the Dart identifier (e.g., `in-progress` vs `inProgress`), this will throw at runtime.</violation>
</file>

<file name="samples/openapi3/client/petstore/dart-dio/oneof_polymorphism_and_inheritance/lib/src/model/fruit.dart">

<violation number="1" location="samples/openapi3/client/petstore/dart-dio/oneof_polymorphism_and_inheritance/lib/src/model/fruit.dart:49">
P2: `discriminatorValue` returns `FruitType` while `discriminatorMapping` remains `Map<String, Type>`, creating a type mismatch between complementary discriminator APIs.</violation>

<violation number="2" location="samples/openapi3/client/petstore/dart-dio/oneof_polymorphism_and_inheritance/lib/src/model/fruit.dart:56">
P1: `discriminatorValue` throws for valid base `Fruit`/`FruitBuilder` wrapper instances instead of falling back to the known `fruitType` field.</violation>
</file>

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

{{#mappedModels}}
if (this is {{modelName}}) {
return {{discriminator.propertyType}}.valueOf(r'{{mappingName}}');
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1: Passing the wire discriminator value (mappingName) to built_value EnumClass.valueOf() is incorrect because valueOf expects the generated Dart constant name, not the wire name. When the wire value differs from the Dart identifier (e.g., in-progress vs inProgress), this will throw at runtime.

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/dart/libraries/dio/serialization/built_value/class_discriminator.mustache, line 29:

<comment>Passing the wire discriminator value (`mappingName`) to built_value `EnumClass.valueOf()` is incorrect because `valueOf` expects the generated Dart constant name, not the wire name. When the wire value differs from the Dart identifier (e.g., `in-progress` vs `inProgress`), this will throw at runtime.</comment>

<file context>
@@ -17,4 +18,27 @@ extension {{classname}}BuilderDiscriminatorExt on {{classname}}Builder {
+        {{#mappedModels}}
+        if (this is {{modelName}}) {
+            return {{discriminator.propertyType}}.valueOf(r'{{mappingName}}');
+        }
+        {{/mappedModels}}
+        throw UnsupportedError('Invalid discriminator value for {{classname}}');
</file context>

return FruitType.valueOf(r'BANANA');
}
return null;
throw UnsupportedError('Invalid discriminator value for Fruit');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1: discriminatorValue throws for valid base Fruit/FruitBuilder wrapper instances instead of falling back to the known fruitType field.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At samples/openapi3/client/petstore/dart-dio/oneof_polymorphism_and_inheritance/lib/src/model/fruit.dart, line 56:

<comment>`discriminatorValue` throws for valid base `Fruit`/`FruitBuilder` wrapper instances instead of falling back to the known `fruitType` field.</comment>

<file context>
@@ -46,25 +46,25 @@ abstract class Fruit implements Built<Fruit, FruitBuilder> {
+            return FruitType.valueOf(r'BANANA');
         }
-        return null;
+        throw UnsupportedError('Invalid discriminator value for Fruit');
     }
 }
</file context>


extension FruitDiscriminatorExt on Fruit {
String? get discriminatorValue {
FruitType get discriminatorValue {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2: discriminatorValue returns FruitType while discriminatorMapping remains Map<String, Type>, creating a type mismatch between complementary discriminator APIs.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At samples/openapi3/client/petstore/dart-dio/oneof_polymorphism_and_inheritance/lib/src/model/fruit.dart, line 49:

<comment>`discriminatorValue` returns `FruitType` while `discriminatorMapping` remains `Map<String, Type>`, creating a type mismatch between complementary discriminator APIs.</comment>

<file context>
@@ -46,25 +46,25 @@ abstract class Fruit implements Built<Fruit, FruitBuilder> {
 
 extension FruitDiscriminatorExt on Fruit {
-    String? get discriminatorValue {
+    FruitType get discriminatorValue {
         if (this is Apple) {
-            return r'APPLE';
</file context>

@wing328 wing328 changed the title Lutra fs fix/dart dio enum discriminator 21570 fix(dart-dio): use enum type for discriminator when property is enum Jun 29, 2026
@wing328

wing328 commented Jun 29, 2026

Copy link
Copy Markdown
Member Author

@Lutra-Fs please review the feedback from cubic-dev-ai when you've time.

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