From 36cac347cf94fe620cd8c18604c44fa070f55614 Mon Sep 17 00:00:00 2001 From: jld3103 Date: Tue, 29 Nov 2022 22:32:13 +0100 Subject: [PATCH] dynamite: Fix JSON encoded string fields --- .../dynamite/lib/src/openapi_builder.dart | 13 +++++++++++- .../dynamite/lib/src/type_result/object.dart | 21 +++++++++++++++---- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/packages/dynamite/lib/src/openapi_builder.dart b/packages/dynamite/lib/src/openapi_builder.dart index 99aa70fb..58439e36 100644 --- a/packages/dynamite/lib/src/openapi_builder.dart +++ b/packages/dynamite/lib/src/openapi_builder.dart @@ -991,6 +991,7 @@ TypeResult resolveObject( final Schema schema, { required final Map? extraJsonSerializableValues, required final Map>? extraJsonKeyValues, + final bool fromJsonString = false, }) { if (!state.resolvedTypes.contains(identifier)) { state.resolvedTypes.add(identifier); @@ -1162,7 +1163,10 @@ TypeResult resolveObject( ), ); } - return TypeResultObject(identifier); + return TypeResultObject( + identifier, + fromJsonString: fromJsonString, + ); } TypeResult resolveType( @@ -1173,6 +1177,7 @@ TypeResult resolveType( final Map? extraJsonSerializableValues, final Map>? extraJsonKeyValues, final bool ignoreEnum = false, + final bool fromJsonString = false, }) { TypeResult? result; if (schema.ref != null) { @@ -1183,6 +1188,7 @@ TypeResult resolveType( name, spec.components!.schemas![name]!, extraJsonSerializableValues: extraJsonSerializableValues, + fromJsonString: fromJsonString, ); } else if (schema.ofs != null) { if (!state.resolvedTypes.contains(identifier)) { @@ -1195,6 +1201,7 @@ TypeResult resolveType( '$identifier${schema.ofs!.indexOf(s)}', s, extraJsonSerializableValues: extraJsonSerializableValues, + fromJsonString: fromJsonString, ), ) .toList(); @@ -1334,6 +1341,7 @@ TypeResult resolveType( identifier, schema.contentSchema!, extraJsonSerializableValues: extraJsonSerializableValues, + fromJsonString: true, ); break; } @@ -1350,6 +1358,7 @@ TypeResult resolveType( identifier, schema.items!, extraJsonSerializableValues: extraJsonSerializableValues, + fromJsonString: fromJsonString, ); result = TypeResultList( 'List<${subResult.name}>', @@ -1382,6 +1391,7 @@ TypeResult resolveType( schema, extraJsonSerializableValues: extraJsonSerializableValues, extraJsonKeyValues: extraJsonKeyValues, + fromJsonString: fromJsonString, ); break; } @@ -1427,6 +1437,7 @@ TypeResult resolveType( schema, ignoreEnum: true, extraJsonSerializableValues: extraJsonSerializableValues, + fromJsonString: fromJsonString, ); b ..name = _toDartName(value.toString()) diff --git a/packages/dynamite/lib/src/type_result/object.dart b/packages/dynamite/lib/src/type_result/object.dart index 539f8431..df66cba9 100644 --- a/packages/dynamite/lib/src/type_result/object.dart +++ b/packages/dynamite/lib/src/type_result/object.dart @@ -2,17 +2,30 @@ part of '../../dynamite.dart'; class TypeResultObject extends TypeResult { TypeResultObject( - super.typeName, - ); + super.typeName, { + this.fromJsonString = false, + }); + + final bool fromJsonString; @override - String serialize(final String object) => '$object.toJson()'; + String serialize(final String object) { + if (fromJsonString) { + return '$object.toJsonString()'; + } + return '$object.toJson()'; + } @override String encode(final String object) => 'json.encode($object)'; @override - String deserialize(final String object) => '$name.fromJson($object as Map)'; + String deserialize(final String object) { + if (fromJsonString) { + return '$name.fromJsonString($object as String)'; + } + return '$name.fromJson($object as Map)'; + } @override String decode(final String object) => 'json.decode($object as String)';