diff --git a/packages/dynamite/dynamite/lib/src/openapi_builder.dart b/packages/dynamite/dynamite/lib/src/openapi_builder.dart index a93d65fe..9369d5c3 100644 --- a/packages/dynamite/dynamite/lib/src/openapi_builder.dart +++ b/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(final Object data) => serializers.deserialize(data, specifiedType: FullType(T))! as T;', @@ -1228,7 +1228,6 @@ class State { final resolvedTypes = []; final registeredJsonObjects = []; final output = []; - bool hasContentString = false; } TypeResult resolveObject( @@ -1675,7 +1674,6 @@ TypeResult resolveType( identifier, schema.contentSchema!, ); - state.hasContentString = true; result = TypeResultObject('ContentString', generics: [subResult]); } else { diff --git a/packages/dynamite/dynamite/lib/src/type_result/object.dart b/packages/dynamite/dynamite/lib/src/type_result/object.dart index f61184f4..9a6ff342 100644 --- a/packages/dynamite/dynamite/lib/src/type_result/object.dart +++ b/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)'; diff --git a/packages/dynamite/dynamite/lib/src/type_result/type_result.dart b/packages/dynamite/dynamite/lib/src/type_result/type_result.dart index 83fdc9c5..ea6cca16 100644 --- a/packages/dynamite/dynamite/lib/src/type_result/type_result.dart +++ b/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}); diff --git a/packages/dynamite/dynamite/test/type_result_test.dart b/packages/dynamite/dynamite/test/type_result_test.dart index 286ec0ee..34ec95f4 100644 --- a/packages/dynamite/dynamite/test/type_result_test.dart +++ b/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'); + expect(type.name, 'BuiltList'); + 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'); + expect(type.name, 'BuiltMap'); + 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'); + expect(type.name, 'CustomType'); + 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)'); }); }); }