Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
## 1.16.1

- A route parameter typed as an `enum` no longer fails when the value arrives as
a qualified name (`Currency.brl`), the form an `enum.toString()` produces and
the one external callers tend to send:

```
type 'String' is not a subtype of type 'Currency?' of 'chargedCurrency'
```

`EnumReflection.from` only read a bare value name, so the qualified form
parsed to `null`, `APIRouteBuilder.resolveValueByType` fell back to the value
it was given, and the raw `String` reached `Function.apply`. The bare name
(`brl`), in any case, already resolved.

- deps: `reflection_factory: ^2.10.0`, which resolves the qualified `enum` name.

## 1.16.0

- Added the `cross_origin` configuration entry, grouping every cross-origin
Expand Down
2 changes: 1 addition & 1 deletion lib/src/bones_api_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ typedef APILogger =
/// Bones API Library class.
class BonesAPI {
// ignore: constant_identifier_names
static const String VERSION = '1.16.0';
static const String VERSION = '1.16.1';

static bool _boot = false;

Expand Down
2 changes: 1 addition & 1 deletion lib/src/bones_api_logging.dart
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ abstract class LoggerHandler {
}
}

MessageLogger? resolveLogDestiny(final Object? logDestiny) {
MessageLogger? resolveLogDestiny(Object? logDestiny) {
if (logDestiny == null) return null;

if (logDestiny is Map) {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/bones_api_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ mixin FieldsFromMap {
/// Resolves [fieldName] to one that matches a [fieldsNames] element.
String? resolveFiledName(
List<String> fieldsNames,
final String fieldName, {
String fieldName, {
Map<String, int>? fieldsNamesIndexes,
List<String>? fieldsNamesLC,
List<String>? fieldsNamesSimple,
Expand Down
4 changes: 2 additions & 2 deletions lib/src/bones_api_sql_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1044,8 +1044,8 @@ extension SQLBuilderListExtension on List<SQLBuilder> {
}

int _bestOrderImpl(
final Map<SQLBuilder, List<String>> entriesReferences,
final Map<SQLBuilder, List<String>> entriesRelationships,
Map<SQLBuilder, List<String>> entriesReferences,
Map<SQLBuilder, List<String>> entriesRelationships,
) {
final length = this.length;

Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: bones_api
description: Bones_API - A powerful API backend framework for Dart. It comes with a built-in HTTP Server, route handler, entity handler, SQL translator, and DB adapters.
version: 1.16.0
version: 1.16.1
homepage: https://github.com/Colossus-Services/bones_api

environment:
Expand All @@ -12,7 +12,7 @@ executables:
dependencies:
async_extension: ^1.2.22
async_events: ^1.3.0
reflection_factory: ^2.9.0
reflection_factory: ^2.10.0
statistics: ^1.2.1
swiss_knife: ^3.3.14
data_serializer: ^1.2.1
Expand Down
222 changes: 222 additions & 0 deletions test/bones_api_route_enum_parameter_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
@TestOn('vm')
import 'package:bones_api/bones_api.dart';
import 'package:test/test.dart';

part 'bones_api_route_enum_parameter_test.reflection.g.dart';

@EnableReflection()
enum PaymentType { creditCard, debitCard, pix }

@EnableReflection()
enum Currency { brl, usd, eur }

class EnumParameterAPIRoot extends APIRoot {
EnumParameterAPIRoot() : super('enum-parameter-test', '1.0');

@override
Set<APIModule> loadModules() => {ExternalIntegrationModule(this)};
}

@EnableReflection()
class ExternalIntegrationModule extends APIModule {
ExternalIntegrationModule(APIRoot apiRoot)
: super(apiRoot, 'external_integration');

@override
void configure() {
routes.anyFrom(reflection);
}

APIResponse<Map> updateOrderStatusFromBroker(
int orderId,
String status,
bool paid,
PaymentType? paymentType,
Currency? chargedCurrency,
double? chargedPrice,
) => APIResponse.ok({
'orderId': orderId,
'status': status,
'paid': paid,
'paymentType': paymentType?.name,
'chargedCurrency': chargedCurrency?.name,
'chargedPrice': chargedPrice,
});
}

void main() {
group('APIRoute enum parameter', () {
setUpAll(() {
ExternalIntegrationModule$reflection.boot();
});

test('enum parameter from `String` (enum name)', () async {
var apiRoot = EnumParameterAPIRoot();

var response = await apiRoot.call(
APIRequest.post(
'/external_integration/updateOrderStatusFromBroker',
parameters: {
'orderId': 198945,
'status': 'canceled',
'paid': false,
'paymentType': 'creditCard',
'chargedCurrency': 'brl',
'chargedPrice': 12.89,
},
),
);

expect(response.error, isNull, reason: '${response.error}');

expect(
response.payload,
equals({
'orderId': 198945,
'status': 'canceled',
'paid': false,
'paymentType': 'creditCard',
'chargedCurrency': 'brl',
'chargedPrice': 12.89,
}),
);

apiRoot.close();
});

test('enum parameter from `String` (qualified enum name)', () async {
var apiRoot = EnumParameterAPIRoot();

var response = await apiRoot.call(
APIRequest.post(
'/external_integration/updateOrderStatusFromBroker',
parameters: {
'orderId': 198945,
'status': 'canceled',
'paid': false,
'paymentType': 'PaymentType.creditCard',
'chargedCurrency': 'Currency.brl',
'chargedPrice': 12.89,
},
),
);

expect(response.error, isNull, reason: '${response.error}');

expect(
response.payload,
equals({
'orderId': 198945,
'status': 'canceled',
'paid': false,
'paymentType': 'creditCard',
'chargedCurrency': 'brl',
'chargedPrice': 12.89,
}),
);

apiRoot.close();
});

test('enum parameter from `String` (case insensitive)', () async {
var apiRoot = EnumParameterAPIRoot();

var response = await apiRoot.call(
APIRequest.post(
'/external_integration/updateOrderStatusFromBroker',
parameters: {
'orderId': 198945,
'status': 'canceled',
'paid': false,
'paymentType': 'CreditCard',
'chargedCurrency': 'BRL',
'chargedPrice': 12.89,
},
),
);

expect(response.error, isNull, reason: '${response.error}');

expect(
response.payload,
equals({
'orderId': 198945,
'status': 'canceled',
'paid': false,
'paymentType': 'creditCard',
'chargedCurrency': 'brl',
'chargedPrice': 12.89,
}),
);

apiRoot.close();
});

test('enum parameter from JSON payload', () async {
var apiRoot = EnumParameterAPIRoot();

var response = await apiRoot.call(
APIRequest.post(
'/external_integration/updateOrderStatusFromBroker',
payload: {
'orderId': 198945,
'status': 'canceled',
'paid': false,
'paymentType': 'creditCard',
'chargedCurrency': 'brl',
'chargedPrice': 12.89,
},
payloadMimeType: 'json',
),
);

expect(response.error, isNull, reason: '${response.error}');

expect(
response.payload,
equals({
'orderId': 198945,
'status': 'canceled',
'paid': false,
'paymentType': 'creditCard',
'chargedCurrency': 'brl',
'chargedPrice': 12.89,
}),
);

apiRoot.close();
});

test('null enum parameter', () async {
var apiRoot = EnumParameterAPIRoot();

var response = await apiRoot.call(
APIRequest.post(
'/external_integration/updateOrderStatusFromBroker',
parameters: {
'orderId': 198945,
'status': 'canceled',
'paid': false,
'chargedPrice': 12.89,
},
),
);

expect(response.error, isNull, reason: '${response.error}');

expect(
response.payload,
equals({
'orderId': 198945,
'status': 'canceled',
'paid': false,
'paymentType': null,
'chargedCurrency': null,
'chargedPrice': 12.89,
}),
);

apiRoot.close();
});
});
}
Loading