fix(dart-dio): use enum type for discriminator when property is enum#24153
fix(dart-dio): use enum type for discriminator when property is enum#24153wing328 wants to merge 3 commits into
Conversation
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
…b.com/Lutra-Fs/openapi-generator into Lutra-Fs-fix/dart-dio-enum-discriminator-21570
…io-enum-discriminator-21570
There was a problem hiding this comment.
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}}'); | ||
| } |
There was a problem hiding this comment.
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'); |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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>
|
@Lutra-Fs please review the feedback from cubic-dev-ai when you've time. |
based on #22612 with resolved merge conflicts
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.
Summary by cubic
Fixes
dart-diobuilt_value discriminator generation when the discriminator is an enum. The generateddiscriminatorValuenow uses the enum type instead ofStringfor type safety and correct enum handling.discriminatorValuegetters inclass_discriminator.mustachewhen the discriminator is an enum; non-enum behavior is unchanged.<Enum>.valueOf('...')for mappings and throwUnsupportedErrorinstead of returningnull.verifyEnumDiscriminatorUsesEnumType) with a new enum-discriminator spec (enum-discriminator.yaml) to enforce the behavior.fruit.dart) and applied a minor template cleanup.Written for commit 67087e5. Summary will update on new commits.