Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(dynamite): handle dart type names #1285

Merged
merged 1 commit into from
Dec 13, 2023
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
38 changes: 36 additions & 2 deletions packages/dynamite/dynamite/lib/src/helpers/dart_helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,43 @@ String toDartName(
}
}

if (_dartKeywords.contains(result) || RegExp(r'^[0-9]+$', multiLine: true).hasMatch(result)) {
if (_reservedNames.contains(result) || RegExp(r'^[0-9]+$', multiLine: true).hasMatch(result)) {
return '\$$result';
}

return result;
}

final _dartKeywords = [
/// Helper methods to work with strings.
extension StringUtils on String {
/// Capitalizes this string.
///
/// ```dart
/// ''.capitalize(); // ''
/// ' '.capitalize(); // ''
/// 'testValue'.capitalize(); // 'TestValue'
/// 'TestValue'.capitalize(); // 'TestValue'
///
/// ```
String capitalize() {
final trimmed = trimLeft();

if (trimmed.isEmpty) {
return this;
}

final capitalChar = trimmed[0].toUpperCase();
return trimmed.replaceRange(0, 1, capitalChar);
}
}

/// A list of dart keywords and type names that need to be escaped.
const _reservedNames = [
'abstract',
'as',
'assert',
'async',
'bool',
'break',
'case',
'catch',
Expand All @@ -37,6 +62,7 @@ final _dartKeywords = [
'default',
'deferred',
'do',
'double',
'dynamic',
'else',
'enum',
Expand All @@ -56,12 +82,17 @@ final _dartKeywords = [
'implements',
'import',
'in',
'int',
'interface',
'is',
'library',
'List',
'Map',
'mixin',
'new',
'null',
'num',
'Object',
'on',
'operator',
'part',
Expand All @@ -70,6 +101,7 @@ final _dartKeywords = [
'set',
'show',
'static',
'String',
'super',
'switch',
'sync',
Expand All @@ -78,6 +110,8 @@ final _dartKeywords = [
'true',
'try',
'typedef',
'Uint8List',
'Uri',
'var',
'void',
'while',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ abstract class TypeResultSomeOf extends TypeResult {
late final String typeName = _typeName;

String get _typeName {
final buffer = StringBuffer(r'$');
final buffer = StringBuffer();
for (final type in optimizedSubTypes) {
buffer.write(toDartName(type.className, uppercaseFirstCharacter: true));
buffer.write(type.className.capitalize());
}

return buffer.toString();
return '\$${toDartName(buffer.toString(), uppercaseFirstCharacter: true)}';
}

BuiltList<TypeResult> get _optimizedSubTypes {
Expand Down
7 changes: 7 additions & 0 deletions packages/dynamite/dynamite/test/dart_helpers_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,12 @@ void main() {
expect(toDartName(value.$1, uppercaseFirstCharacter: true), value.$3);
}
});

test('capitalize', () {
expect(''.capitalize(), '');
expect(' '.capitalize(), ' ');
expect('testValue'.capitalize(), 'TestValue');
expect('TestValue'.capitalize(), 'TestValue');
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ class Client extends DynamiteClient {
/// Parameters:
/// * [contentString]
/// * [contentParameter]
/// * [array]
/// * [$bool]
/// * [string]
/// * [stringBinary]
/// * [$int]
/// * [$double]
/// * [$num]
///
/// Status codes:
/// * 200
Expand All @@ -49,10 +56,24 @@ class Client extends DynamiteClient {
Future<DynamiteResponse<JsonObject, void>> $get({
final ContentString<BuiltMap<String, JsonObject>>? contentString,
final ContentString<BuiltMap<String, JsonObject>>? contentParameter,
final List<JsonObject>? array,
final bool? $bool,
final String? string,
final Uint8List? stringBinary,
final int? $int,
final double? $double,
final num? $num,
}) async {
final rawResponse = $getRaw(
contentString: contentString,
contentParameter: contentParameter,
array: array,
$bool: $bool,
string: string,
stringBinary: stringBinary,
$int: $int,
$double: $double,
$num: $num,
);

return rawResponse.future;
Expand All @@ -66,6 +87,13 @@ class Client extends DynamiteClient {
/// Parameters:
/// * [contentString]
/// * [contentParameter]
/// * [array]
/// * [$bool]
/// * [string]
/// * [stringBinary]
/// * [$int]
/// * [$double]
/// * [$num]
///
/// Status codes:
/// * 200
Expand All @@ -76,6 +104,13 @@ class Client extends DynamiteClient {
DynamiteRawResponse<JsonObject, void> $getRaw({
final ContentString<BuiltMap<String, JsonObject>>? contentString,
final ContentString<BuiltMap<String, JsonObject>>? contentParameter,
final List<JsonObject>? array,
final bool? $bool,
final String? string,
final Uint8List? stringBinary,
final int? $int,
final double? $double,
final num? $num,
}) {
final pathParameters = <String, dynamic>{};
final queryParameters = <String, dynamic>{};
Expand All @@ -100,6 +135,27 @@ class Client extends DynamiteClient {
]),
);
}
if (array != null) {
queryParameters['array'] = array.map((final e) => e.toString());
}
if ($bool != null) {
queryParameters['bool'] = $bool.toString();
}
if (string != null) {
queryParameters['string'] = string;
}
if (stringBinary != null) {
queryParameters['string_binary'] = stringBinary.toString();
}
if ($int != null) {
queryParameters['int'] = $int.toString();
}
if ($double != null) {
queryParameters['double'] = $double.toString();
}
if ($num != null) {
queryParameters['num'] = $num.toString();
}
var uri = Uri.parse(UriTemplate('/').expand(pathParameters));
if (queryParameters.isNotEmpty) {
uri = uri.replace(queryParameters: queryParameters);
Expand Down Expand Up @@ -188,7 +244,8 @@ final Serializers serializers = (Serializers().toBuilder()
]),
ContentStringBuilder<BuiltMap<String, JsonObject>>.new,
)
..add(ContentString.serializer))
..add(ContentString.serializer)
..addBuilderFactory(const FullType(BuiltList, [FullType(JsonObject)]), ListBuilder<JsonObject>.new))
.build();

@visibleForTesting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,57 @@
}
}
}
},
{
"name": "array",
"in": "query",
"schema": {
"type": "array"
}
},
{
"name": "bool",
"in": "query",
"schema": {
"type": "boolean"
}
},
{
"name": "string",
"in": "query",
"schema": {
"type": "string"
}
},
{
"name": "string_binary",
"in": "query",
"schema": {
"type": "string",
"format": "binary"
}
},
{
"name": "int",
"in": "query",
"schema": {
"type": "integer"
}
},
{
"name": "double",
"in": "query",
"schema": {
"type": "number",
"format": "float"
}
},
{
"name": "num",
"in": "query",
"schema": {
"type": "number"
}
}
],
"responses": {
Expand Down
12 changes: 12 additions & 0 deletions packages/dynamite/dynamite_end_to_end_test/lib/types.openapi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ class Client extends DynamiteClient {
);
}

typedef $Object = dynamic;

typedef $String = dynamic;

typedef $Uri = dynamic;

typedef $Uint8List = dynamic;

typedef $List = dynamic;

typedef $Map = dynamic;

@BuiltValue(instantiable: false)
abstract interface class $BaseInterface {
@BuiltValueField(wireName: 'bool')
Expand Down
18 changes: 18 additions & 0 deletions packages/dynamite/dynamite_end_to_end_test/lib/types.openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,24 @@
}
}
}
},
"Object": {
"type": "object"
},
"String": {
"type": "object"
},
"Uri": {
"type": "object"
},
"Uint8List": {
"type": "object"
},
"List": {
"type": "object"
},
"Map": {
"type": "object"
}
}
},
Expand Down