Skip to content

[dart-dio][json_serializable] fix unknownEnumValue for collection-typed enum properties - #24673

Open
marcelpinto wants to merge 2 commits into
OpenAPITools:masterfrom
marcelpinto:fix/dart-dio-collection-unknown-enum-value
Open

[dart-dio][json_serializable] fix unknownEnumValue for collection-typed enum properties#24673
marcelpinto wants to merge 2 commits into
OpenAPITools:masterfrom
marcelpinto:fix/dart-dio-collection-unknown-enum-value

Conversation

@marcelpinto

@marcelpinto marcelpinto commented Aug 10, 2026

Copy link
Copy Markdown

enumUnknownDefaultCase is unusable with serializationLibrary=json_serializable whenever a spec contains a collection of enums. The template emits the fallback from the property's datatypeWithEnum, guarded on the property's isEnumOrRef. 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:

unknownEnumValue: List<EnumArraysArrayEnumEnum>.unknownDefaultOpenApi,

dart analyze on the current sample:

error - enum_arrays.dart:46:21 - The class 'List' doesn't have a constructor
        named 'unknownDefaultOpenApi'. - class_instantiation_access_to_member
error - enum_arrays.dart:46:21 - Arguments of a constant creation must be
        constant expressions. - const_with_non_constant_argument

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 from bin/configs/dart-dio-petstore-client-lib-fake-json_serializable.yaml, which already sets enumUnknownDefaultCase: "true".

2. List of $ref'd enums → no fallback at all. isEnumOrRef is false on the property, so nothing is emitted and those fields still throw ArgumentError: `x` is not one of the supported values on an unknown value — the exact failure enumUnknownDefaultCase exists 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 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 — confirmed in the generated .g.dart:

availableMethods: (v as List<dynamic>)
    .map((e) => $enumDecode(_$RefundMethodEnumMap, e,
          unknownValue: RefundMethod.unknownDefaultOpenApi))
    .toList(),

uniqueItems: true properties (Set<…>) were affected identically and are fixed by the same branch.

Fixes #22160

PR checklist

  • Read the contribution guidelines.
  • Ran ./mvnw clean package, ./bin/generate-samples.sh, ./bin/utils/export_docs_generators.sh and committed all changed files. Sample diff is 3 lines across 3 files; export_docs_generators.sh produces no diff, as this is a template-only change that adds no option.
  • @mention the technical committee: @yissachar @joernahrens @swipesight @jaumard

Note on verification: I confirmed the fix with dart analyze rather than a full build_runner run — the json_serializable sample fails build_runner with 46 FormatterExceptions on my machine both before and after this change, a pre-existing SDK / dart_style mismatch unrelated to it.


Summary by cubic

Fixes enumUnknownDefaultCase in dart-dio with json_serializable for collection-typed enums. Generates valid Dart, applies per-element fallbacks for arrays/sets, and omits unsupported maps and nested collections.

  • Bug Fixes

Written for commit df5ced5. Summary will update on new commits.

Review in cubic

…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

@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.

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.
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.

[BUG] [DART-DIO] generator does not correctly handle unknownEnumValue if it is of type List

1 participant