Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dart] Fix api client deserialization for json_serializable #8882

27 changes: 13 additions & 14 deletions modules/openapi-generator/src/main/resources/dart2/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class {{{classname}}} {
{{/allParams}}

{{/hasParams}}
final path = '{{{path}}}'{{#pathParams}}
final path = r'{{{path}}}'{{#pathParams}}
.replaceAll('{' + '{{{baseName}}}' + '}', {{{paramName}}}.toString()){{/pathParams}};

Object postBody{{#bodyParam}} = {{{paramName}}}{{/bodyParam}};
Expand Down Expand Up @@ -185,19 +185,18 @@ class {{{classname}}} {
// At the time of writing this, `dart:convert` will throw an "Unexpected end of input"
// FormatException when trying to decode an empty string.
if (response.body != null && response.statusCode != HttpStatus.noContent) {
{{#isArray}}
return (apiClient.deserialize(_decodeBodyBytes(response), '{{{returnType}}}') as List)
.cast<{{{returnBaseType}}}>()
.{{#uniqueItems}}toSet(){{/uniqueItems}}{{^uniqueItems}}toList(growable: false){{/uniqueItems}};
{{/isArray}}
{{^isArray}}
{{#isMap}}
return {{{returnType}}}.from(apiClient.deserialize(_decodeBodyBytes(response), '{{{returnType}}}'));
{{/isMap}}
{{^isMap}}
return apiClient.deserialize(_decodeBodyBytes(response), '{{{returnType}}}') as {{{returnType}}};
{{/isMap}}
{{/isArray}}
{{#isArray}}
agilob marked this conversation as resolved.
Show resolved Hide resolved
final l = json.decode(response.body);
return {{{returnType}}}.from(l.map((model) => Pet.fromJson(model)));
{{/isArray}}
{{^isArray}}
{{#isMap}}
return {{{returnType}}}.from(json.decode(response.body));
{{/isMap}}
{{^isMap}}
return {{{returnType}}}.fromJson(json.decode(response.body));
{{/isMap}}
{{/isArray}}
}
return null;
{{/returnType}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,6 @@ class ApiClient {
Map<String, Authentication> get authentications =>
Map.unmodifiable(_authentications);

dynamic deserialize(String json, String targetType, {bool growable}) {
// Remove all spaces. Necessary for reg expressions as well.
targetType = targetType.replaceAll(' ', '');

return targetType == 'String'
? json
: _deserialize(jsonDecode(json), targetType, growable: true == growable);
}

String serialize(Object obj) => obj == null ? '' : json.encode(obj);

T getAuthentication<T extends Authentication>(String name) {
final authentication = _authentications[name];
return authentication is T ? authentication : null;
Expand Down Expand Up @@ -159,6 +148,7 @@ class ApiClient {
throw ApiException(HttpStatus.badRequest, 'Invalid HTTP operation: $method $path',);
}

{{#native_serialization}}
dynamic _deserialize(dynamic value, String targetType, {bool growable}) {
try {
switch (targetType) {
Expand Down Expand Up @@ -216,6 +206,18 @@ class ApiClient {
throw ApiException(HttpStatus.internalServerError, 'Could not find a suitable class for deserialization',);
}

dynamic deserialize(String json, String targetType, {bool growable}) {
// Remove all spaces. Necessary for reg expressions as well.
targetType = targetType.replaceAll(' ', '');

return targetType == 'String'
? json
: _deserialize(jsonDecode(json), targetType, growable: true == growable);
}
{{/native_serialization}}

String serialize(Object obj) => obj == null ? '' : json.encode(obj);

/// Update query and header parameters based on authentication settings.
/// @param authNames The authentications to apply
void _updateParamsForAuth(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ String parameterToString(dynamic value) {
{{#model}}
{{#isEnum}}
if (value is {{{classname}}}) {
{{#native_serialization}} return {{{classname}}}TypeTransformer().encode(value).toString();{{/native_serialization}}
{{#json_serializable}} return _${{{classname}}}EnumMap[value];{{/json_serializable}}
{{#native_serialization}} return {{{classname}}}TypeTransformer().encode(value).toString();{{/native_serialization}}{{#json_serializable}} return value.toString();{{/json_serializable}}
}
{{/isEnum}}
{{/model}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class PetApi {
throw ApiException(HttpStatus.badRequest, 'Missing required param: body');
}

final path = '/pet';
final path = r'/pet';

Object postBody = body;

Expand Down Expand Up @@ -94,7 +94,7 @@ class PetApi {
throw ApiException(HttpStatus.badRequest, 'Missing required param: petId');
}

final path = '/pet/{petId}'
final path = r'/pet/{petId}'
.replaceAll('{' + 'petId' + '}', petId.toString());

Object postBody;
Expand Down Expand Up @@ -166,7 +166,7 @@ class PetApi {
throw ApiException(HttpStatus.badRequest, 'Missing required param: status');
}

final path = '/pet/findByStatus';
final path = r'/pet/findByStatus';

Object postBody;

Expand Down Expand Up @@ -221,9 +221,8 @@ class PetApi {
// At the time of writing this, `dart:convert` will throw an "Unexpected end of input"
// FormatException when trying to decode an empty string.
if (response.body != null && response.statusCode != HttpStatus.noContent) {
return (apiClient.deserialize(_decodeBodyBytes(response), 'List<Pet>') as List)
.cast<Pet>()
.toList(growable: false);
final l = json.decode(response.body);
return List<Pet>.from(l.map((model) => Pet.fromJson(model)));
}
return null;
}
Expand All @@ -244,7 +243,7 @@ class PetApi {
throw ApiException(HttpStatus.badRequest, 'Missing required param: tags');
}

final path = '/pet/findByTags';
final path = r'/pet/findByTags';

Object postBody;

Expand Down Expand Up @@ -299,9 +298,8 @@ class PetApi {
// At the time of writing this, `dart:convert` will throw an "Unexpected end of input"
// FormatException when trying to decode an empty string.
if (response.body != null && response.statusCode != HttpStatus.noContent) {
return (apiClient.deserialize(_decodeBodyBytes(response), 'List<Pet>') as List)
.cast<Pet>()
.toList(growable: false);
final l = json.decode(response.body);
return List<Pet>.from(l.map((model) => Pet.fromJson(model)));
}
return null;
}
Expand All @@ -322,7 +320,7 @@ class PetApi {
throw ApiException(HttpStatus.badRequest, 'Missing required param: petId');
}

final path = '/pet/{petId}'
final path = r'/pet/{petId}'
.replaceAll('{' + 'petId' + '}', petId.toString());

Object postBody;
Expand Down Expand Up @@ -376,7 +374,7 @@ class PetApi {
// At the time of writing this, `dart:convert` will throw an "Unexpected end of input"
// FormatException when trying to decode an empty string.
if (response.body != null && response.statusCode != HttpStatus.noContent) {
return apiClient.deserialize(_decodeBodyBytes(response), 'Pet') as Pet;
return Pet.fromJson(json.decode(response.body));
}
return null;
}
Expand All @@ -395,7 +393,7 @@ class PetApi {
throw ApiException(HttpStatus.badRequest, 'Missing required param: body');
}

final path = '/pet';
final path = r'/pet';

Object postBody = body;

Expand Down Expand Up @@ -464,7 +462,7 @@ class PetApi {
throw ApiException(HttpStatus.badRequest, 'Missing required param: petId');
}

final path = '/pet/{petId}'
final path = r'/pet/{petId}'
.replaceAll('{' + 'petId' + '}', petId.toString());

Object postBody;
Expand Down Expand Up @@ -554,7 +552,7 @@ class PetApi {
throw ApiException(HttpStatus.badRequest, 'Missing required param: petId');
}

final path = '/pet/{petId}/uploadImage'
final path = r'/pet/{petId}/uploadImage'
.replaceAll('{' + 'petId' + '}', petId.toString());

Object postBody;
Expand Down Expand Up @@ -624,7 +622,7 @@ class PetApi {
// At the time of writing this, `dart:convert` will throw an "Unexpected end of input"
// FormatException when trying to decode an empty string.
if (response.body != null && response.statusCode != HttpStatus.noContent) {
return apiClient.deserialize(_decodeBodyBytes(response), 'ApiResponse') as ApiResponse;
return ApiResponse.fromJson(json.decode(response.body));
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class StoreApi {
throw ApiException(HttpStatus.badRequest, 'Missing required param: orderId');
}

final path = '/store/order/{orderId}'
final path = r'/store/order/{orderId}'
.replaceAll('{' + 'orderId' + '}', orderId.toString());

Object postBody;
Expand Down Expand Up @@ -89,7 +89,7 @@ class StoreApi {
///
/// Note: This method returns the HTTP [Response].
Future<Response> getInventoryWithHttpInfo() async {
final path = '/store/inventory';
final path = r'/store/inventory';

Object postBody;

Expand Down Expand Up @@ -137,7 +137,7 @@ class StoreApi {
// At the time of writing this, `dart:convert` will throw an "Unexpected end of input"
// FormatException when trying to decode an empty string.
if (response.body != null && response.statusCode != HttpStatus.noContent) {
return Map<String, int>.from(apiClient.deserialize(_decodeBodyBytes(response), 'Map<String, int>'));
return Map<String, int>.from(json.decode(response.body));
}
return null;
}
Expand All @@ -158,7 +158,7 @@ class StoreApi {
throw ApiException(HttpStatus.badRequest, 'Missing required param: orderId');
}

final path = '/store/order/{orderId}'
final path = r'/store/order/{orderId}'
.replaceAll('{' + 'orderId' + '}', orderId.toString());

Object postBody;
Expand Down Expand Up @@ -212,7 +212,7 @@ class StoreApi {
// At the time of writing this, `dart:convert` will throw an "Unexpected end of input"
// FormatException when trying to decode an empty string.
if (response.body != null && response.statusCode != HttpStatus.noContent) {
return apiClient.deserialize(_decodeBodyBytes(response), 'Order') as Order;
return Order.fromJson(json.decode(response.body));
}
return null;
}
Expand All @@ -231,7 +231,7 @@ class StoreApi {
throw ApiException(HttpStatus.badRequest, 'Missing required param: body');
}

final path = '/store/order';
final path = r'/store/order';

Object postBody = body;

Expand Down Expand Up @@ -282,7 +282,7 @@ class StoreApi {
// At the time of writing this, `dart:convert` will throw an "Unexpected end of input"
// FormatException when trying to decode an empty string.
if (response.body != null && response.statusCode != HttpStatus.noContent) {
return apiClient.deserialize(_decodeBodyBytes(response), 'Order') as Order;
return Order.fromJson(json.decode(response.body));
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class UserApi {
throw ApiException(HttpStatus.badRequest, 'Missing required param: body');
}

final path = '/user';
final path = r'/user';

Object postBody = body;

Expand Down Expand Up @@ -96,7 +96,7 @@ class UserApi {
throw ApiException(HttpStatus.badRequest, 'Missing required param: body');
}

final path = '/user/createWithArray';
final path = r'/user/createWithArray';

Object postBody = body;

Expand Down Expand Up @@ -159,7 +159,7 @@ class UserApi {
throw ApiException(HttpStatus.badRequest, 'Missing required param: body');
}

final path = '/user/createWithList';
final path = r'/user/createWithList';

Object postBody = body;

Expand Down Expand Up @@ -224,7 +224,7 @@ class UserApi {
throw ApiException(HttpStatus.badRequest, 'Missing required param: username');
}

final path = '/user/{username}'
final path = r'/user/{username}'
.replaceAll('{' + 'username' + '}', username.toString());

Object postBody;
Expand Down Expand Up @@ -290,7 +290,7 @@ class UserApi {
throw ApiException(HttpStatus.badRequest, 'Missing required param: username');
}

final path = '/user/{username}'
final path = r'/user/{username}'
.replaceAll('{' + 'username' + '}', username.toString());

Object postBody;
Expand Down Expand Up @@ -342,7 +342,7 @@ class UserApi {
// At the time of writing this, `dart:convert` will throw an "Unexpected end of input"
// FormatException when trying to decode an empty string.
if (response.body != null && response.statusCode != HttpStatus.noContent) {
return apiClient.deserialize(_decodeBodyBytes(response), 'User') as User;
return User.fromJson(json.decode(response.body));
}
return null;
}
Expand All @@ -367,7 +367,7 @@ class UserApi {
throw ApiException(HttpStatus.badRequest, 'Missing required param: password');
}

final path = '/user/login';
final path = r'/user/login';

Object postBody;

Expand Down Expand Up @@ -424,7 +424,7 @@ class UserApi {
// At the time of writing this, `dart:convert` will throw an "Unexpected end of input"
// FormatException when trying to decode an empty string.
if (response.body != null && response.statusCode != HttpStatus.noContent) {
return apiClient.deserialize(_decodeBodyBytes(response), 'String') as String;
return String.fromJson(json.decode(response.body));
agilob marked this conversation as resolved.
Show resolved Hide resolved
}
return null;
}
Expand All @@ -433,7 +433,7 @@ class UserApi {
///
/// Note: This method returns the HTTP [Response].
Future<Response> logoutUserWithHttpInfo() async {
final path = '/user/logout';
final path = r'/user/logout';

Object postBody;

Expand Down Expand Up @@ -499,7 +499,7 @@ class UserApi {
throw ApiException(HttpStatus.badRequest, 'Missing required param: body');
}

final path = '/user/{username}'
final path = r'/user/{username}'
.replaceAll('{' + 'username' + '}', username.toString());

Object postBody = body;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,6 @@ class ApiClient {
Map<String, Authentication> get authentications =>
Map.unmodifiable(_authentications);

dynamic deserialize(String json, String targetType, {bool growable}) {
// Remove all spaces. Necessary for reg expressions as well.
targetType = targetType.replaceAll(' ', '');

return targetType == 'String'
? json
: _deserialize(jsonDecode(json), targetType, growable: true == growable);
}

String serialize(Object obj) => obj == null ? '' : json.encode(obj);

T getAuthentication<T extends Authentication>(String name) {
final authentication = _authentications[name];
return authentication is T ? authentication : null;
Expand Down Expand Up @@ -209,6 +198,17 @@ class ApiClient {
throw ApiException(HttpStatus.internalServerError, 'Could not find a suitable class for deserialization',);
}

dynamic deserialize(String json, String targetType, {bool growable}) {
// Remove all spaces. Necessary for reg expressions as well.
targetType = targetType.replaceAll(' ', '');

return targetType == 'String'
? json
: _deserialize(jsonDecode(json), targetType, growable: true == growable);
}

String serialize(Object obj) => obj == null ? '' : json.encode(obj);

/// Update query and header parameters based on authentication settings.
/// @param authNames The authentications to apply
void _updateParamsForAuth(
Expand Down
Loading