Skip to content

Commit

Permalink
refactor(dynamite_runtime)!: validate status code in the dynamite ser…
Browse files Browse the repository at this point in the history
…ializer

Signed-off-by: Nikolas Rimikis <leptopoda@users.noreply.github.com>
  • Loading branch information
Leptopoda committed Mar 8, 2024
1 parent 26541d7 commit bc6f7d9
Show file tree
Hide file tree
Showing 34 changed files with 410 additions and 612 deletions.
8 changes: 4 additions & 4 deletions packages/dynamite/dynamite/example/lib/petstore.openapi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class $Client extends _i1.DynamiteClient {
bodyType: const FullType(BuiltList, [FullType(Pet)]),
headersType: null,
serializers: _$jsonSerializers,
validStatuses: const {200},
);

/// Returns all pets from the system that the user has access to.
Expand Down Expand Up @@ -133,7 +134,6 @@ class $Client extends _i1.DynamiteClient {
'get',
_path,
headers: _headers,
validStatuses: const {200},
);

final _serializer = $findPets_Serializer();
Expand All @@ -146,6 +146,7 @@ class $Client extends _i1.DynamiteClient {
bodyType: const FullType(Pet),
headersType: null,
serializers: _$jsonSerializers,
validStatuses: const {200},
);

/// Creates a new pet in the store. Duplicates are allowed.
Expand Down Expand Up @@ -193,7 +194,6 @@ class $Client extends _i1.DynamiteClient {
_path,
headers: _headers,
body: _body,
validStatuses: const {200},
);

final _serializer = $addPet_Serializer();
Expand All @@ -206,6 +206,7 @@ class $Client extends _i1.DynamiteClient {
bodyType: const FullType(Pet),
headersType: null,
serializers: _$jsonSerializers,
validStatuses: const {200},
);

/// Returns a user based on a single ID, if the user does not have access to the pet.
Expand Down Expand Up @@ -259,7 +260,6 @@ class $Client extends _i1.DynamiteClient {
'get',
_path,
headers: _headers,
validStatuses: const {200},
);

final _serializer = $findPetById_Serializer();
Expand All @@ -272,6 +272,7 @@ class $Client extends _i1.DynamiteClient {
bodyType: null,
headersType: null,
serializers: _$jsonSerializers,
validStatuses: const {204},
);

/// deletes a single pet based on the ID supplied.
Expand Down Expand Up @@ -323,7 +324,6 @@ class $Client extends _i1.DynamiteClient {
final _response = await executeRequest(
'delete',
_path,
validStatuses: const {204},
);

final _serializer = $deletePet_Serializer();
Expand Down
21 changes: 13 additions & 8 deletions packages/dynamite/dynamite/lib/src/builder/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -323,15 +323,23 @@ Iterable<Method> buildTags(
b
..returns = returnType
..lambda = true
..body = Code.scope(
(allocate) => '''
..body = Code.scope((allocate) {
final code = StringBuffer('''
${allocate(returnType)}(
bodyType: ${bodyType.fullType ?? 'null'},
headersType: ${headersType.fullType ?? 'null'},
serializers: _\$jsonSerializers,
)
''',
);
''');

final statusCodes = responseEntry.value;
if (responses.values.isNotEmpty && !statusCodes.contains('default')) {
code.writeln("validStatuses: const {${statusCodes.join(',')}},");
}

code.writeln(')');

return code.toString();
});
});

yield Method((b) {
Expand Down Expand Up @@ -466,16 +474,13 @@ return ${allocate(responseType)}.fromRawResponse(_rawResponse);

buildUrlPath(pathEntry.key, parameters, code, state.emitter.allocator.allocate);

final statusCodes = responseEntry.value;
final requestExpression = refer(client).property('executeRequest').call([
literalString(httpMethod),
refer('_path'),
], {
if (acceptHeader.isNotEmpty || hasHeaderParameters || hasAuthentication || hasContentEncoding)
'headers': refer('_headers'),
if (operation.requestBody != null) 'body': refer('_body'),
if (responses.values.isNotEmpty && !statusCodes.contains('default'))
'validStatuses': literalConstSet(statusCodes),
});

final responseConverterType = refer(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class $Client extends _i1.DynamiteClient {
bodyType: const FullType(JsonObject),
headersType: null,
serializers: _$jsonSerializers,
validStatuses: const {200},
);

/// Returns a [Future] containing a `DynamiteResponse` with the status code, deserialized body and headers.
Expand Down Expand Up @@ -77,7 +78,6 @@ class $Client extends _i1.DynamiteClient {
'get',
_path,
headers: _headers,
validStatuses: const {200},
);

final _serializer = $noAuthentication_Serializer();
Expand All @@ -90,6 +90,7 @@ class $Client extends _i1.DynamiteClient {
bodyType: const FullType(JsonObject),
headersType: null,
serializers: _$jsonSerializers,
validStatuses: const {200},
);

/// Returns a [Future] containing a `DynamiteResponse` with the status code, deserialized body and headers.
Expand Down Expand Up @@ -142,7 +143,6 @@ class $Client extends _i1.DynamiteClient {
'get',
_path,
headers: _headers,
validStatuses: const {200},
);

final _serializer = $basicAuthentication_Serializer();
Expand All @@ -155,6 +155,7 @@ class $Client extends _i1.DynamiteClient {
bodyType: const FullType(JsonObject),
headersType: null,
serializers: _$jsonSerializers,
validStatuses: const {200},
);

/// Returns a [Future] containing a `DynamiteResponse` with the status code, deserialized body and headers.
Expand Down Expand Up @@ -207,7 +208,6 @@ class $Client extends _i1.DynamiteClient {
'get',
_path,
headers: _headers,
validStatuses: const {200},
);

final _serializer = $bearerAuthentication_Serializer();
Expand All @@ -220,6 +220,7 @@ class $Client extends _i1.DynamiteClient {
bodyType: const FullType(JsonObject),
headersType: null,
serializers: _$jsonSerializers,
validStatuses: const {200},
);

/// Returns a [Future] containing a `DynamiteResponse` with the status code, deserialized body and headers.
Expand Down Expand Up @@ -272,7 +273,6 @@ class $Client extends _i1.DynamiteClient {
'get',
_path,
headers: _headers,
validStatuses: const {200},
);

final _serializer = $multipleAuthentications_Serializer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class $Client extends _i1.DynamiteClient {
bodyType: null,
headersType: const FullType(GetHeaders),
serializers: _$jsonSerializers,
validStatuses: const {200},
);

/// Returns a [Future] containing a `DynamiteResponse` with the status code, deserialized body and headers.
Expand Down Expand Up @@ -76,7 +77,6 @@ class $Client extends _i1.DynamiteClient {
final _response = await executeRequest(
'get',
_path,
validStatuses: const {200},
);

final _serializer = $$get_Serializer();
Expand All @@ -90,6 +90,7 @@ class $Client extends _i1.DynamiteClient {
bodyType: null,
headersType: const FullType(WithContentOperationIdHeaders),
serializers: _$jsonSerializers,
validStatuses: const {200},
);

/// Returns a [Future] containing a `DynamiteResponse` with the status code, deserialized body and headers.
Expand Down Expand Up @@ -122,7 +123,6 @@ class $Client extends _i1.DynamiteClient {
final _response = await executeRequest(
'get',
_path,
validStatuses: const {200},
);

final _serializer = $withContentOperationId_Serializer();
Expand All @@ -135,6 +135,7 @@ class $Client extends _i1.DynamiteClient {
bodyType: const FullType(Uint8List),
headersType: const FullType(GetWithContentHeaders),
serializers: _$jsonSerializers,
validStatuses: const {200},
);

/// Returns a [Future] containing a `DynamiteResponse` with the status code, deserialized body and headers.
Expand Down Expand Up @@ -170,7 +171,6 @@ class $Client extends _i1.DynamiteClient {
'get',
_path,
headers: _headers,
validStatuses: const {200},
);

final _serializer = $getWithContent_Serializer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class $Client extends _i1.DynamiteClient {
bodyType: const FullType(JsonObject),
headersType: null,
serializers: _$jsonSerializers,
validStatuses: const {200},
);

/// Returns a [Future] containing a `DynamiteResponse` with the status code, deserialized body and headers.
Expand Down Expand Up @@ -222,7 +223,6 @@ class $Client extends _i1.DynamiteClient {
'get',
_path,
headers: _headers,
validStatuses: const {200},
);

final _serializer = $$get_Serializer();
Expand All @@ -235,6 +235,7 @@ class $Client extends _i1.DynamiteClient {
bodyType: const FullType(JsonObject),
headersType: null,
serializers: _$jsonSerializers,
validStatuses: const {200},
);

/// Returns a [Future] containing a `DynamiteResponse` with the status code, deserialized body and headers.
Expand Down Expand Up @@ -433,7 +434,6 @@ class $Client extends _i1.DynamiteClient {
'get',
_path,
headers: _headers,
validStatuses: const {200},
);

final _serializer = $getHeaders_Serializer();
Expand All @@ -446,6 +446,7 @@ class $Client extends _i1.DynamiteClient {
bodyType: const FullType(JsonObject),
headersType: null,
serializers: _$jsonSerializers,
validStatuses: const {200},
);

/// Returns a [Future] containing a `DynamiteResponse` with the status code, deserialized body and headers.
Expand Down Expand Up @@ -487,7 +488,6 @@ class $Client extends _i1.DynamiteClient {
'get',
_path,
headers: _headers,
validStatuses: const {200},
);

final _serializer = $getPathParameter_Serializer();
Expand All @@ -500,6 +500,7 @@ class $Client extends _i1.DynamiteClient {
bodyType: const FullType(JsonObject),
headersType: null,
serializers: _$jsonSerializers,
validStatuses: const {200},
);

/// Returns a [Future] containing a `DynamiteResponse` with the status code, deserialized body and headers.
Expand Down Expand Up @@ -577,7 +578,6 @@ class $Client extends _i1.DynamiteClient {
'get',
_path,
headers: _headers,
validStatuses: const {200},
);

final _serializer = $getNamingCollisions_Serializer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ class $Client extends _i1.DynamiteClient {
bodyType: const FullType(String),
headersType: null,
serializers: _$jsonSerializers,
validStatuses: const {200},
);

/// Returns a [Future] containing a `DynamiteResponse` with the status code, deserialized body and headers.
Expand Down Expand Up @@ -123,7 +124,6 @@ class $Client extends _i1.DynamiteClient {
'put',
_path,
headers: _headers,
validStatuses: const {200},
);

final _serializer = $$put_Serializer();
Expand Down Expand Up @@ -187,6 +187,7 @@ class $Client extends _i1.DynamiteClient {
bodyType: const FullType(String),
headersType: null,
serializers: _$jsonSerializers,
validStatuses: const {200, 201},
);

/// Returns a [Future] containing a `DynamiteResponse` with the status code, deserialized body and headers.
Expand Down Expand Up @@ -224,10 +225,6 @@ class $Client extends _i1.DynamiteClient {
'patch',
_path,
headers: _headers,
validStatuses: const {
200,
201,
},
);

final _serializer = $$patch_Serializer();
Expand Down
17 changes: 1 addition & 16 deletions packages/dynamite/dynamite_runtime/lib/src/client/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import 'dart:typed_data';

import 'package:cookie_jar/cookie_jar.dart';
import 'package:dynamite_runtime/http_client.dart';
import 'package:dynamite_runtime/src/utils/debug_mode.dart';
import 'package:dynamite_runtime/src/utils/uri.dart';
import 'package:http/http.dart' as http;
import 'package:meta/meta.dart';
Expand Down Expand Up @@ -64,7 +63,6 @@ class DynamiteClient with http.BaseClient {
String path, {
Map<String, String>? headers,
Uint8List? body,
Set<int>? validStatuses,
}) {
final uri = Uri.parse('$baseURL$path');

Expand All @@ -73,7 +71,6 @@ class DynamiteClient with http.BaseClient {
uri,
headers: headers,
body: body,
validStatuses: validStatuses,
);
}

Expand All @@ -83,7 +80,6 @@ class DynamiteClient with http.BaseClient {
Uri uri, {
Map<String, String>? headers,
Uint8List? body,
Set<int>? validStatuses,
}) async {
final request = http.Request(method, uri);

Expand All @@ -95,18 +91,7 @@ class DynamiteClient with http.BaseClient {
request.bodyBytes = body;
}

final response = await sendWithCookies(request);

if (validStatuses?.contains(response.statusCode) ?? true) {
return response;
} else {
if (kDebugMode) {
final result = await http.Response.fromStream(response);
throw DynamiteStatusCodeException.fromResponse(result);
} else {
throw DynamiteStatusCodeException(response.statusCode);
}
}
return sendWithCookies(request);
}

/// Sends an HTTP request and asynchronously returns the response.
Expand Down

0 comments on commit bc6f7d9

Please sign in to comment.