From 22684cde70ebc70184eb8f673a1f03161bcd2a20 Mon Sep 17 00:00:00 2001 From: jld3103 Date: Mon, 19 Dec 2022 19:21:21 +0100 Subject: [PATCH 1/2] dynamite: Simplify JsonKey annotations --- packages/dynamite/lib/src/openapi_builder.dart | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/packages/dynamite/lib/src/openapi_builder.dart b/packages/dynamite/lib/src/openapi_builder.dart index 06d445bf..7ef67c72 100644 --- a/packages/dynamite/lib/src/openapi_builder.dart +++ b/packages/dynamite/lib/src/openapi_builder.dart @@ -1168,21 +1168,23 @@ TypeResult resolveObject( '/// ${propertySchema.description!}', ], ]); - if (_toDartName(propertyName) != propertyName || - propertySchema.isJsonString || - extraJsonKeyValues != null) { + final hasDifferentName = _toDartName(propertyName) != propertyName; + final isJsonString = propertySchema.isJsonString; + final hasExtraJsonKeyValues = + extraJsonKeyValues != null && extraJsonKeyValues.containsKey(propertyName); + if (hasDifferentName || isJsonString || hasExtraJsonKeyValues) { b.annotations.add( refer('JsonKey').call( [], { - if (_toDartName(propertyName) != propertyName) ...{ + if (hasDifferentName) ...{ 'name': refer("'$propertyName'"), }, - if (propertySchema.isJsonString) ...{ + if (isJsonString) ...{ 'fromJson': refer('${result.name}.fromJsonString'), 'toJson': refer('${result.name}.toJsonString'), }, - if (extraJsonKeyValues != null && extraJsonKeyValues.containsKey(propertyName)) ...{ + if (hasExtraJsonKeyValues) ...{ for (final key in extraJsonKeyValues[propertyName]!.keys) ...{ key: refer(extraJsonKeyValues[propertyName]![key]!), }, From 021092893ce1e1383ebb9f563bdc0a510bf91a33 Mon Sep 17 00:00:00 2001 From: jld3103 Date: Mon, 19 Dec 2022 20:18:10 +0100 Subject: [PATCH 2/2] dynamite: Properly serialize and deserialize string arrays in objects --- .../dynamite/lib/src/openapi_builder.dart | 56 +++++++++++++++++-- .../dynamite/lib/src/type_result/list.dart | 2 +- .../dynamite/lib/src/type_result/object.dart | 2 +- 3 files changed, 54 insertions(+), 6 deletions(-) diff --git a/packages/dynamite/lib/src/openapi_builder.dart b/packages/dynamite/lib/src/openapi_builder.dart index 7ef67c72..6b4b80fc 100644 --- a/packages/dynamite/lib/src/openapi_builder.dart +++ b/packages/dynamite/lib/src/openapi_builder.dart @@ -1170,9 +1170,57 @@ TypeResult resolveObject( ]); final hasDifferentName = _toDartName(propertyName) != propertyName; final isJsonString = propertySchema.isJsonString; + final isJsonStringArray = result is TypeResultList && (propertySchema.items?.isJsonString ?? false); final hasExtraJsonKeyValues = extraJsonKeyValues != null && extraJsonKeyValues.containsKey(propertyName); - if (hasDifferentName || isJsonString || hasExtraJsonKeyValues) { + if (hasDifferentName || isJsonString || isJsonStringArray || hasExtraJsonKeyValues) { + var fromJson = '${result.name}.fromJsonString'; + var toJson = '${result.name}.toJsonString'; + if (isJsonStringArray) { + fromJson = '_${_toDartName('${identifier}FromJsonString')}'; + if (!state.resolvedTypes.contains(fromJson)) { + state.resolvedTypes.add(fromJson); + state.output.add( + Method( + (final b) => b + ..name = fromJson + ..returns = refer(result.name) + ..lambda = true + ..requiredParameters.addAll([ + Parameter( + (final b) => b + ..name = 'data' + ..type = refer('String'), + ), + ]) + ..body = Code('${result.deserialize(result.decode('data'))};'), + ), + ); + } + toJson = '_${_toDartName('${identifier}ToJsonString')}'; + if (!state.resolvedTypes.contains(toJson)) { + state.resolvedTypes.add(toJson); + state.output.add( + Method( + (final b) => b + ..name = toJson + ..returns = refer('List?') + ..lambda = true + ..requiredParameters.addAll([ + Parameter( + (final b) => b + ..name = 'data' + ..type = refer(_makeNullable(result.name, true)), + ), + ]) + ..body = Code( + 'data == null ? null : ${result.encode(result.serialize('data'), mimeType: 'application/json', onlyChildren: true)};', + ), + ), + ); + } + } + b.annotations.add( refer('JsonKey').call( [], @@ -1180,9 +1228,9 @@ TypeResult resolveObject( if (hasDifferentName) ...{ 'name': refer("'$propertyName'"), }, - if (isJsonString) ...{ - 'fromJson': refer('${result.name}.fromJsonString'), - 'toJson': refer('${result.name}.toJsonString'), + if (isJsonString || isJsonStringArray) ...{ + 'fromJson': refer(fromJson), + 'toJson': refer(toJson), }, if (hasExtraJsonKeyValues) ...{ for (final key in extraJsonKeyValues[propertyName]!.keys) ...{ diff --git a/packages/dynamite/lib/src/type_result/list.dart b/packages/dynamite/lib/src/type_result/list.dart index 2208a834..3b7685de 100644 --- a/packages/dynamite/lib/src/type_result/list.dart +++ b/packages/dynamite/lib/src/type_result/list.dart @@ -20,7 +20,7 @@ class TypeResultList extends TypeResult { final String? mimeType, }) { if (onlyChildren) { - return '$object.map((final e) => ${subType.encode('e')}).toList()'; + return '$object.map((final e) => ${subType.encode('e', mimeType: mimeType)}).toList()'; } switch (mimeType) { diff --git a/packages/dynamite/lib/src/type_result/object.dart b/packages/dynamite/lib/src/type_result/object.dart index 40f09d62..65eeb365 100644 --- a/packages/dynamite/lib/src/type_result/object.dart +++ b/packages/dynamite/lib/src/type_result/object.dart @@ -11,7 +11,7 @@ class TypeResultObject extends TypeResult { @override String serialize(final String object) { if (fromJsonString) { - return '$object.toJsonString()'; + return '$name.toJsonString($object)'; } return '$object.toJson()'; }