Skip to content

Commit bfcfc6f

Browse files
authored
[Dart2] Added better double handling to 'mapValueOfType<T>', which previously would fail when deserding doubles (#17808)
1 parent 6bc8e0b commit bfcfc6f

File tree

3 files changed

+9
-0
lines changed

3 files changed

+9
-0
lines changed

modules/openapi-generator/src/main/resources/dart2/api_helper.mustache

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ Future<String> _decodeBodyBytes(Response response) async {
6969
/// Returns a valid [T] value found at the specified Map [key], null otherwise.
7070
T? mapValueOfType<T>(dynamic map, String key) {
7171
final dynamic value = map is Map ? map[key] : null;
72+
if (T == double && value is int) {
73+
return value.toDouble() as T;
74+
}
7275
return value is T ? value : null;
7376
}
7477

samples/openapi3/client/petstore/dart2/petstore_client_lib/lib/api_helper.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ Future<String> _decodeBodyBytes(Response response) async {
7070
/// Returns a valid [T] value found at the specified Map [key], null otherwise.
7171
T? mapValueOfType<T>(dynamic map, String key) {
7272
final dynamic value = map is Map ? map[key] : null;
73+
if (T == double && value is int) {
74+
return value.toDouble() as T;
75+
}
7376
return value is T ? value : null;
7477
}
7578

samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/lib/api_helper.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ Future<String> _decodeBodyBytes(Response response) async {
8888
/// Returns a valid [T] value found at the specified Map [key], null otherwise.
8989
T? mapValueOfType<T>(dynamic map, String key) {
9090
final dynamic value = map is Map ? map[key] : null;
91+
if (T == double && value is int) {
92+
return value.toDouble() as T;
93+
}
9194
return value is T ? value : null;
9295
}
9396

0 commit comments

Comments
 (0)