Skip to content

Commit 3252fcf

Browse files
authored
[dart] Preserve inner generic type for Map<String, List<T>> deserialization (#22717)
* preserve inner generic type for Map<String, List<T>> deserialization * regenerated samples * retrigger ci
1 parent 9432aaf commit 3252fcf

8 files changed

Lines changed: 58 additions & 6 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ class {{{classname}}} {
163163
: {{items.complexType}}.mapListFromJson(json[r'{{{baseName}}}']),
164164
{{/items.complexType}}
165165
{{^items.complexType}}
166-
: mapCastOfType<String, List>(json, r'{{{baseName}}}'),
166+
: (json[r'{{{baseName}}}'] as Map<String, dynamic>).map((k, v) => MapEntry(k, v == null ? {{#items.isNullable}}null{{/items.isNullable}}{{^items.isNullable}}const <{{items.items.dataType}}>[]{{/items.isNullable}} : (v as List).cast<{{items.items.dataType}}>())),
167167
{{/items.complexType}}
168168
{{/items.isArray}}
169169
{{^items.isArray}}

modules/openapi-generator/src/test/resources/3_0/dart/petstore-with-fake-endpoints-models-for-testing.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1731,6 +1731,12 @@ components:
17311731
type: object
17321732
additionalProperties:
17331733
type: string
1734+
map_of_array_integer:
1735+
type: object
1736+
additionalProperties:
1737+
type: array
1738+
items:
1739+
type: integer
17341740
MixedPropertiesAndAdditionalPropertiesClass:
17351741
type: object
17361742
properties:

samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/doc/AdditionalPropertiesClass.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Name | Type | Description | Notes
1010
------------ | ------------- | ------------- | -------------
1111
**mapProperty** | **Map&lt;String, String&gt;** | | [optional]
1212
**mapOfMapProperty** | [**Map&lt;String, Map&lt;String, String&gt;&gt;**](Map.md) | | [optional]
13+
**mapOfArrayInteger** | [**Map&lt;String, List&lt;int&gt;&gt;**](List.md) | | [optional]
1314

1415
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
1516

samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/model/additional_properties_class.dart

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class AdditionalPropertiesClass {
2323
this.mapProperty,
2424

2525
this.mapOfMapProperty,
26+
27+
this.mapOfArrayInteger,
2628
});
2729

2830
@JsonKey(
@@ -49,17 +51,31 @@ class AdditionalPropertiesClass {
4951

5052

5153

54+
@JsonKey(
55+
56+
name: r'map_of_array_integer',
57+
required: false,
58+
includeIfNull: false,
59+
)
60+
61+
62+
final Map<String, List<int>>? mapOfArrayInteger;
63+
64+
65+
5266

5367

5468
@override
5569
bool operator ==(Object other) => identical(this, other) || other is AdditionalPropertiesClass &&
5670
other.mapProperty == mapProperty &&
57-
other.mapOfMapProperty == mapOfMapProperty;
71+
other.mapOfMapProperty == mapOfMapProperty &&
72+
other.mapOfArrayInteger == mapOfArrayInteger;
5873

5974
@override
6075
int get hashCode =>
6176
mapProperty.hashCode +
62-
mapOfMapProperty.hashCode;
77+
mapOfMapProperty.hashCode +
78+
mapOfArrayInteger.hashCode;
6379

6480
factory AdditionalPropertiesClass.fromJson(Map<String, dynamic> json) => _$AdditionalPropertiesClassFromJson(json);
6581

samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/doc/AdditionalPropertiesClass.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Name | Type | Description | Notes
1010
------------ | ------------- | ------------- | -------------
1111
**mapProperty** | **BuiltMap&lt;String, String&gt;** | | [optional]
1212
**mapOfMapProperty** | [**BuiltMap&lt;String, BuiltMap&lt;String, String&gt;&gt;**](BuiltMap.md) | | [optional]
13+
**mapOfArrayInteger** | [**BuiltMap&lt;String, BuiltList&lt;int&gt;&gt;**](BuiltList.md) | | [optional]
1314

1415
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
1516

samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/lib/src/model/additional_properties_class.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ part 'additional_properties_class.g.dart';
1414
/// Properties:
1515
/// * [mapProperty]
1616
/// * [mapOfMapProperty]
17+
/// * [mapOfArrayInteger]
1718
@BuiltValue()
1819
abstract class AdditionalPropertiesClass implements Built<AdditionalPropertiesClass, AdditionalPropertiesClassBuilder> {
1920
@BuiltValueField(wireName: r'map_property')
@@ -22,6 +23,9 @@ abstract class AdditionalPropertiesClass implements Built<AdditionalPropertiesCl
2223
@BuiltValueField(wireName: r'map_of_map_property')
2324
BuiltMap<String, BuiltMap<String, String>>? get mapOfMapProperty;
2425

26+
@BuiltValueField(wireName: r'map_of_array_integer')
27+
BuiltMap<String, BuiltList<int>>? get mapOfArrayInteger;
28+
2529
AdditionalPropertiesClass._();
2630

2731
factory AdditionalPropertiesClass([void updates(AdditionalPropertiesClassBuilder b)]) = _$AdditionalPropertiesClass;
@@ -59,6 +63,13 @@ class _$AdditionalPropertiesClassSerializer implements PrimitiveSerializer<Addit
5963
specifiedType: const FullType(BuiltMap, [FullType(String), FullType(BuiltMap, [FullType(String), FullType(String)])]),
6064
);
6165
}
66+
if (object.mapOfArrayInteger != null) {
67+
yield r'map_of_array_integer';
68+
yield serializers.serialize(
69+
object.mapOfArrayInteger,
70+
specifiedType: const FullType(BuiltMap, [FullType(String), FullType(BuiltList, [FullType(int)])]),
71+
);
72+
}
6273
}
6374

6475
@override
@@ -96,6 +107,13 @@ class _$AdditionalPropertiesClassSerializer implements PrimitiveSerializer<Addit
96107
) as BuiltMap<String, BuiltMap<String, String>>;
97108
result.mapOfMapProperty.replace(valueDes);
98109
break;
110+
case r'map_of_array_integer':
111+
final valueDes = serializers.deserialize(
112+
value,
113+
specifiedType: const FullType(BuiltMap, [FullType(String), FullType(BuiltList, [FullType(int)])]),
114+
) as BuiltMap<String, BuiltList<int>>;
115+
result.mapOfArrayInteger.replace(valueDes);
116+
break;
99117
default:
100118
unhandled.add(key);
101119
unhandled.add(value);

samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/doc/AdditionalPropertiesClass.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Name | Type | Description | Notes
1010
------------ | ------------- | ------------- | -------------
1111
**mapProperty** | **Map<String, String>** | | [optional] [default to const {}]
1212
**mapOfMapProperty** | [**Map<String, Map<String, String>>**](Map.md) | | [optional] [default to const {}]
13+
**mapOfArrayInteger** | [**Map<String, List<int>>**](List.md) | | [optional] [default to const {}]
1314

1415
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
1516

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,36 @@ class AdditionalPropertiesClass {
1515
AdditionalPropertiesClass({
1616
this.mapProperty = const {},
1717
this.mapOfMapProperty = const {},
18+
this.mapOfArrayInteger = const {},
1819
});
1920

2021
Map<String, String> mapProperty;
2122

2223
Map<String, Map<String, String>> mapOfMapProperty;
2324

25+
Map<String, List<int>> mapOfArrayInteger;
26+
2427
@override
2528
bool operator ==(Object other) => identical(this, other) || other is AdditionalPropertiesClass &&
2629
_deepEquality.equals(other.mapProperty, mapProperty) &&
27-
_deepEquality.equals(other.mapOfMapProperty, mapOfMapProperty);
30+
_deepEquality.equals(other.mapOfMapProperty, mapOfMapProperty) &&
31+
_deepEquality.equals(other.mapOfArrayInteger, mapOfArrayInteger);
2832

2933
@override
3034
int get hashCode =>
3135
// ignore: unnecessary_parenthesis
3236
(mapProperty.hashCode) +
33-
(mapOfMapProperty.hashCode);
37+
(mapOfMapProperty.hashCode) +
38+
(mapOfArrayInteger.hashCode);
3439

3540
@override
36-
String toString() => 'AdditionalPropertiesClass[mapProperty=$mapProperty, mapOfMapProperty=$mapOfMapProperty]';
41+
String toString() => 'AdditionalPropertiesClass[mapProperty=$mapProperty, mapOfMapProperty=$mapOfMapProperty, mapOfArrayInteger=$mapOfArrayInteger]';
3742

3843
Map<String, dynamic> toJson() {
3944
final json = <String, dynamic>{};
4045
json[r'map_property'] = this.mapProperty;
4146
json[r'map_of_map_property'] = this.mapOfMapProperty;
47+
json[r'map_of_array_integer'] = this.mapOfArrayInteger;
4248
return json;
4349
}
4450

@@ -63,6 +69,9 @@ class AdditionalPropertiesClass {
6369
return AdditionalPropertiesClass(
6470
mapProperty: mapCastOfType<String, String>(json, r'map_property') ?? const {},
6571
mapOfMapProperty: mapCastOfType<String, dynamic>(json, r'map_of_map_property') ?? const {},
72+
mapOfArrayInteger: json[r'map_of_array_integer'] == null
73+
? const {}
74+
: (json[r'map_of_array_integer'] as Map<String, dynamic>).map((k, v) => MapEntry(k, v == null ? const <int>[] : (v as List).cast<int>())),
6675
);
6776
}
6877
return null;

0 commit comments

Comments
 (0)