From fe6bf418cf40487b0ec2e8ffcb0e324e49525532 Mon Sep 17 00:00:00 2001 From: jld3103 Date: Tue, 14 Nov 2023 06:56:16 +0100 Subject: [PATCH 1/2] fix(dynamite): Fix application/octet-stream encoding Signed-off-by: jld3103 --- .../dynamite/lib/src/models/type_result/base.dart | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/dynamite/dynamite/lib/src/models/type_result/base.dart b/packages/dynamite/dynamite/lib/src/models/type_result/base.dart index 215f0a4c..170d8569 100644 --- a/packages/dynamite/dynamite/lib/src/models/type_result/base.dart +++ b/packages/dynamite/dynamite/lib/src/models/type_result/base.dart @@ -30,7 +30,14 @@ class TypeResultBase extends TypeResult { return '$object.toString()'; } case 'application/octet-stream': - return 'utf8.encode($object) as Uint8List'; + switch (className) { + case 'Uint8List': + return object; + case 'String': + return '(utf8.encode($object) as Uint8List)'; + default: + throw Exception('"$mimeType" can only be Uint8List or String'); + } default: throw Exception('Can not encode mime type "$mimeType"'); } From 6cd3fdfe0d1f4d711bef17a420985e61a5d2c59a Mon Sep 17 00:00:00 2001 From: jld3103 Date: Tue, 14 Nov 2023 06:56:42 +0100 Subject: [PATCH 2/2] feat(dynamite_end_to_end_test): Add request body test Signed-off-by: jld3103 --- .../lib/request_body.openapi.dart | 148 ++++++++++++++++++ .../lib/request_body.openapi.json | 44 ++++++ 2 files changed, 192 insertions(+) create mode 100644 packages/dynamite/dynamite_end_to_end_test/lib/request_body.openapi.dart create mode 100644 packages/dynamite/dynamite_end_to_end_test/lib/request_body.openapi.json diff --git a/packages/dynamite/dynamite_end_to_end_test/lib/request_body.openapi.dart b/packages/dynamite/dynamite_end_to_end_test/lib/request_body.openapi.dart new file mode 100644 index 00000000..07c67f84 --- /dev/null +++ b/packages/dynamite/dynamite_end_to_end_test/lib/request_body.openapi.dart @@ -0,0 +1,148 @@ +// ignore_for_file: camel_case_types +// ignore_for_file: discarded_futures +// ignore_for_file: public_member_api_docs +// ignore_for_file: unreachable_switch_case +import 'dart:convert'; +import 'dart:typed_data'; + +import 'package:built_value/serializer.dart'; +import 'package:built_value/standard_json_plugin.dart'; +import 'package:dynamite_runtime/built_value.dart'; +import 'package:dynamite_runtime/http_client.dart'; +import 'package:meta/meta.dart'; +import 'package:universal_io/io.dart'; + +class Client extends DynamiteClient { + Client( + super.baseURL, { + super.baseHeaders, + super.userAgent, + super.httpClient, + super.cookieJar, + }); + + Client.fromClient(final DynamiteClient client) + : super( + client.baseURL, + baseHeaders: client.baseHeaders, + httpClient: client.httpClient, + cookieJar: client.cookieJar, + authentications: client.authentications, + ); + + /// Returns a [Future] containing a [DynamiteResponse] with the status code, deserialized body and headers. + /// Throws a [DynamiteApiException] if the API call does not return an expected status code. + /// + /// Status codes: + /// * default + /// + /// See: + /// * [$getRaw] for an experimental operation that returns a [DynamiteRawResponse] that can be serialized. + Future> $get({final Uint8List? uint8List}) async { + final rawResponse = $getRaw( + uint8List: uint8List, + ); + + return rawResponse.future; + } + + /// This method and the response it returns is experimental. The API might change without a major version bump. + /// + /// Returns a [Future] containing a [DynamiteRawResponse] with the raw [HttpClientResponse] and serialization helpers. + /// Throws a [DynamiteApiException] if the API call does not return an expected status code. + /// + /// Status codes: + /// * default + /// + /// See: + /// * [$get] for an operation that returns a [DynamiteResponse] with a stable API. + @experimental + DynamiteRawResponse $getRaw({final Uint8List? uint8List}) { + final queryParameters = {}; + final headers = {}; + Uint8List? body; + + headers['Content-Type'] = 'application/octet-stream'; + if (uint8List != null) { + body = uint8List; + } + const path = '/'; + final uri = Uri(path: path, queryParameters: queryParameters.isNotEmpty ? queryParameters : null); + + return DynamiteRawResponse( + response: executeRequest( + 'get', + uri, + headers, + body, + null, + ), + bodyType: null, + headersType: null, + serializers: _jsonSerializers, + ); + } + + /// Returns a [Future] containing a [DynamiteResponse] with the status code, deserialized body and headers. + /// Throws a [DynamiteApiException] if the API call does not return an expected status code. + /// + /// Status codes: + /// * default + /// + /// See: + /// * [postRaw] for an experimental operation that returns a [DynamiteRawResponse] that can be serialized. + Future> post({final String? string}) async { + final rawResponse = postRaw( + string: string, + ); + + return rawResponse.future; + } + + /// This method and the response it returns is experimental. The API might change without a major version bump. + /// + /// Returns a [Future] containing a [DynamiteRawResponse] with the raw [HttpClientResponse] and serialization helpers. + /// Throws a [DynamiteApiException] if the API call does not return an expected status code. + /// + /// Status codes: + /// * default + /// + /// See: + /// * [post] for an operation that returns a [DynamiteResponse] with a stable API. + @experimental + DynamiteRawResponse postRaw({final String? string}) { + final queryParameters = {}; + final headers = {}; + Uint8List? body; + + headers['Content-Type'] = 'application/octet-stream'; + if (string != null) { + body = utf8.encode(string) as Uint8List; + } + const path = '/'; + final uri = Uri(path: path, queryParameters: queryParameters.isNotEmpty ? queryParameters : null); + + return DynamiteRawResponse( + response: executeRequest( + 'post', + uri, + headers, + body, + null, + ), + bodyType: null, + headersType: null, + serializers: _jsonSerializers, + ); + } +} + +// coverage:ignore-start +final Serializers _serializers = Serializers().toBuilder().build(); + +final Serializers _jsonSerializers = (_serializers.toBuilder() + ..add(DynamiteDoubleSerializer()) + ..addPlugin(StandardJsonPlugin()) + ..addPlugin(const ContentStringPlugin())) + .build(); +// coverage:ignore-end diff --git a/packages/dynamite/dynamite_end_to_end_test/lib/request_body.openapi.json b/packages/dynamite/dynamite_end_to_end_test/lib/request_body.openapi.json new file mode 100644 index 00000000..f92bf70d --- /dev/null +++ b/packages/dynamite/dynamite_end_to_end_test/lib/request_body.openapi.json @@ -0,0 +1,44 @@ +{ + "openapi": "3.1.0", + "info": { + "title": "request body test", + "version": "0.0.1" + }, + "paths": { + "/": { + "get": { + "requestBody": { + "content": { + "application/octet-stream": { + "schema": { + "type": "string", + "format": "binary" + } + } + } + }, + "responses": { + "default": { + "description": "" + } + } + }, + "post": { + "requestBody": { + "content": { + "application/octet-stream": { + "schema": { + "type": "string" + } + } + } + }, + "responses": { + "default": { + "description": "" + } + } + } + } + } +}