Browse Source

dynamite: add contentString serialization

Signed-off-by: Nikolas Rimikis <rimikis.nikolas@gmail.com>
pull/194/head
Nikolas Rimikis 2 years ago
parent
commit
faac42984a
No known key found for this signature in database
GPG Key ID: 85ED1DE9786A4FF2
  1. 4
      packages/dynamite/dynamite/lib/src/openapi_builder.dart
  2. 24
      packages/dynamite/dynamite/lib/src/type_result/object.dart
  3. 12
      packages/dynamite/dynamite/lib/src/type_result/type_result.dart
  4. 16
      packages/dynamite/dynamite/test/type_result_test.dart

4
packages/dynamite/dynamite/lib/src/openapi_builder.dart

@ -1018,7 +1018,7 @@ class OpenAPIBuilder implements Builder {
'])',
r'final Serializers serializers = _$serializers;',
'',
'final Serializers jsonSerializers = (serializers.toBuilder()..addPlugin(StandardJsonPlugin())).build();',
'final Serializers jsonSerializers = (serializers.toBuilder()..addPlugin(StandardJsonPlugin())..addPlugin(const ContentStringPlugin())).build();',
'',
'// coverage:ignore-start',
'T deserialize$prefix<T>(final Object data) => serializers.deserialize(data, specifiedType: FullType(T))! as T;',
@ -1228,7 +1228,6 @@ class State {
final resolvedTypes = <String>[];
final registeredJsonObjects = <String>[];
final output = <Spec>[];
bool hasContentString = false;
}
TypeResult resolveObject(
@ -1675,7 +1674,6 @@ TypeResult resolveType(
identifier,
schema.contentSchema!,
);
state.hasContentString = true;
result = TypeResultObject('ContentString', generics: [subResult]);
} else {

24
packages/dynamite/dynamite/lib/src/type_result/object.dart

@ -1,5 +1,7 @@
part of '../../dynamite.dart';
const _contentString = 'ContentString';
class TypeResultObject extends TypeResult {
TypeResultObject(
super.className, {
@ -10,7 +12,13 @@ class TypeResultObject extends TypeResult {
);
@override
String serialize(final String object) => '$object.toJson()';
String serialize(final String object) {
if (className == _contentString) {
return 'jsonSerializers.serialize(messages, specifiedType: const $fullType)';
}
return '$object.toJson()';
}
@override
String encode(
@ -18,6 +26,11 @@ class TypeResultObject extends TypeResult {
final bool onlyChildren = false,
final String? mimeType,
}) {
if (className == _contentString) {
assert(mimeType == 'application/json', '$_contentString should have a mimeType of application/json');
return object;
}
switch (mimeType) {
case 'application/json':
return 'json.encode($object)';
@ -29,8 +42,13 @@ class TypeResultObject extends TypeResult {
}
@override
String deserialize(final String object, {final bool toBuilder = false}) =>
'$name.fromJson($object as Object)${toBuilder ? '.toBuilder()' : ''}';
String deserialize(final String object, {final bool toBuilder = false}) {
if (className == 'ContentString') {
return 'jsonSerializers.deserialize(messages, specifiedType: const $fullType)! as $name';
}
return '$name.fromJson($object as Object)${toBuilder ? '.toBuilder()' : ''}';
}
@override
String decode(final String object) => 'json.decode($object as String)';

12
packages/dynamite/dynamite/lib/src/type_result/type_result.dart

@ -20,6 +20,18 @@ abstract class TypeResult {
return className;
}
String get fullType {
if (generics.isNotEmpty) {
final buffer = StringBuffer('FullType($className, [')
..writeAll(generics.map((final c) => c.fullType).intersperse(', '))
..write('])');
return buffer.toString();
}
return 'FullType($className)';
}
String serialize(final String object);
String deserialize(final String object, {final bool toBuilder = false});

16
packages/dynamite/dynamite/test/type_result_test.dart

@ -5,27 +5,30 @@ void main() {
group(TypeResultList, () {
test('name', () {
final subType = TypeResultBase('String');
final listType = TypeResultList('BuiltList', subType);
final type = TypeResultList('BuiltList', subType);
expect(listType.name, 'BuiltList<String>');
expect(type.name, 'BuiltList<String>');
expect(type.fullType, 'FullType(BuiltList, [FullType(String)])');
});
});
group(TypeResultMap, () {
test('name', () {
final subType = TypeResultBase('int');
final listType = TypeResultMap('BuiltMap', subType);
final type = TypeResultMap('BuiltMap', subType);
expect(listType.name, 'BuiltMap<String, int>');
expect(type.name, 'BuiltMap<String, int>');
expect(type.fullType, 'FullType(BuiltMap, [FullType(String), FullType(int)])');
});
});
group(TypeResultObject, () {
test('name', () {
final subType = TypeResultBase('String');
final listType = TypeResultObject('CustomType', generics: [subType]);
final type = TypeResultObject('CustomType', generics: [subType]);
expect(listType.name, 'CustomType<String>');
expect(type.name, 'CustomType<String>');
expect(type.fullType, 'FullType(CustomType, [FullType(String)])');
});
});
@ -34,6 +37,7 @@ void main() {
final type = TypeResultBase('String');
expect(type.name, 'String');
expect(type.fullType, 'FullType(String)');
});
});
}

Loading…
Cancel
Save