diff --git a/packages/dynamite/lib/src/openapi_builder.dart b/packages/dynamite/lib/src/openapi_builder.dart index ac8e16e7..a7a11c40 100644 --- a/packages/dynamite/lib/src/openapi_builder.dart +++ b/packages/dynamite/lib/src/openapi_builder.dart @@ -65,6 +65,8 @@ class OpenAPIBuilder implements Builder { '', "import 'package:built_collection/built_collection.dart';", "import 'package:built_value/built_value.dart';", + "import 'package:built_value/serializer.dart';", + "import 'package:built_value/standard_json_plugin.dart';", "import 'package:cookie_jar/cookie_jar.dart';", "import 'package:universal_io/io.dart';", '', @@ -1031,24 +1033,17 @@ class OpenAPIBuilder implements Builder { if (state.registeredJsonObjects.isNotEmpty) { output.addAll([ - '// coverage:ignore-start', - 'final _deserializers = {', + '@SerializersFor(const [', for (final name in state.registeredJsonObjects) ...[ - '$name: (final data) => ${TypeResultObject(name).deserialize('data')},', - 'List<$name>: (final data) => ${TypeResultList('List<$name>', TypeResultObject(name)).deserialize('data')},', + '$name,', ], - '};', + '])', + r'final Serializers serializers = (_$serializers.toBuilder()..addPlugin(StandardJsonPlugin())).build();', '', - 'final _serializers = {', - for (final name in state.registeredJsonObjects) ...[ - '$name: (final data) => ${TypeResultObject(name).serialize('data')},', - 'List<$name>: (final data) => ${TypeResultList('List<$name>', TypeResultObject(name)).serialize('data')},', - ], - '};', - '', - 'T deserialize$prefix(final dynamic data) => _deserializers[T]!(data) as T;', + '// coverage:ignore-start', + 'T deserialize$prefix(final Object data) => serializers.deserialize(data, specifiedType: FullType(T))! as T;', '', - 'dynamic serialize$prefix(final T data) => _serializers[T]!(data);', + 'Object? serialize$prefix(final T data) => serializers.serialize(data, specifiedType: FullType(T));', '// coverage:ignore-end', ]); }