From 3dd93beac2dda879b735d0641ba3c4a6991a91d3 Mon Sep 17 00:00:00 2001 From: Noor Dawod Date: Tue, 20 Jun 2023 03:35:25 +0200 Subject: [PATCH] Introduce changes necessary for Dart 3.0.0/Flutter 3.10.0 (#15516) * Expose `deserialize` function. * Rename `json` argument. * Generate Petstore code. * Upgrade minimum version of `intl` dependency. --- .../main/resources/dart2/api_client.mustache | 34 +++++++++++++------ .../src/main/resources/dart2/pubspec.mustache | 4 +-- .../petstore_client_lib/lib/api_client.dart | 34 +++++++++++++------ .../dart2/petstore_client_lib/pubspec.yaml | 3 +- .../lib/api_client.dart | 34 +++++++++++++------ .../petstore_client_lib_fake/pubspec.yaml | 3 +- 6 files changed, 75 insertions(+), 37 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/dart2/api_client.mustache b/modules/openapi-generator/src/main/resources/dart2/api_client.mustache index b340a974575..00f5e6aeb9a 100644 --- a/modules/openapi-generator/src/main/resources/dart2/api_client.mustache +++ b/modules/openapi-generator/src/main/resources/dart2/api_client.mustache @@ -134,19 +134,19 @@ class ApiClient { } {{#native_serialization}} - Future deserializeAsync(String json, String targetType, {bool growable = false,}) async => + Future deserializeAsync(String value, String targetType, {bool growable = false,}) async => // ignore: deprecated_member_use_from_same_package - deserialize(json, targetType, growable: growable); + deserialize(value, targetType, growable: growable); @Deprecated('Scheduled for removal in OpenAPI Generator 6.x. Use deserializeAsync() instead.') - dynamic deserialize(String json, String targetType, {bool growable = false,}) { + dynamic deserialize(String value, String targetType, {bool growable = false,}) { // Remove all spaces. Necessary for regular expressions as well. targetType = targetType.replaceAll(' ', ''); // ignore: parameter_assignments // If the expected target type is String, nothing to do... return targetType == 'String' - ? json - : _deserialize(jsonDecode(json), targetType, growable: growable); + ? value + : fromJson(json.decode(value), targetType, growable: growable); } {{/native_serialization}} @@ -157,7 +157,8 @@ class ApiClient { String serialize(Object? value) => value == null ? '' : json.encode(value); {{#native_serialization}} - static dynamic _deserialize(dynamic value, String targetType, {bool growable = false}) { + /// Returns a native instance of an OpenAPI class matching the [specified type][targetType]. + static dynamic fromJson(dynamic value, String targetType, {bool growable = false,}) { try { switch (targetType) { case 'String': @@ -189,18 +190,18 @@ class ApiClient { dynamic match; if (value is List && (match = _regList.firstMatch(targetType)?.group(1)) != null) { return value - .map((dynamic v) => _deserialize(v, match, growable: growable,)) + .map((dynamic v) => fromJson(v, match, growable: growable,)) .toList(growable: growable); } if (value is Set && (match = _regSet.firstMatch(targetType)?.group(1)) != null) { return value - .map((dynamic v) => _deserialize(v, match, growable: growable,)) + .map((dynamic v) => fromJson(v, match, growable: growable,)) .toSet(); } if (value is Map && (match = _regMap.firstMatch(targetType)?.group(1)) != null) { return Map.fromIterables( value.keys.cast(), - value.values.map((dynamic v) => _deserialize(v, match, growable: growable,)), + value.values.map((dynamic v) => fromJson(v, match, growable: growable,)), ); } } @@ -231,6 +232,17 @@ class DeserializationMessage { final bool growable; } +/// Primarily intended for use in an isolate. +Future decodeAsync(DeserializationMessage message) async { + // Remove all spaces. Necessary for regular expressions as well. + final targetType = message.targetType.replaceAll(' ', ''); + + // If the expected target type is String, nothing to do... + return targetType == 'String' + ? message.json + : json.decode(message.json); +} + /// Primarily intended for use in an isolate. Future deserializeAsync(DeserializationMessage message) async { // Remove all spaces. Necessary for regular expressions as well. @@ -239,8 +251,8 @@ Future deserializeAsync(DeserializationMessage message) async { // If the expected target type is String, nothing to do... return targetType == 'String' ? message.json - : ApiClient._deserialize( - jsonDecode(message.json), + : ApiClient.fromJson( + json.decode(message.json), targetType, growable: message.growable, ); diff --git a/modules/openapi-generator/src/main/resources/dart2/pubspec.mustache b/modules/openapi-generator/src/main/resources/dart2/pubspec.mustache index 0b054d73d5c..8a74ffa028e 100644 --- a/modules/openapi-generator/src/main/resources/dart2/pubspec.mustache +++ b/modules/openapi-generator/src/main/resources/dart2/pubspec.mustache @@ -16,10 +16,10 @@ environment: sdk: '>=2.12.0 <3.0.0' dependencies: http: '>=0.13.0 <0.14.0' - intl: '^0.17.0' + intl: '^0.18.0' meta: '^1.1.8' dev_dependencies: test: '>=1.16.0 <1.18.0' {{#json_serializable}} build_runner: '^1.10.9' - json_serializable: '^3.5.1'{{/json_serializable}} \ No newline at end of file + json_serializable: '^3.5.1'{{/json_serializable}} diff --git a/samples/openapi3/client/petstore/dart2/petstore_client_lib/lib/api_client.dart b/samples/openapi3/client/petstore/dart2/petstore_client_lib/lib/api_client.dart index 00b9e81f7ea..303efa0f896 100644 --- a/samples/openapi3/client/petstore/dart2/petstore_client_lib/lib/api_client.dart +++ b/samples/openapi3/client/petstore/dart2/petstore_client_lib/lib/api_client.dart @@ -143,19 +143,19 @@ class ApiClient { ); } - Future deserializeAsync(String json, String targetType, {bool growable = false,}) async => + Future deserializeAsync(String value, String targetType, {bool growable = false,}) async => // ignore: deprecated_member_use_from_same_package - deserialize(json, targetType, growable: growable); + deserialize(value, targetType, growable: growable); @Deprecated('Scheduled for removal in OpenAPI Generator 6.x. Use deserializeAsync() instead.') - dynamic deserialize(String json, String targetType, {bool growable = false,}) { + dynamic deserialize(String value, String targetType, {bool growable = false,}) { // Remove all spaces. Necessary for regular expressions as well. targetType = targetType.replaceAll(' ', ''); // ignore: parameter_assignments // If the expected target type is String, nothing to do... return targetType == 'String' - ? json - : _deserialize(jsonDecode(json), targetType, growable: growable); + ? value + : fromJson(json.decode(value), targetType, growable: growable); } // ignore: deprecated_member_use_from_same_package @@ -164,7 +164,8 @@ class ApiClient { @Deprecated('Scheduled for removal in OpenAPI Generator 6.x. Use serializeAsync() instead.') String serialize(Object? value) => value == null ? '' : json.encode(value); - static dynamic _deserialize(dynamic value, String targetType, {bool growable = false}) { + /// Returns a native instance of an OpenAPI class matching the [specified type][targetType]. + static dynamic fromJson(dynamic value, String targetType, {bool growable = false,}) { try { switch (targetType) { case 'String': @@ -197,18 +198,18 @@ class ApiClient { dynamic match; if (value is List && (match = _regList.firstMatch(targetType)?.group(1)) != null) { return value - .map((dynamic v) => _deserialize(v, match, growable: growable,)) + .map((dynamic v) => fromJson(v, match, growable: growable,)) .toList(growable: growable); } if (value is Set && (match = _regSet.firstMatch(targetType)?.group(1)) != null) { return value - .map((dynamic v) => _deserialize(v, match, growable: growable,)) + .map((dynamic v) => fromJson(v, match, growable: growable,)) .toSet(); } if (value is Map && (match = _regMap.firstMatch(targetType)?.group(1)) != null) { return Map.fromIterables( value.keys.cast(), - value.values.map((dynamic v) => _deserialize(v, match, growable: growable,)), + value.values.map((dynamic v) => fromJson(v, match, growable: growable,)), ); } } @@ -237,6 +238,17 @@ class DeserializationMessage { final bool growable; } +/// Primarily intended for use in an isolate. +Future decodeAsync(DeserializationMessage message) async { + // Remove all spaces. Necessary for regular expressions as well. + final targetType = message.targetType.replaceAll(' ', ''); + + // If the expected target type is String, nothing to do... + return targetType == 'String' + ? message.json + : json.decode(message.json); +} + /// Primarily intended for use in an isolate. Future deserializeAsync(DeserializationMessage message) async { // Remove all spaces. Necessary for regular expressions as well. @@ -245,8 +257,8 @@ Future deserializeAsync(DeserializationMessage message) async { // If the expected target type is String, nothing to do... return targetType == 'String' ? message.json - : ApiClient._deserialize( - jsonDecode(message.json), + : ApiClient.fromJson( + json.decode(message.json), targetType, growable: message.growable, ); diff --git a/samples/openapi3/client/petstore/dart2/petstore_client_lib/pubspec.yaml b/samples/openapi3/client/petstore/dart2/petstore_client_lib/pubspec.yaml index 00043ba660c..49f21520ec8 100644 --- a/samples/openapi3/client/petstore/dart2/petstore_client_lib/pubspec.yaml +++ b/samples/openapi3/client/petstore/dart2/petstore_client_lib/pubspec.yaml @@ -10,7 +10,8 @@ environment: sdk: '>=2.12.0 <3.0.0' dependencies: http: '>=0.13.0 <0.14.0' - intl: '^0.17.0' + intl: '^0.18.0' meta: '^1.1.8' dev_dependencies: test: '>=1.16.0 <1.18.0' + diff --git a/samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/lib/api_client.dart b/samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/lib/api_client.dart index 4a50876c56e..d494ed9f13f 100644 --- a/samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/lib/api_client.dart +++ b/samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/lib/api_client.dart @@ -143,19 +143,19 @@ class ApiClient { ); } - Future deserializeAsync(String json, String targetType, {bool growable = false,}) async => + Future deserializeAsync(String value, String targetType, {bool growable = false,}) async => // ignore: deprecated_member_use_from_same_package - deserialize(json, targetType, growable: growable); + deserialize(value, targetType, growable: growable); @Deprecated('Scheduled for removal in OpenAPI Generator 6.x. Use deserializeAsync() instead.') - dynamic deserialize(String json, String targetType, {bool growable = false,}) { + dynamic deserialize(String value, String targetType, {bool growable = false,}) { // Remove all spaces. Necessary for regular expressions as well. targetType = targetType.replaceAll(' ', ''); // ignore: parameter_assignments // If the expected target type is String, nothing to do... return targetType == 'String' - ? json - : _deserialize(jsonDecode(json), targetType, growable: growable); + ? value + : fromJson(json.decode(value), targetType, growable: growable); } // ignore: deprecated_member_use_from_same_package @@ -164,7 +164,8 @@ class ApiClient { @Deprecated('Scheduled for removal in OpenAPI Generator 6.x. Use serializeAsync() instead.') String serialize(Object? value) => value == null ? '' : json.encode(value); - static dynamic _deserialize(dynamic value, String targetType, {bool growable = false}) { + /// Returns a native instance of an OpenAPI class matching the [specified type][targetType]. + static dynamic fromJson(dynamic value, String targetType, {bool growable = false,}) { try { switch (targetType) { case 'String': @@ -279,18 +280,18 @@ class ApiClient { dynamic match; if (value is List && (match = _regList.firstMatch(targetType)?.group(1)) != null) { return value - .map((dynamic v) => _deserialize(v, match, growable: growable,)) + .map((dynamic v) => fromJson(v, match, growable: growable,)) .toList(growable: growable); } if (value is Set && (match = _regSet.firstMatch(targetType)?.group(1)) != null) { return value - .map((dynamic v) => _deserialize(v, match, growable: growable,)) + .map((dynamic v) => fromJson(v, match, growable: growable,)) .toSet(); } if (value is Map && (match = _regMap.firstMatch(targetType)?.group(1)) != null) { return Map.fromIterables( value.keys.cast(), - value.values.map((dynamic v) => _deserialize(v, match, growable: growable,)), + value.values.map((dynamic v) => fromJson(v, match, growable: growable,)), ); } } @@ -319,6 +320,17 @@ class DeserializationMessage { final bool growable; } +/// Primarily intended for use in an isolate. +Future decodeAsync(DeserializationMessage message) async { + // Remove all spaces. Necessary for regular expressions as well. + final targetType = message.targetType.replaceAll(' ', ''); + + // If the expected target type is String, nothing to do... + return targetType == 'String' + ? message.json + : json.decode(message.json); +} + /// Primarily intended for use in an isolate. Future deserializeAsync(DeserializationMessage message) async { // Remove all spaces. Necessary for regular expressions as well. @@ -327,8 +339,8 @@ Future deserializeAsync(DeserializationMessage message) async { // If the expected target type is String, nothing to do... return targetType == 'String' ? message.json - : ApiClient._deserialize( - jsonDecode(message.json), + : ApiClient.fromJson( + json.decode(message.json), targetType, growable: message.growable, ); diff --git a/samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/pubspec.yaml b/samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/pubspec.yaml index 00043ba660c..49f21520ec8 100644 --- a/samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/pubspec.yaml +++ b/samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/pubspec.yaml @@ -10,7 +10,8 @@ environment: sdk: '>=2.12.0 <3.0.0' dependencies: http: '>=0.13.0 <0.14.0' - intl: '^0.17.0' + intl: '^0.18.0' meta: '^1.1.8' dev_dependencies: test: '>=1.16.0 <1.18.0' +