From 97f226bbed414d884bd7456ab1d1712c86589368 Mon Sep 17 00:00:00 2001 From: jld3103 Date: Mon, 19 Dec 2022 19:20:44 +0100 Subject: [PATCH] dynamite: Implement urlencoded requests --- packages/dynamite/lib/src/openapi_builder.dart | 9 ++++++++- packages/dynamite/lib/src/type_result/base.dart | 6 +++++- packages/dynamite/lib/src/type_result/enum.dart | 7 ++++++- packages/dynamite/lib/src/type_result/list.dart | 16 ++++++++++++++-- packages/dynamite/lib/src/type_result/map.dart | 15 ++++++++++++++- .../dynamite/lib/src/type_result/object.dart | 15 ++++++++++++++- .../lib/src/type_result/type_result.dart | 6 +++++- 7 files changed, 66 insertions(+), 8 deletions(-) diff --git a/packages/dynamite/lib/src/openapi_builder.dart b/packages/dynamite/lib/src/openapi_builder.dart index e0cdd428..06d445bf 100644 --- a/packages/dynamite/lib/src/openapi_builder.dart +++ b/packages/dynamite/lib/src/openapi_builder.dart @@ -606,6 +606,8 @@ class OpenAPIBuilder implements Builder { final value = result.encode( result.serialize(_toDartName(parameter.name)), onlyChildren: isPlainList && parameter.in_ == 'query', + // Objects inside the query always have to be interpreted in some way + mimeType: 'application/json', ); if (defaultValueCode != null && parameter.in_ != 'path') { code.write('if (${_toDartName(parameter.name)} != $defaultValueCode) {'); @@ -654,6 +656,7 @@ class OpenAPIBuilder implements Builder { ); switch (mimeType) { case 'application/json': + case 'application/x-www-form-urlencoded': final nullable = _isParameterNullable( operation.requestBody!.required, mediaType.schema?.default_, @@ -672,7 +675,7 @@ class OpenAPIBuilder implements Builder { code.write('if (${_toDartName(result.name)} != null) {'); } code.write( - 'body = Uint8List.fromList(utf8.encode(${result.encode(result.serialize(_toDartName(result.name)))}));', + 'body = Uint8List.fromList(utf8.encode(${result.encode(result.serialize(_toDartName(result.name)), mimeType: mimeType)}));', ); if (nullable) { code.write('}'); @@ -773,6 +776,10 @@ class OpenAPIBuilder implements Builder { dataType = 'Uint8List'; dataValue = 'response.body'; break; + case 'text/plain': + dataType = 'String'; + dataValue = 'utf8.decode(response.body)'; + break; default: throw Exception('Can not parse mime type "$mimeType"'); } diff --git a/packages/dynamite/lib/src/type_result/base.dart b/packages/dynamite/lib/src/type_result/base.dart index 4446013a..54fcc9e5 100644 --- a/packages/dynamite/lib/src/type_result/base.dart +++ b/packages/dynamite/lib/src/type_result/base.dart @@ -7,7 +7,11 @@ class TypeResultBase extends TypeResult { String serialize(final String object) => object; @override - String encode(final String object, {final bool onlyChildren = false}) => + String encode( + final String object, { + final bool onlyChildren = false, + final String? mimeType, + }) => name == 'String' ? object : '$object.toString()'; @override diff --git a/packages/dynamite/lib/src/type_result/enum.dart b/packages/dynamite/lib/src/type_result/enum.dart index b5b85880..dbbb25e2 100644 --- a/packages/dynamite/lib/src/type_result/enum.dart +++ b/packages/dynamite/lib/src/type_result/enum.dart @@ -12,7 +12,12 @@ class TypeResultEnum extends TypeResult { String serialize(final String object) => '$object.value'; @override - String encode(final String object, {final bool onlyChildren = false}) => subType.encode(object); + String encode( + final String object, { + final bool onlyChildren = false, + final String? mimeType, + }) => + subType.encode(object); @override String deserialize(final String object) => '$name.fromValue($object as ${subType.name})'; diff --git a/packages/dynamite/lib/src/type_result/list.dart b/packages/dynamite/lib/src/type_result/list.dart index d59b4155..2208a834 100644 --- a/packages/dynamite/lib/src/type_result/list.dart +++ b/packages/dynamite/lib/src/type_result/list.dart @@ -14,11 +14,23 @@ class TypeResultList extends TypeResult { String serialize(final String object) => '$object.map((final e) => ${subType.serialize('e')}).toList()'; @override - String encode(final String object, {final bool onlyChildren = false}) { + String encode( + final String object, { + final bool onlyChildren = false, + final String? mimeType, + }) { if (onlyChildren) { return '$object.map((final e) => ${subType.encode('e')}).toList()'; } - return 'json.encode($object)'; + + switch (mimeType) { + case 'application/json': + return 'json.encode($object)'; + case 'application/x-www-form-urlencoded': + return 'Uri(queryParameters: $object).query'; + default: + throw Exception('Can not encode mime type "$mimeType"'); + } } @override diff --git a/packages/dynamite/lib/src/type_result/map.dart b/packages/dynamite/lib/src/type_result/map.dart index b0f24027..6f4da86f 100644 --- a/packages/dynamite/lib/src/type_result/map.dart +++ b/packages/dynamite/lib/src/type_result/map.dart @@ -12,7 +12,20 @@ class TypeResultMap extends TypeResult { String serialize(final String object) => object; @override - String encode(final String object, {final bool onlyChildren = false}) => 'json.encode($object)'; + String encode( + final String object, { + final bool onlyChildren = false, + final String? mimeType, + }) { + switch (mimeType) { + case 'application/json': + return 'json.encode($object)'; + case 'application/x-www-form-urlencoded': + return 'Uri(queryParameters: $object).query'; + default: + throw Exception('Can not encode mime type "$mimeType"'); + } + } @override String deserialize(final String object) => '($object as Map)'; diff --git a/packages/dynamite/lib/src/type_result/object.dart b/packages/dynamite/lib/src/type_result/object.dart index 0b556af3..40f09d62 100644 --- a/packages/dynamite/lib/src/type_result/object.dart +++ b/packages/dynamite/lib/src/type_result/object.dart @@ -17,7 +17,20 @@ class TypeResultObject extends TypeResult { } @override - String encode(final String object, {final bool onlyChildren = false}) => 'json.encode($object)'; + String encode( + final String object, { + final bool onlyChildren = false, + final String? mimeType, + }) { + switch (mimeType) { + case 'application/json': + return 'json.encode($object)'; + case 'application/x-www-form-urlencoded': + return 'Uri(queryParameters: $object).query'; + default: + throw Exception('Can not encode mime type "$mimeType"'); + } + } @override String deserialize(final String object) { diff --git a/packages/dynamite/lib/src/type_result/type_result.dart b/packages/dynamite/lib/src/type_result/type_result.dart index 1ceb5992..393a3428 100644 --- a/packages/dynamite/lib/src/type_result/type_result.dart +++ b/packages/dynamite/lib/src/type_result/type_result.dart @@ -11,5 +11,9 @@ abstract class TypeResult { String decode(final String object); - String encode(final String object, {final bool onlyChildren = false}); + String encode( + final String object, { + final bool onlyChildren = false, + final String? mimeType, + }); }