[dart-dio][json_serializable] fix unknownEnumValue for collection-typed enum properties - #24673
Open
marcelpinto wants to merge 2 commits into
Open
Conversation
…operties The json_serializable class template emitted the unknown-enum fallback from the property's own datatypeWithEnum, guarded on the property's isEnumOrRef. Both are wrong for collection-typed enum properties. For a List/Set of inline enums the property type is the container, so the template produced unknownEnumValue: List<EnumArraysArrayEnumEnum>.unknownDefaultOpenApi, which is not valid Dart -- the analyzer rejects it with "The class 'List' doesn't have a constructor named 'unknownDefaultOpenApi'". Three committed samples contain this today, so enumUnknownDefaultCase is unusable with serializationLibrary=json_serializable whenever a spec has an array of enums. For a List of $ref'd enums, isEnumOrRef is false on the property, so no fallback was emitted at all and those fields keep throwing ArgumentError on an unknown value -- the exact failure enumUnknownDefaultCase exists to prevent. Key the guard and the type off items for collections and off the property otherwise. json_serializable applies unknownValue: per element for iterables of enums, so the element type is the correct target in both cases. Fixes OpenAPITools#22160
Contributor
There was a problem hiding this comment.
All reported issues were addressed across 4 files
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
json_serializable rejects the annotation outright on a Map field: Error with `@JsonKey` on the `mapRef` field. `unknownEnumValue` can only be set on fields of type enum or on Iterable, List, or Set instances of an enum type. so a map of enums must emit nothing at all. The template previously produced `Map<String, XEnum>.unknownDefaultOpenApi` for these -- invalid Dart on top of being unsupported. samples/.../map_test.dart carried one. It also only unwraps one level: for List<List<Enum>> it expects an unknownEnumValue of type List<Enum>, so passing the leaf enum is a hard build error. Nested collections therefore emit nothing as well. Verified against a spec covering scalar/array/set/nested-array/map of both inline and $ref'd enums: build_runner and dart analyze are clean, and unknownValue is threaded per element for the array and set cases.
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.
enumUnknownDefaultCaseis unusable withserializationLibrary=json_serializablewhenever a spec contains a collection of enums. The template emits the fallback from the property'sdatatypeWithEnum, guarded on the property'sisEnumOrRef. Both are wrong for collection-typed properties, in opposite directions.1. List/Set of inline enums → invalid Dart. The property type is the container, so the template produces:
dart analyzeon the current sample:Both errors are gone after this change, and no other diagnostic in the file changes. Three committed samples contain this today —
enum_arrays.dart,object_with_inline_enum.dart,object_with_duplicate_inline_enum.dart— so it reproduces straight frombin/configs/dart-dio-petstore-client-lib-fake-json_serializable.yaml, which already setsenumUnknownDefaultCase: "true".2. List of
$ref'd enums → no fallback at all.isEnumOrRefisfalseon the property, so nothing is emitted and those fields still throwArgumentError: `x` is not one of the supported valueson an unknown value — the exact failureenumUnknownDefaultCaseexists to prevent. No sample covers this shape, so it produces no sample diff; reproduce by pointing an array property at a$ref'd enum.Fix. Key the guard and the type off
itemsfor collections, and off the property otherwise.json_serializableappliesunknownValue:per element for iterables of enums, so the element type is the correct target in both cases — confirmed in the generated.g.dart:uniqueItems: trueproperties (Set<…>) were affected identically and are fixed by the same branch.Fixes #22160
PR checklist
./mvnw clean package,./bin/generate-samples.sh,./bin/utils/export_docs_generators.shand committed all changed files. Sample diff is 3 lines across 3 files;export_docs_generators.shproduces no diff, as this is a template-only change that adds no option.Note on verification: I confirmed the fix with
dart analyzerather than a fullbuild_runnerrun — the json_serializable sample failsbuild_runnerwith 46FormatterExceptions on my machine both before and after this change, a pre-existing SDK /dart_stylemismatch unrelated to it.Summary by cubic
Fixes
enumUnknownDefaultCaseindart-diowithjson_serializablefor collection-typed enums. Generates valid Dart, applies per-element fallbacks for arrays/sets, and omits unsupported maps and nested collections.unknownEnumValueon array/set properties; fall back to property type otherwise.List<...>.unknownDefaultOpenApi/Set<...>.unknownDefaultOpenApiand omitunknownEnumValueon map and nested collection fields.$refenums in arrays so unknown values no longer throw.class.mustache; updated samples accordingly. Fixes [BUG] [DART-DIO] generator does not correctly handle unknownEnumValue if it is of type List #22160.Written for commit df5ced5. Summary will update on new commits.