From e6daa44cacee4c6f755ca0ca42a4a80b55ede16a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20Pint=C3=B3=20Biescas?= Date: Mon, 10 Aug 2026 21:45:25 +0200 Subject: [PATCH 1/2] fix(dart-dio): use element type for unknownEnumValue on collection properties 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.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 #22160 --- .../serialization/json_serializable/class.mustache | 11 +++++++++-- .../lib/src/model/enum_arrays.dart | 2 +- .../src/model/object_with_duplicate_inline_enum.dart | 2 +- .../lib/src/model/object_with_inline_enum.dart | 2 +- 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/dart/libraries/dio/serialization/json_serializable/class.mustache b/modules/openapi-generator/src/main/resources/dart/libraries/dio/serialization/json_serializable/class.mustache index 73e37f9c373e..c4323078c2f2 100644 --- a/modules/openapi-generator/src/main/resources/dart/libraries/dio/serialization/json_serializable/class.mustache +++ b/modules/openapi-generator/src/main/resources/dart/libraries/dio/serialization/json_serializable/class.mustache @@ -59,11 +59,18 @@ class {{{classname}}} { required: {{#required}}true{{/required}}{{^required}}false{{/required}}, includeIfNull: {{#required}}{{#isNullable}}true{{/isNullable}}{{^isNullable}}false{{/isNullable}}{{/required}}{{^required}}false{{/required}},{{^required}}{{#useOptional}} readValue: readOptionalValue,{{/useOptional}}{{/required}} - {{#isEnumOrRef}} {{#enumUnknownDefaultCase}} + {{#isArray}} + {{#items.isEnumOrRef}} + unknownEnumValue: {{{items.datatypeWithEnum}}}.unknownDefaultOpenApi, + {{/items.isEnumOrRef}} + {{/isArray}} + {{^isArray}} + {{#isEnumOrRef}} unknownEnumValue: {{{datatypeWithEnum}}}.unknownDefaultOpenApi, - {{/enumUnknownDefaultCase}} {{/isEnumOrRef}} + {{/isArray}} + {{/enumUnknownDefaultCase}} ) {{/isBinary}} {{#isBinary}} diff --git a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/model/enum_arrays.dart b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/model/enum_arrays.dart index 70551bb1d4e6..26eea637873b 100644 --- a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/model/enum_arrays.dart +++ b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/model/enum_arrays.dart @@ -43,7 +43,7 @@ class EnumArrays { name: r'array_enum', required: false, includeIfNull: false, - unknownEnumValue: List.unknownDefaultOpenApi, + unknownEnumValue: EnumArraysArrayEnumEnum.unknownDefaultOpenApi, ) diff --git a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/model/object_with_duplicate_inline_enum.dart b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/model/object_with_duplicate_inline_enum.dart index 6b4bfc6b4b59..74c7e7bb302b 100644 --- a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/model/object_with_duplicate_inline_enum.dart +++ b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/model/object_with_duplicate_inline_enum.dart @@ -29,7 +29,7 @@ class ObjectWithDuplicateInlineEnum { name: r'attribute', required: false, includeIfNull: false, - unknownEnumValue: Set.unknownDefaultOpenApi, + unknownEnumValue: ObjectWithDuplicateInlineEnumAttributeEnum.unknownDefaultOpenApi, ) diff --git a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/model/object_with_inline_enum.dart b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/model/object_with_inline_enum.dart index 70d144a01c15..554a0f6add67 100644 --- a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/model/object_with_inline_enum.dart +++ b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/model/object_with_inline_enum.dart @@ -29,7 +29,7 @@ class ObjectWithInlineEnum { name: r'attribute', required: false, includeIfNull: false, - unknownEnumValue: Set.unknownDefaultOpenApi, + unknownEnumValue: ObjectWithInlineEnumAttributeEnum.unknownDefaultOpenApi, ) From df5ced50c0dd32ea766e32ffc6260710c42bf59e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20Pint=C3=B3=20Biescas?= Date: Tue, 11 Aug 2026 09:17:36 +0200 Subject: [PATCH 2/2] fix(dart-dio): drop unknownEnumValue on map properties 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.unknownDefaultOpenApi` for these -- invalid Dart on top of being unsupported. samples/.../map_test.dart carried one. It also only unwraps one level: for List> it expects an unknownEnumValue of type List, 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. --- .../dio/serialization/json_serializable/class.mustache | 2 ++ .../lib/src/model/map_test.dart | 1 - 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/modules/openapi-generator/src/main/resources/dart/libraries/dio/serialization/json_serializable/class.mustache b/modules/openapi-generator/src/main/resources/dart/libraries/dio/serialization/json_serializable/class.mustache index c4323078c2f2..1be71f5fbddb 100644 --- a/modules/openapi-generator/src/main/resources/dart/libraries/dio/serialization/json_serializable/class.mustache +++ b/modules/openapi-generator/src/main/resources/dart/libraries/dio/serialization/json_serializable/class.mustache @@ -66,9 +66,11 @@ class {{{classname}}} { {{/items.isEnumOrRef}} {{/isArray}} {{^isArray}} + {{^isMap}} {{#isEnumOrRef}} unknownEnumValue: {{{datatypeWithEnum}}}.unknownDefaultOpenApi, {{/isEnumOrRef}} + {{/isMap}} {{/isArray}} {{/enumUnknownDefaultCase}} ) diff --git a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/model/map_test.dart b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/model/map_test.dart index 7549af37b88b..0ae910b5e9fd 100644 --- a/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/model/map_test.dart +++ b/samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/model/map_test.dart @@ -46,7 +46,6 @@ class MapTest { name: r'map_of_enum_string', required: false, includeIfNull: false, - unknownEnumValue: Map.unknownDefaultOpenApi, )