From 99e04b211d9891e85a5ccb3034d99a3674599490 Mon Sep 17 00:00:00 2001 From: Noor Dawod Date: Mon, 15 May 2023 21:18:56 +0200 Subject: [PATCH 1/4] Expose `deserialize` function. --- .../main/resources/dart2/api_client.mustache | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 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 b340a9745757..813934d770ad 100644 --- a/modules/openapi-generator/src/main/resources/dart2/api_client.mustache +++ b/modules/openapi-generator/src/main/resources/dart2/api_client.mustache @@ -146,7 +146,7 @@ class ApiClient { // If the expected target type is String, nothing to do... return targetType == 'String' ? json - : _deserialize(jsonDecode(json), targetType, growable: growable); + : deserialize(jsonDecode(json), 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 deserialize(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) => deserialize(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) => deserialize(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) => deserialize(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 + : jsonDecode(message.json); +} + /// Primarily intended for use in an isolate. Future deserializeAsync(DeserializationMessage message) async { // Remove all spaces. Necessary for regular expressions as well. @@ -239,7 +251,7 @@ Future deserializeAsync(DeserializationMessage message) async { // If the expected target type is String, nothing to do... return targetType == 'String' ? message.json - : ApiClient._deserialize( + : ApiClient.deserialize( jsonDecode(message.json), targetType, growable: message.growable, From fcce90cd8819481b37371c92dbdc471811cb74ac Mon Sep 17 00:00:00 2001 From: Noor Dawod Date: Mon, 15 May 2023 22:23:04 +0200 Subject: [PATCH 2/4] Rename `json` argument. --- .../main/resources/dart2/api_client.mustache | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 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 813934d770ad..00f5e6aeb9a5 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}} @@ -158,7 +158,7 @@ class ApiClient { {{#native_serialization}} /// Returns a native instance of an OpenAPI class matching the [specified type][targetType]. - static dynamic deserialize(dynamic value, String targetType, {bool growable = false,}) { + static dynamic fromJson(dynamic value, String targetType, {bool growable = false,}) { try { switch (targetType) { case 'String': @@ -190,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,)), ); } } @@ -240,7 +240,7 @@ Future decodeAsync(DeserializationMessage message) async { // If the expected target type is String, nothing to do... return targetType == 'String' ? message.json - : jsonDecode(message.json); + : json.decode(message.json); } /// Primarily intended for use in an isolate. @@ -251,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, ); From 7136e0a175eee346dfaf57ad7b1a88c3f39d98cd Mon Sep 17 00:00:00 2001 From: Noor Dawod Date: Mon, 15 May 2023 22:41:30 +0200 Subject: [PATCH 3/4] Generate Petstore code. --- .../petstore_client_lib/lib/api_client.dart | 34 +++++++++++++------ .../lib/api_client.dart | 34 +++++++++++++------ 2 files changed, 46 insertions(+), 22 deletions(-) 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 00b9e81f7ea9..303efa0f8961 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_fake/lib/api_client.dart b/samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/lib/api_client.dart index 22839ae04f0b..3802827de866 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': @@ -281,18 +282,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,)), ); } } @@ -321,6 +322,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. @@ -329,8 +341,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, ); From 1e2ea981b5a099ffa9f5199df2f9260569322fc9 Mon Sep 17 00:00:00 2001 From: Noor Dawod Date: Wed, 24 May 2023 13:00:58 +0300 Subject: [PATCH 4/4] Upgrade minimum version of `intl` dependency. --- .../src/main/resources/dart2/pubspec.mustache | 4 ++-- .../client/petstore/dart2/petstore_client_lib/pubspec.yaml | 3 ++- .../petstore/dart2/petstore_client_lib_fake/pubspec.yaml | 3 ++- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/dart2/pubspec.mustache b/modules/openapi-generator/src/main/resources/dart2/pubspec.mustache index 0b054d73d5c2..8a74ffa028e7 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/pubspec.yaml b/samples/openapi3/client/petstore/dart2/petstore_client_lib/pubspec.yaml index 00043ba660cd..49f21520ec84 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/pubspec.yaml b/samples/openapi3/client/petstore/dart2/petstore_client_lib_fake/pubspec.yaml index 00043ba660cd..49f21520ec84 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' +