fix(dart-dio): use enum type for discriminator when property is enum#22612
Open
Lutra-Fs wants to merge 1 commit intoOpenAPITools:masterfrom
Open
fix(dart-dio): use enum type for discriminator when property is enum#22612Lutra-Fs wants to merge 1 commit intoOpenAPITools:masterfrom
Lutra-Fs wants to merge 1 commit intoOpenAPITools:masterfrom
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 OpenAPITools#21570
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes enum discriminator handling in the dart-dio generator by using the actual enum type instead of String for discriminatorValue getters when the discriminator property is an enum type. This is a breaking change that improves type safety.
Key changes:
- Modified discriminator handling to detect enum types and generate appropriate code
- Changed return type from
String?to the enum type when applicable - Replaced null returns with exception throwing for invalid discriminator values in enum cases
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
modules/openapi-generator/src/main/resources/dart/libraries/dio/serialization/built_value/class_discriminator.mustache |
Adds conditional logic to generate enum-specific discriminator extensions with proper type signatures and valueOf() calls |
modules/openapi-generator/src/main/resources/dart/libraries/dio/serialization/built_value/class.mustache |
Minor formatting cleanup removing extraneous blank lines |
modules/openapi-generator/src/test/resources/3_0/dart-dio/enum-discriminator.yaml |
Test specification demonstrating enum discriminator scenario with merchant types |
modules/openapi-generator/src/test/java/org/openapitools/codegen/dart/dio/DartDioClientCodegenTest.java |
Adds test verifying enum discriminators generate correct type signatures and valueOf() calls |
samples/openapi3/client/petstore/dart-dio/oneof_polymorphism_and_inheritance/lib/src/model/fruit.dart |
Regenerated sample showing enum discriminator implementation with FruitType enum |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When discriminator property references an enum type, use that enum type instead of String for
discriminatorValuegetter/extension. This ensures type safety and proper enum handling in generated Dart code.Changes:
class_discriminator.mustacheto handle enum discriminatorsenum.valueOf()for enum discriminators, throw error instead of returning nullFixes #21570
This is a breaking change. When the discriminator property is an enum type,
discriminatorValuereturn type changes fromString?to the actual enum type, and throws an exception instead of returning null. Users who rely on string parsing this return value will need to update their code to handle the new enum type. I am opening to add any config if needed, but the old behaviour is not compilable for resources with discriminator issues, so such this PR's behaviour should be at least set as default.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
Switch discriminatorValue to use the enum type when the discriminator property is an enum in Dart-Dio built_value models. This improves type safety and fixes incorrect string-based handling. Fixes #21570.
Bug Fixes
Migration
Written for commit 931346c. Summary will update on new commits.