Skip to content

Commit

Permalink
use utf-8 encoding for json responses.
Browse files Browse the repository at this point in the history
  • Loading branch information
hpoul committed Aug 21, 2024
1 parent e137630 commit b7aed63
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 16 deletions.
4 changes: 4 additions & 0 deletions packages/openapi_base/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.0.0-rc.1

* Use UTF-8 encoding by default for json responses.

## 1.3.2

* Allow encoding optional parameters.
Expand Down
24 changes: 18 additions & 6 deletions packages/openapi_base/lib/src/openapi_client_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ abstract class OpenApiClientResponse {

Map<String, List<String>> get headers;

OpenApiContentType responseContentType();
OpenApiContentType? responseContentType();

Future<Map<String, dynamic>> responseBodyJson();

Expand Down Expand Up @@ -342,12 +342,12 @@ class HttpClientResponse extends OpenApiClientResponse {

@override
Future<Map<String, dynamic>> responseBodyJson() async {
return json.decode(response.body) as Map<String, dynamic>;
return json.decode(await responseBodyString()) as Map<String, dynamic>;
}

@override
Future<dynamic> responseBodyJsonDynamic() async {
return json.decode(response.body);
return json.decode(await responseBodyString());
}

@override
Expand All @@ -357,14 +357,26 @@ class HttpClientResponse extends OpenApiClientResponse {
late final Map<String, List<String>> headers =
response.headers.map((key, value) => MapEntry(key, [value]));

String? _responseContentTypeString() => response.headers['content-type'];

@override
OpenApiContentType responseContentType() {
final contentTypeString = response.headers['content-type']!;
return OpenApiContentType.parse(contentTypeString);
OpenApiContentType? responseContentType() {
if (_responseContentTypeString() case final contentTypeString?) {
return OpenApiContentType.parse(contentTypeString);
}
return null;
}

@override
Future<String> responseBodyString() async {
if (responseContentType() case final contentType?) {
final encoding = switch (contentType.charset) {
final charset? => Encoding.getByName(charset),
null => null,
} ??
(contentType.isJson ? utf8 : latin1);
return encoding.decode(response.bodyBytes);
}
return response.body;
}

Expand Down
11 changes: 9 additions & 2 deletions packages/openapi_base/lib/src/openapi_content_type.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import 'package:http_parser/http_parser.dart';

/// Represents the content type header describing content encoding for
/// request and responses.
class OpenApiContentType {
const OpenApiContentType._(this.contentType);
const OpenApiContentType._(this.contentType, [this.mediaType]);

/// Parses the value as returned by [toString]
factory OpenApiContentType.parse(String value) {
return OpenApiContentType._(value);
final mediaType = MediaType.parse(value);
return OpenApiContentType._(value, mediaType);
}

final MediaType? mediaType;

static const json = OpenApiContentType._('application/json');
static const html = OpenApiContentType._('text/html');
static const textPlain = OpenApiContentType._('text/plain');
Expand All @@ -23,6 +28,8 @@ class OpenApiContentType {

static const allKnown = [json, html, urlencoded];

String? get charset => mediaType?.parameters['charset'];

/// Reverse of [parse]
@override
String toString() {
Expand Down
12 changes: 6 additions & 6 deletions packages/openapi_base/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ packages:
dependency: "direct main"
description:
name: collection
sha256: ee67cb0715911d28db6bf4af1026078bd6f0128b07a5f66fb2ed94ec6783c09a
sha256: a1ace0a119f20aabc852d165077c036cd864315bd99b7eaa10a60100341941bf
url: "https://pub.dev"
source: hosted
version: "1.18.0"
version: "1.19.0"
crypto:
dependency: transitive
description:
Expand Down Expand Up @@ -82,13 +82,13 @@ packages:
source: hosted
version: "1.2.0"
http_parser:
dependency: transitive
dependency: "direct main"
description:
name: http_parser
sha256: "2aa08ce0341cc9b354a498388e30986515406668dbcc4f7c950c3e715496693b"
sha256: "40f592dd352890c3b60fec1b68e786cefb9603e05ff303dbc4dda49b304ecdf4"
url: "https://pub.dev"
source: hosted
version: "4.0.2"
version: "4.1.0"
intl:
dependency: transitive
description:
Expand Down Expand Up @@ -266,4 +266,4 @@ packages:
source: hosted
version: "0.4.2"
sdks:
dart: ">=3.2.0 <4.0.0"
dart: ">=3.4.0 <4.0.0"
5 changes: 3 additions & 2 deletions packages/openapi_base/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
name: openapi_base
description: Open API base implementation for client/server to be used with openapi_code_builder.
version: 1.3.2
version: 2.0.0-rc.1
homepage: https://github.com/hpoul/openapi_dart/tree/master/packages/openapi_base

environment:
sdk: '>=2.17.0 <4.0.0'
sdk: '>=3.3.0 <4.0.0'

dependencies:
# path: ^1.7.0
Expand All @@ -25,6 +25,7 @@ dependencies:

# for the client:
http: ">=0.13.0 <2.0.0"
http_parser: ">=4.0.0 <5.0.0"

dev_dependencies:
lints: ^4.0.0

0 comments on commit b7aed63

Please sign in to comment.