diff --git a/packages/dynamite/lib/src/openapi_builder.dart b/packages/dynamite/lib/src/openapi_builder.dart index a7a11c40..e9684b94 100644 --- a/packages/dynamite/lib/src/openapi_builder.dart +++ b/packages/dynamite/lib/src/openapi_builder.dart @@ -65,6 +65,7 @@ class OpenAPIBuilder implements Builder { '', "import 'package:built_collection/built_collection.dart';", "import 'package:built_value/built_value.dart';", + "import 'package:built_value/json_object.dart';", "import 'package:built_value/serializer.dart';", "import 'package:built_value/standard_json_plugin.dart';", "import 'package:cookie_jar/cookie_jar.dart';", @@ -1520,7 +1521,7 @@ TypeResult resolveType( }) { TypeResult? result; if (schema.ref == null && schema.ofs == null && schema.type == null) { - return TypeResultBase('dynamic'); + return TypeResultBase('JsonObject'); } if (schema.ref != null) { final name = schema.ref!.split('/').last; @@ -1759,14 +1760,14 @@ TypeResult resolveType( extraJsonSerializableValues: extraJsonSerializableValues, ); result = TypeResultList( - 'List<${subResult.name}>', + 'BuiltList<${subResult.name}>', subResult, fromContentString: fromContentString, ); } else { result = TypeResultList( - 'List', - TypeResultBase('dynamic'), + 'BuiltList', + TypeResultBase('JsonObject'), ); } break; @@ -1775,8 +1776,8 @@ TypeResult resolveType( if (schema.additionalProperties != null) { if (schema.additionalProperties is EmptySchema) { result = TypeResultMap( - 'Map', - TypeResultBase('dynamic'), + 'BuiltMap', + TypeResultBase('JsonObject'), ); } else { final subResult = resolveType( @@ -1787,19 +1788,19 @@ TypeResult resolveType( extraJsonSerializableValues: extraJsonSerializableValues, ); result = TypeResultMap( - 'Map', - TypeResultBase('dynamic'), + 'BuiltMap', + TypeResultBase('JsonObject'), ); } break; } - result = TypeResultBase('dynamic'); + result = TypeResultBase('JsonObject'); break; } if (schema.properties!.isEmpty) { result = TypeResultMap( - 'Map', - TypeResultBase('dynamic'), + 'BuiltMap', + TypeResultBase('JsonObject'), ); break; } diff --git a/packages/dynamite/lib/src/type_result/base.dart b/packages/dynamite/lib/src/type_result/base.dart index 54fcc9e5..5ccb68f0 100644 --- a/packages/dynamite/lib/src/type_result/base.dart +++ b/packages/dynamite/lib/src/type_result/base.dart @@ -15,7 +15,12 @@ class TypeResultBase extends TypeResult { name == 'String' ? object : '$object.toString()'; @override - String deserialize(final String object) => '($object as $name)'; + String deserialize(final String object, {final bool toBuilder = false}) { + if (name == 'JsonObject') { + return 'JsonObject($object)'; + } + return '($object as $name)'; + } @override String decode(final String object) { @@ -24,6 +29,8 @@ class TypeResultBase extends TypeResult { return '($object as String)'; case 'int': return 'int.parse($object as String)'; + case 'JsonObject': + return 'JsonObject($object)'; default: throw Exception('Can not decode "$name" from String'); } diff --git a/packages/dynamite/lib/src/type_result/enum.dart b/packages/dynamite/lib/src/type_result/enum.dart index 7c361c1c..f14e2db4 100644 --- a/packages/dynamite/lib/src/type_result/enum.dart +++ b/packages/dynamite/lib/src/type_result/enum.dart @@ -20,7 +20,8 @@ class TypeResultEnum extends TypeResult { subType.encode(object); @override - String deserialize(final String object) => '$name.valueOf($object as ${subType.name})'; + String deserialize(final String object, {final bool toBuilder = false}) => + '$name.valueOf($object as ${subType.name})'; @override String decode(final String object) => subType.decode(object); diff --git a/packages/dynamite/lib/src/type_result/list.dart b/packages/dynamite/lib/src/type_result/list.dart index a5f52da6..37c815c1 100644 --- a/packages/dynamite/lib/src/type_result/list.dart +++ b/packages/dynamite/lib/src/type_result/list.dart @@ -11,7 +11,7 @@ class TypeResultList extends TypeResult { final bool fromContentString; @override - String serialize(final String object) => '$object.map((final e) => ${subType.serialize('e')}).toList()'; + String serialize(final String object) => '$object.map((final e) => ${subType.serialize('e')})'; @override String encode( @@ -20,7 +20,7 @@ class TypeResultList extends TypeResult { final String? mimeType, }) { if (onlyChildren) { - return '$object.map((final e) => ${subType.encode('e', mimeType: mimeType)}).toList()'; + return '$object.map((final e) => ${subType.encode('e', mimeType: mimeType)})'; } switch (mimeType) { @@ -34,7 +34,8 @@ class TypeResultList extends TypeResult { } @override - String deserialize(final String object) => '($object as List).map((final e) => ${subType.deserialize('e')}).toList()'; + String deserialize(final String object, {final bool toBuilder = false}) => + '$name(($object as List).map((final e) => ${subType.deserialize('e')}))${toBuilder ? '.toBuilder()' : ''}'; @override String decode(final String object) => 'json.decode($object as String)'; diff --git a/packages/dynamite/lib/src/type_result/map.dart b/packages/dynamite/lib/src/type_result/map.dart index 6f4da86f..65f99eb0 100644 --- a/packages/dynamite/lib/src/type_result/map.dart +++ b/packages/dynamite/lib/src/type_result/map.dart @@ -28,7 +28,8 @@ class TypeResultMap extends TypeResult { } @override - String deserialize(final String object) => '($object as Map)'; + String deserialize(final String object, {final bool toBuilder = false}) => + '($object as BuiltMap)${toBuilder ? '.toBuilder()' : ''}'; @override String decode(final String object) => 'json.decode($object as String)'; diff --git a/packages/dynamite/lib/src/type_result/object.dart b/packages/dynamite/lib/src/type_result/object.dart index f6f4a1fa..baed6689 100644 --- a/packages/dynamite/lib/src/type_result/object.dart +++ b/packages/dynamite/lib/src/type_result/object.dart @@ -4,7 +4,7 @@ class TypeResultObject extends TypeResult { TypeResultObject( super.name, { this.fromContentString = false, - }); + }) : assert(name != 'JsonObject' && name != 'Object' && name != 'dynamic', 'Use TypeResultBase instead'); final bool fromContentString; @@ -33,11 +33,11 @@ class TypeResultObject extends TypeResult { } @override - String deserialize(final String object) { + String deserialize(final String object, {final bool toBuilder = false}) { if (fromContentString) { - return '$name.fromJsonString($object as String)'; + return '$name.fromJsonString($object as String)${toBuilder ? '.toBuilder()' : ''}'; } - return '$name.fromJson($object as Map)'; + return '$name.fromJson($object as Map)${toBuilder ? '.toBuilder()' : ''}'; } @override diff --git a/packages/dynamite/lib/src/type_result/type_result.dart b/packages/dynamite/lib/src/type_result/type_result.dart index 393a3428..33cdbd74 100644 --- a/packages/dynamite/lib/src/type_result/type_result.dart +++ b/packages/dynamite/lib/src/type_result/type_result.dart @@ -7,7 +7,7 @@ abstract class TypeResult { String serialize(final String object); - String deserialize(final String object); + String deserialize(final String object, {final bool toBuilder = false}); String decode(final String object);