Skip to content

Commit 8df1ddd

Browse files
committed
[BUG][DART] Fix nested-map ! suffix to apply to whole nullable expression
Cubic review on OpenAPITools#23671 caught that the prior shape json[X] == null ? null : (json[X] as Map).map(...).cast<...>()! applied the trailing ! only to the else branch, so a required non-nullable Map<String, Map<...>> field could still be assigned null from the ? branch. Restructure to a single nullable expression that matches the existing mapCastOfType pattern in the same file: (json[X] as Map?)?.map((k, v) => MapEntry(k as String, (v as Map).cast<String, T>())) {{#required}}{{^isNullable}}!{{/isNullable}}{{/required}} {{^required}}{{#defaultValue}} ?? <default>{{/defaultValue}}{{/required}} Now ! correctly applies to the whole nullable expression, ?? feeds the default consistently with the other map branches, and the optional case collapses to a single nullable expression instead of a ternary.
1 parent b33ba0c commit 8df1ddd

5 files changed

Lines changed: 5 additions & 19 deletions

File tree

modules/openapi-generator/src/main/resources/dart2/serialization/native/native_class.mustache

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,7 @@ class {{{classname}}} {
228228
{{{name}}}: {{items.complexType}}.mapFromJson(json[r'{{{baseName}}}']),
229229
{{/items.complexType}}
230230
{{^items.complexType}}
231-
{{{name}}}: json[r'{{{baseName}}}'] == null
232-
? {{#defaultValue}}{{{.}}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}
233-
: (json[r'{{{baseName}}}'] as Map).map((k, v) => MapEntry(k as String, (v as Map).cast<String, {{items.items.dataType}}>())){{#required}}{{^isNullable}}!{{/isNullable}}{{/required}},
231+
{{{name}}}: (json[r'{{{baseName}}}'] as Map?)?.map((k, v) => MapEntry(k as String, (v as Map).cast<String, {{items.items.dataType}}>())){{#required}}{{^isNullable}}!{{/isNullable}}{{/required}}{{^required}}{{#defaultValue}} ?? {{{.}}}{{/defaultValue}}{{/required}},
234232
{{/items.complexType}}
235233
{{/items.isMap}}
236234
{{^items.isMap}}

samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/.openapi-generator/FILES

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,3 @@ lib/model/test_enum.dart
147147
lib/model/test_inline_freeform_additional_properties_request.dart
148148
lib/model/user.dart
149149
pubspec.yaml
150-
test/pet_empty_metadata_test.dart
151-
test/pet_reaction_response_test.dart
152-
test/pet_reaction_status_test.dart
153-
test/pet_reactions_response_test.dart

samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/lib/model/additional_properties_class.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,7 @@ class AdditionalPropertiesClass {
6666

6767
return AdditionalPropertiesClass(
6868
mapProperty: mapCastOfType<String, String>(json, r'map_property') ?? const {},
69-
mapOfMapProperty: json[r'map_of_map_property'] == null
70-
? const {}
71-
: (json[r'map_of_map_property'] as Map).map((k, v) => MapEntry(k as String, (v as Map).cast<String, String>())),
69+
mapOfMapProperty: (json[r'map_of_map_property'] as Map?)?.map((k, v) => MapEntry(k as String, (v as Map).cast<String, String>())) ?? const {},
7270
mapOfArrayInteger: json[r'map_of_array_integer'] == null
7371
? const {}
7472
: (json[r'map_of_array_integer'] as Map<String, dynamic>).map((k, v) => MapEntry(k, v == null ? const <int>[] : (v as List).cast<int>())),

samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/lib/model/map_test.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,7 @@ class MapTest {
7171
}());
7272

7373
return MapTest(
74-
mapMapOfString: json[r'map_map_of_string'] == null
75-
? const {}
76-
: (json[r'map_map_of_string'] as Map).map((k, v) => MapEntry(k as String, (v as Map).cast<String, String>())),
74+
mapMapOfString: (json[r'map_map_of_string'] as Map?)?.map((k, v) => MapEntry(k as String, (v as Map).cast<String, String>())) ?? const {},
7775
mapOfEnumString: mapCastOfType<String, String>(json, r'map_of_enum_string') ?? const {},
7876
directMap: mapCastOfType<String, bool>(json, r'direct_map') ?? const {},
7977
indirectMap: mapCastOfType<String, bool>(json, r'indirect_map') ?? const {},

samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/lib/model/pet_reactions_response.dart

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,8 @@ class PetReactionsResponse {
5959
}());
6060

6161
return PetReactionsResponse(
62-
myReacts: json[r'myReacts'] == null
63-
? const {}
64-
: (json[r'myReacts'] as Map).map((k, v) => MapEntry(k as String, (v as Map).cast<String, bool>())),
65-
reactionCounts: json[r'reactionCounts'] == null
66-
? const {}
67-
: (json[r'reactionCounts'] as Map).map((k, v) => MapEntry(k as String, (v as Map).cast<String, int>())),
62+
myReacts: (json[r'myReacts'] as Map?)?.map((k, v) => MapEntry(k as String, (v as Map).cast<String, bool>())) ?? const {},
63+
reactionCounts: (json[r'reactionCounts'] as Map?)?.map((k, v) => MapEntry(k as String, (v as Map).cast<String, int>())) ?? const {},
6864
);
6965
}
7066
return null;

0 commit comments

Comments
 (0)