Skip to content

Commit

Permalink
Introduce changes necessary for Dart 3.0.0/Flutter 3.10.0 (#15516)
Browse files Browse the repository at this point in the history
* Expose `deserialize` function.

* Rename `json` argument.

* Generate Petstore code.

* Upgrade minimum version of `intl` dependency.
  • Loading branch information
noordawod authored Jun 20, 2023
1 parent e0d89c3 commit 3dd93be
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,19 +134,19 @@ class ApiClient {
}
{{#native_serialization}}
Future<dynamic> deserializeAsync(String json, String targetType, {bool growable = false,}) async =>
Future<dynamic> 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}}
Expand All @@ -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':
Expand Down Expand Up @@ -189,18 +190,18 @@ class ApiClient {
dynamic match;
if (value is List && (match = _regList.firstMatch(targetType)?.group(1)) != null) {
return value
.map<dynamic>((dynamic v) => _deserialize(v, match, growable: growable,))
.map<dynamic>((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>((dynamic v) => _deserialize(v, match, growable: growable,))
.map<dynamic>((dynamic v) => fromJson(v, match, growable: growable,))
.toSet();
}
if (value is Map && (match = _regMap.firstMatch(targetType)?.group(1)) != null) {
return Map<String, dynamic>.fromIterables(
value.keys.cast<String>(),
value.values.map<dynamic>((dynamic v) => _deserialize(v, match, growable: growable,)),
value.values.map<dynamic>((dynamic v) => fromJson(v, match, growable: growable,)),
);
}
}
Expand Down Expand Up @@ -231,6 +232,17 @@ class DeserializationMessage {
final bool growable;
}

/// Primarily intended for use in an isolate.
Future<dynamic> 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<dynamic> deserializeAsync(DeserializationMessage message) async {
// Remove all spaces. Necessary for regular expressions as well.
Expand All @@ -239,8 +251,8 @@ Future<dynamic> 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,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
json_serializable: '^3.5.1'{{/json_serializable}}
Original file line number Diff line number Diff line change
Expand Up @@ -143,19 +143,19 @@ class ApiClient {
);
}

Future<dynamic> deserializeAsync(String json, String targetType, {bool growable = false,}) async =>
Future<dynamic> 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
Expand All @@ -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':
Expand Down Expand Up @@ -197,18 +198,18 @@ class ApiClient {
dynamic match;
if (value is List && (match = _regList.firstMatch(targetType)?.group(1)) != null) {
return value
.map<dynamic>((dynamic v) => _deserialize(v, match, growable: growable,))
.map<dynamic>((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>((dynamic v) => _deserialize(v, match, growable: growable,))
.map<dynamic>((dynamic v) => fromJson(v, match, growable: growable,))
.toSet();
}
if (value is Map && (match = _regMap.firstMatch(targetType)?.group(1)) != null) {
return Map<String, dynamic>.fromIterables(
value.keys.cast<String>(),
value.values.map<dynamic>((dynamic v) => _deserialize(v, match, growable: growable,)),
value.values.map<dynamic>((dynamic v) => fromJson(v, match, growable: growable,)),
);
}
}
Expand Down Expand Up @@ -237,6 +238,17 @@ class DeserializationMessage {
final bool growable;
}

/// Primarily intended for use in an isolate.
Future<dynamic> 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<dynamic> deserializeAsync(DeserializationMessage message) async {
// Remove all spaces. Necessary for regular expressions as well.
Expand All @@ -245,8 +257,8 @@ Future<dynamic> 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,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Original file line number Diff line number Diff line change
Expand Up @@ -143,19 +143,19 @@ class ApiClient {
);
}

Future<dynamic> deserializeAsync(String json, String targetType, {bool growable = false,}) async =>
Future<dynamic> 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
Expand All @@ -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':
Expand Down Expand Up @@ -279,18 +280,18 @@ class ApiClient {
dynamic match;
if (value is List && (match = _regList.firstMatch(targetType)?.group(1)) != null) {
return value
.map<dynamic>((dynamic v) => _deserialize(v, match, growable: growable,))
.map<dynamic>((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>((dynamic v) => _deserialize(v, match, growable: growable,))
.map<dynamic>((dynamic v) => fromJson(v, match, growable: growable,))
.toSet();
}
if (value is Map && (match = _regMap.firstMatch(targetType)?.group(1)) != null) {
return Map<String, dynamic>.fromIterables(
value.keys.cast<String>(),
value.values.map<dynamic>((dynamic v) => _deserialize(v, match, growable: growable,)),
value.values.map<dynamic>((dynamic v) => fromJson(v, match, growable: growable,)),
);
}
}
Expand Down Expand Up @@ -319,6 +320,17 @@ class DeserializationMessage {
final bool growable;
}

/// Primarily intended for use in an isolate.
Future<dynamic> 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<dynamic> deserializeAsync(DeserializationMessage message) async {
// Remove all spaces. Necessary for regular expressions as well.
Expand All @@ -327,8 +339,8 @@ Future<dynamic> 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,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'

0 comments on commit 3dd93be

Please sign in to comment.