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;', 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', '// coverage:ignore-start',
'T deserialize$prefix<T>(final Object data) => serializers.deserialize(data, specifiedType: FullType(T))! as T;', '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 resolvedTypes = <String>[];
final registeredJsonObjects = <String>[]; final registeredJsonObjects = <String>[];
final output = <Spec>[]; final output = <Spec>[];
bool hasContentString = false;
} }
TypeResult resolveObject( TypeResult resolveObject(
@ -1675,7 +1674,6 @@ TypeResult resolveType(
identifier, identifier,
schema.contentSchema!, schema.contentSchema!,
); );
state.hasContentString = true;
result = TypeResultObject('ContentString', generics: [subResult]); result = TypeResultObject('ContentString', generics: [subResult]);
} else { } else {

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

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

Loading…
Cancel
Save