diff --git a/modules/openapi-generator/src/main/resources/dart/api.mustache b/modules/openapi-generator/src/main/resources/dart/api.mustache index 6ed091fc7ee..373ce2508b3 100644 --- a/modules/openapi-generator/src/main/resources/dart/api.mustache +++ b/modules/openapi-generator/src/main/resources/dart/api.mustache @@ -89,22 +89,22 @@ class {{classname}} { authNames); if(response.statusCode >= 400) { - throw new ApiException(response.statusCode, response.body); + throw new ApiException(response.statusCode, _decodeBodyBytes(response)); } else if(response.body != null) { {{#isListContainer}} {{#returnType}} - return (apiClient.deserialize(response.body, '{{{returnType}}}') as List).map((item) => item as {{returnBaseType}}).toList(); + return (apiClient.deserialize(_decodeBodyBytes(response), '{{{returnType}}}') as List).map((item) => item as {{returnBaseType}}).toList(); {{/returnType}} {{/isListContainer}} {{^isListContainer}} {{#isMapContainer}} {{#returnType}} - return new {{{returnType}}}.from(apiClient.deserialize(response.body, '{{{returnType}}}')); + return new {{{returnType}}}.from(apiClient.deserialize(_decodeBodyBytes(response), '{{{returnType}}}')); {{/returnType}}; {{/isMapContainer}} {{^isMapContainer}} {{#returnType}} - return apiClient.deserialize(response.body, '{{{returnType}}}') as {{{returnType}}}; + return apiClient.deserialize(_decodeBodyBytes(response), '{{{returnType}}}') as {{{returnType}}}; {{/returnType}} {{/isMapContainer}} {{/isListContainer}} diff --git a/modules/openapi-generator/src/main/resources/dart/api_helper.mustache b/modules/openapi-generator/src/main/resources/dart/api_helper.mustache index 3929fa2b762..73b2585b1b4 100644 --- a/modules/openapi-generator/src/main/resources/dart/api_helper.mustache +++ b/modules/openapi-generator/src/main/resources/dart/api_helper.mustache @@ -50,3 +50,15 @@ String parameterToString(dynamic value) { return value.toString(); } } + +/// Returns the decoded body by utf-8 if application/json with the given headers. +/// Else, returns the decoded body by default algorithm of dart:http. +/// Because avoid to text garbling when header only contains "application/json" without "; charset=utf-8". +String _decodeBodyBytes(Response response) { + var contentType = response.headers['content-type']; + if (contentType != null && contentType.contains("application/json")) { + return utf8.decode(response.bodyBytes); + } else { + return response.body; + } +} diff --git a/modules/openapi-generator/src/main/resources/dart2/api.mustache b/modules/openapi-generator/src/main/resources/dart2/api.mustache index 76797fa2ddc..3a29f01f827 100644 --- a/modules/openapi-generator/src/main/resources/dart2/api.mustache +++ b/modules/openapi-generator/src/main/resources/dart2/api.mustache @@ -89,22 +89,22 @@ class {{classname}} { authNames); if(response.statusCode >= 400) { - throw new ApiException(response.statusCode, response.body); + throw new ApiException(response.statusCode, _decodeBodyBytes(response)); } else if(response.body != null) { {{#isListContainer}} {{#returnType}} - return (apiClient.deserialize(response.body, '{{{returnType}}}') as List).map((item) => item as {{returnBaseType}}).toList(); + return (apiClient.deserialize(_decodeBodyBytes(response), '{{{returnType}}}') as List).map((item) => item as {{returnBaseType}}).toList(); {{/returnType}} {{/isListContainer}} {{^isListContainer}} {{#isMapContainer}} {{#returnType}} - return new {{{returnType}}}.from(apiClient.deserialize(response.body, '{{{returnType}}}')); + return new {{{returnType}}}.from(apiClient.deserialize(_decodeBodyBytes(response), '{{{returnType}}}')); {{/returnType}}; {{/isMapContainer}} {{^isMapContainer}} {{#returnType}} - return apiClient.deserialize(response.body, '{{{returnType}}}') as {{{returnType}}}; + return apiClient.deserialize(_decodeBodyBytes(response), '{{{returnType}}}') as {{{returnType}}}; {{/returnType}} {{/isMapContainer}} {{/isListContainer}} diff --git a/modules/openapi-generator/src/main/resources/dart2/api_helper.mustache b/modules/openapi-generator/src/main/resources/dart2/api_helper.mustache index 1e554bede4b..7027f30a5b6 100644 --- a/modules/openapi-generator/src/main/resources/dart2/api_helper.mustache +++ b/modules/openapi-generator/src/main/resources/dart2/api_helper.mustache @@ -50,3 +50,15 @@ String parameterToString(dynamic value) { return value.toString(); } } + +/// Returns the decoded body by utf-8 if application/json with the given headers. +/// Else, returns the decoded body by default algorithm of dart:http. +/// Because avoid to text garbling when header only contains "application/json" without "; charset=utf-8". +String _decodeBodyBytes(Response response) { + var contentType = response.headers['content-type']; + if (contentType != null && contentType.contains("application/json")) { + return utf8.decode(response.bodyBytes); + } else { + return response.body; + } +}