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): too long names for someOf typedefs #1350

Merged
merged 1 commit into from
Dec 25, 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
1 change: 1 addition & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"**.mocks.dart",
"**/assets",
"**/l10n/!(en.arb)",
"**.openapi.dart",
"external",
"packages/dynamite/dynamite_petstore_example/lib",
"packages/file_icons/lib/src/data.dart",
Expand Down
57 changes: 49 additions & 8 deletions packages/dynamite/dynamite/lib/src/builder/generate_ofs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:dynamite/src/builder/state.dart';
import 'package:dynamite/src/helpers/dart_helpers.dart';
import 'package:dynamite/src/models/openapi.dart' as openapi;
import 'package:dynamite/src/models/type_result.dart';
import 'package:source_helper/source_helper.dart';

Iterable<Spec> buildOfsExtensions(
final openapi.OpenAPI spec,
Expand All @@ -20,6 +21,42 @@ Iterable<Spec> buildOfsExtensions(
..name = result.className
..definition = refer(result.dartType.name);
});

final serializerMethod = Method(
(final b) => b
..static = true
..returns = refer('Serializer<${result.className}>')
..type = MethodType.getter
..name = 'serializer'
..lambda = true
..body = Code('${result.typeName}Extension._serializer'),
);

final toJson = Method(
(final b) => b
..static = true
..returns = refer(result.className)
..name = 'fromJson'
..requiredParameters.add(
Parameter(
(final b) => b
..name = 'json'
..type = refer('Object?'),
),
)
..lambda = true
..body = Code('${result.typeName}Extension._fromJson(json)'),
);

yield Extension(
(final b) => b
..name = '\$${result.className}Extension'
..on = refer(result.className)
..methods.addAll([
serializerMethod,
toJson,
]),
);
}

for (final result in state.uniqueSomeOfTypes) {
Expand All @@ -29,7 +66,7 @@ Iterable<Spec> buildOfsExtensions(

yield TypeDef((final b) {
b
..name = result.typeName
..name = '_${result.typeName}'
..definition = refer(result.dartType.name);
});

Expand All @@ -40,9 +77,9 @@ Iterable<Spec> buildOfsExtensions(
Iterable<Spec> generateSomeOf(
final TypeResultSomeOf result,
) sync* {
final identifier = result.typeName;
final identifier = '_${result.typeName}';
final results = result.optimizedSubTypes;
final serializerName = '_${identifier}Serializer';
final serializerName = '${identifier}Serializer';

final fields = <TypeResult, String>{};
for (final result in results) {
Expand Down Expand Up @@ -81,7 +118,7 @@ Iterable<Spec> generateSomeOf(
..static = true
..returns = refer('Serializer<$identifier>')
..type = MethodType.getter
..name = 'serializer'
..name = '_serializer'
..lambda = true
..body = Code('const $serializerName()'),
);
Expand All @@ -90,7 +127,7 @@ Iterable<Spec> generateSomeOf(
(final b) => b
..static = true
..returns = refer(identifier)
..name = 'fromJson'
..name = '_fromJson'
..requiredParameters.add(
Parameter(
(final b) => b
Expand All @@ -99,21 +136,25 @@ Iterable<Spec> generateSomeOf(
),
)
..lambda = true
..body = const Code('jsonSerializers.deserializeWith(serializer, json)!'),
..body = const Code('jsonSerializers.deserializeWith(_serializer, json)!'),
);

final toJson = Method(
(final b) => b
..name = 'toJson'
..returns = refer('Object?')
..lambda = true
..body = const Code('jsonSerializers.serializeWith(serializer, this)'),
..body = const Code('jsonSerializers.serializeWith(_serializer, this)'),
);

yield Extension(
(final b) => b
..name = '${identifier}Extension'
..name = '${identifier}Extension'.nonPrivate
..on = refer(identifier)
..docs.addAll([
'/// @nodoc',
'// ignore: library_private_types_in_public_api',
])
..methods.addAll([
values,
oneOfValidator,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ abstract class TypeResultSomeOf extends TypeResult {
return null;
}

return '..add(${typeName}Extension.serializer)';
return '..add(${typeName}Extension._serializer)';
}

late final BuiltSet<TypeResult> optimizedSubTypes = _optimizedSubTypes.toBuiltSet();
Expand All @@ -50,15 +50,15 @@ abstract class TypeResultSomeOf extends TypeResult {
);
}

late final String typeName = _typeName;
late final String typeName = '\$${md5.convert(utf8.encode(_typeName))}';

String get _typeName {
final buffer = StringBuffer();
for (final type in optimizedSubTypes) {
buffer.write(type.className.capitalize());
}

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

BuiltList<TypeResult> get _optimizedSubTypes {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import 'dart:convert';

import 'package:built_collection/built_collection.dart';
import 'package:crypto/crypto.dart';
import 'package:dynamite/src/helpers/dart_helpers.dart';
import 'package:meta/meta.dart';

Expand Down
1 change: 1 addition & 0 deletions packages/dynamite/dynamite/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ dependencies:
checked_yaml: ^2.0.0
code_builder: ^4.5.0
collection: ^1.0.0
crypto: ^3.0.0
dart_style: ^2.0.0
intersperse: ^2.0.0
meta: ^1.0.0
Expand Down
1 change: 1 addition & 0 deletions packages/dynamite/dynamite_end_to_end_test/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ targets:
pageWidth: 120
analyzer_ignores:
- camel_case_types
- camel_case_extensions
- discarded_futures
- public_member_api_docs
- unreachable_switch_case
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ignore_for_file: camel_case_types, discarded_futures
// ignore_for_file: camel_case_extensions, camel_case_types, discarded_futures
// ignore_for_file: no_leading_underscores_for_local_identifiers
// ignore_for_file: public_member_api_docs, unreachable_switch_case
// ignore_for_file: unused_element
Expand Down
104 changes: 67 additions & 37 deletions packages/dynamite/dynamite_end_to_end_test/lib/any_of.openapi.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ignore_for_file: camel_case_types, discarded_futures
// ignore_for_file: camel_case_extensions, camel_case_types, discarded_futures
// ignore_for_file: no_leading_underscores_for_local_identifiers
// ignore_for_file: public_member_api_docs, unreachable_switch_case
// ignore_for_file: unused_element
Expand Down Expand Up @@ -92,32 +92,54 @@ abstract class OneObjectAnyOf0 implements $OneObjectAnyOf0Interface, Built<OneOb
}

typedef ObjectAnyOf = ({ObjectAnyOf0? objectAnyOf0, ObjectAnyOf1? objectAnyOf1});

extension $ObjectAnyOfExtension on ObjectAnyOf {
static Serializer<ObjectAnyOf> get serializer => $0c9017d9a03ba2eb2f15acadeab85bbeExtension._serializer;
static ObjectAnyOf fromJson(Object? json) => $0c9017d9a03ba2eb2f15acadeab85bbeExtension._fromJson(json);
}

typedef MixedAnyOf = ({MixedAnyOf1? mixedAnyOf1, String? string});

extension $MixedAnyOfExtension on MixedAnyOf {
static Serializer<MixedAnyOf> get serializer => $fba45e085ee99d64c5141852d4323e3dExtension._serializer;
static MixedAnyOf fromJson(Object? json) => $fba45e085ee99d64c5141852d4323e3dExtension._fromJson(json);
}

typedef AnyOfIntDoubleOther = ({num? $num, String? string});
typedef $ObjectAnyOf0ObjectAnyOf1 = ({ObjectAnyOf0? objectAnyOf0, ObjectAnyOf1? objectAnyOf1});

extension $ObjectAnyOf0ObjectAnyOf1Extension on $ObjectAnyOf0ObjectAnyOf1 {
extension $AnyOfIntDoubleOtherExtension on AnyOfIntDoubleOther {
static Serializer<AnyOfIntDoubleOther> get serializer => $b6d67dc2a96424d2f407f8e51557f3deExtension._serializer;
static AnyOfIntDoubleOther fromJson(Object? json) => $b6d67dc2a96424d2f407f8e51557f3deExtension._fromJson(json);
}

typedef _$0c9017d9a03ba2eb2f15acadeab85bbe = ({ObjectAnyOf0? objectAnyOf0, ObjectAnyOf1? objectAnyOf1});

/// @nodoc
// ignore: library_private_types_in_public_api
extension $0c9017d9a03ba2eb2f15acadeab85bbeExtension on _$0c9017d9a03ba2eb2f15acadeab85bbe {
List<dynamic> get _values => [objectAnyOf0, objectAnyOf1];
void validateOneOf() => dynamite_utils.validateOneOf(_values);
void validateAnyOf() => dynamite_utils.validateAnyOf(_values);
static Serializer<$ObjectAnyOf0ObjectAnyOf1> get serializer => const _$ObjectAnyOf0ObjectAnyOf1Serializer();
static $ObjectAnyOf0ObjectAnyOf1 fromJson(Object? json) => jsonSerializers.deserializeWith(serializer, json)!;
Object? toJson() => jsonSerializers.serializeWith(serializer, this);
static Serializer<_$0c9017d9a03ba2eb2f15acadeab85bbe> get _serializer =>
const _$0c9017d9a03ba2eb2f15acadeab85bbeSerializer();
static _$0c9017d9a03ba2eb2f15acadeab85bbe _fromJson(Object? json) =>
jsonSerializers.deserializeWith(_serializer, json)!;
Object? toJson() => jsonSerializers.serializeWith(_serializer, this);
}

class _$ObjectAnyOf0ObjectAnyOf1Serializer implements PrimitiveSerializer<$ObjectAnyOf0ObjectAnyOf1> {
const _$ObjectAnyOf0ObjectAnyOf1Serializer();
class _$0c9017d9a03ba2eb2f15acadeab85bbeSerializer implements PrimitiveSerializer<_$0c9017d9a03ba2eb2f15acadeab85bbe> {
const _$0c9017d9a03ba2eb2f15acadeab85bbeSerializer();

@override
Iterable<Type> get types => const [$ObjectAnyOf0ObjectAnyOf1];
Iterable<Type> get types => const [_$0c9017d9a03ba2eb2f15acadeab85bbe];

@override
String get wireName => r'$ObjectAnyOf0ObjectAnyOf1';
String get wireName => r'_$0c9017d9a03ba2eb2f15acadeab85bbe';

@override
Object serialize(
Serializers serializers,
$ObjectAnyOf0ObjectAnyOf1 object, {
_$0c9017d9a03ba2eb2f15acadeab85bbe object, {
FullType specifiedType = FullType.unspecified,
}) {
dynamic value;
Expand All @@ -134,7 +156,7 @@ class _$ObjectAnyOf0ObjectAnyOf1Serializer implements PrimitiveSerializer<$Objec
}

@override
$ObjectAnyOf0ObjectAnyOf1 deserialize(
_$0c9017d9a03ba2eb2f15acadeab85bbe deserialize(
Serializers serializers,
Object data, {
FullType specifiedType = FullType.unspecified,
Expand All @@ -151,30 +173,34 @@ class _$ObjectAnyOf0ObjectAnyOf1Serializer implements PrimitiveSerializer<$Objec
}
}

typedef $MixedAnyOf1String = ({MixedAnyOf1? mixedAnyOf1, String? string});
typedef _$fba45e085ee99d64c5141852d4323e3d = ({MixedAnyOf1? mixedAnyOf1, String? string});

extension $MixedAnyOf1StringExtension on $MixedAnyOf1String {
/// @nodoc
// ignore: library_private_types_in_public_api
extension $fba45e085ee99d64c5141852d4323e3dExtension on _$fba45e085ee99d64c5141852d4323e3d {
List<dynamic> get _values => [mixedAnyOf1, string];
void validateOneOf() => dynamite_utils.validateOneOf(_values);
void validateAnyOf() => dynamite_utils.validateAnyOf(_values);
static Serializer<$MixedAnyOf1String> get serializer => const _$MixedAnyOf1StringSerializer();
static $MixedAnyOf1String fromJson(Object? json) => jsonSerializers.deserializeWith(serializer, json)!;
Object? toJson() => jsonSerializers.serializeWith(serializer, this);
static Serializer<_$fba45e085ee99d64c5141852d4323e3d> get _serializer =>
const _$fba45e085ee99d64c5141852d4323e3dSerializer();
static _$fba45e085ee99d64c5141852d4323e3d _fromJson(Object? json) =>
jsonSerializers.deserializeWith(_serializer, json)!;
Object? toJson() => jsonSerializers.serializeWith(_serializer, this);
}

class _$MixedAnyOf1StringSerializer implements PrimitiveSerializer<$MixedAnyOf1String> {
const _$MixedAnyOf1StringSerializer();
class _$fba45e085ee99d64c5141852d4323e3dSerializer implements PrimitiveSerializer<_$fba45e085ee99d64c5141852d4323e3d> {
const _$fba45e085ee99d64c5141852d4323e3dSerializer();

@override
Iterable<Type> get types => const [$MixedAnyOf1String];
Iterable<Type> get types => const [_$fba45e085ee99d64c5141852d4323e3d];

@override
String get wireName => r'$MixedAnyOf1String';
String get wireName => r'_$fba45e085ee99d64c5141852d4323e3d';

@override
Object serialize(
Serializers serializers,
$MixedAnyOf1String object, {
_$fba45e085ee99d64c5141852d4323e3d object, {
FullType specifiedType = FullType.unspecified,
}) {
dynamic value;
Expand All @@ -191,7 +217,7 @@ class _$MixedAnyOf1StringSerializer implements PrimitiveSerializer<$MixedAnyOf1S
}

@override
$MixedAnyOf1String deserialize(
_$fba45e085ee99d64c5141852d4323e3d deserialize(
Serializers serializers,
Object data, {
FullType specifiedType = FullType.unspecified,
Expand All @@ -208,30 +234,34 @@ class _$MixedAnyOf1StringSerializer implements PrimitiveSerializer<$MixedAnyOf1S
}
}

typedef $NumString = ({num? $num, String? string});
typedef _$b6d67dc2a96424d2f407f8e51557f3de = ({num? $num, String? string});

extension $NumStringExtension on $NumString {
/// @nodoc
// ignore: library_private_types_in_public_api
extension $b6d67dc2a96424d2f407f8e51557f3deExtension on _$b6d67dc2a96424d2f407f8e51557f3de {
List<dynamic> get _values => [$num, string];
void validateOneOf() => dynamite_utils.validateOneOf(_values);
void validateAnyOf() => dynamite_utils.validateAnyOf(_values);
static Serializer<$NumString> get serializer => const _$NumStringSerializer();
static $NumString fromJson(Object? json) => jsonSerializers.deserializeWith(serializer, json)!;
Object? toJson() => jsonSerializers.serializeWith(serializer, this);
static Serializer<_$b6d67dc2a96424d2f407f8e51557f3de> get _serializer =>
const _$b6d67dc2a96424d2f407f8e51557f3deSerializer();
static _$b6d67dc2a96424d2f407f8e51557f3de _fromJson(Object? json) =>
jsonSerializers.deserializeWith(_serializer, json)!;
Object? toJson() => jsonSerializers.serializeWith(_serializer, this);
}

class _$NumStringSerializer implements PrimitiveSerializer<$NumString> {
const _$NumStringSerializer();
class _$b6d67dc2a96424d2f407f8e51557f3deSerializer implements PrimitiveSerializer<_$b6d67dc2a96424d2f407f8e51557f3de> {
const _$b6d67dc2a96424d2f407f8e51557f3deSerializer();

@override
Iterable<Type> get types => const [$NumString];
Iterable<Type> get types => const [_$b6d67dc2a96424d2f407f8e51557f3de];

@override
String get wireName => r'$NumString';
String get wireName => r'_$b6d67dc2a96424d2f407f8e51557f3de';

@override
Object serialize(
Serializers serializers,
$NumString object, {
_$b6d67dc2a96424d2f407f8e51557f3de object, {
FullType specifiedType = FullType.unspecified,
}) {
dynamic value;
Expand All @@ -248,7 +278,7 @@ class _$NumStringSerializer implements PrimitiveSerializer<$NumString> {
}

@override
$NumString deserialize(
_$b6d67dc2a96424d2f407f8e51557f3de deserialize(
Serializers serializers,
Object data, {
FullType specifiedType = FullType.unspecified,
Expand All @@ -272,13 +302,13 @@ final Serializers serializers = (Serializers().toBuilder()
..add(ObjectAnyOf0.serializer)
..addBuilderFactory(const FullType(ObjectAnyOf1), ObjectAnyOf1Builder.new)
..add(ObjectAnyOf1.serializer)
..add($ObjectAnyOf0ObjectAnyOf1Extension.serializer)
..add($0c9017d9a03ba2eb2f15acadeab85bbeExtension._serializer)
..addBuilderFactory(const FullType(MixedAnyOf1), MixedAnyOf1Builder.new)
..add(MixedAnyOf1.serializer)
..add($MixedAnyOf1StringExtension.serializer)
..add($fba45e085ee99d64c5141852d4323e3dExtension._serializer)
..addBuilderFactory(const FullType(OneObjectAnyOf0), OneObjectAnyOf0Builder.new)
..add(OneObjectAnyOf0.serializer)
..add($NumStringExtension.serializer))
..add($b6d67dc2a96424d2f407f8e51557f3deExtension._serializer))
.build();
@visibleForTesting
final Serializers jsonSerializers = (serializers.toBuilder()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ignore_for_file: camel_case_types, discarded_futures
// ignore_for_file: camel_case_extensions, camel_case_types, discarded_futures
// ignore_for_file: no_leading_underscores_for_local_identifiers
// ignore_for_file: public_member_api_docs, unreachable_switch_case
// ignore_for_file: unused_element
Expand Down
Loading