From 10e641daa1132088bf34dce6b653635e968caae4 Mon Sep 17 00:00:00 2001 From: Nikolas Rimikis Date: Sun, 26 Mar 2023 20:20:54 +0200 Subject: [PATCH] fix contentString with a base object contentSchema Signed-off-by: Nikolas Rimikis --- .../dynamite/lib/src/openapi_builder.dart | 46 +++++++++++-------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/packages/dynamite/lib/src/openapi_builder.dart b/packages/dynamite/lib/src/openapi_builder.dart index f18dc4ea..548f903b 100644 --- a/packages/dynamite/lib/src/openapi_builder.dart +++ b/packages/dynamite/lib/src/openapi_builder.dart @@ -1265,13 +1265,28 @@ TypeResult resolveObject( ..docs.addAll(_descriptionToDocs(propertySchema.description)); final hasDifferentName = _toDartName(propertyName) != propertyName; final isContentString = propertySchema.isContentString; - final isContentStringArray = isContentString && result is TypeResultList; final hasExtraJsonKeyValues = extraJsonKeyValues != null && extraJsonKeyValues.containsKey(propertyName); - if (hasDifferentName || isContentString || isContentStringArray || hasExtraJsonKeyValues) { + + final arguments = { + if (hasDifferentName) ...{ + 'name': refer("'$propertyName'"), + }, + if (hasExtraJsonKeyValues) ...{ + for (final key in extraJsonKeyValues[propertyName]!.keys) ...{ + key: refer(extraJsonKeyValues[propertyName]![key]!), + }, + }, + }; + + if (isContentString) { + if (result is! TypeResultObject || result is! TypeResultList) { + print("The content string $identifier.$propertyName can't be decoded automatically."); + } + var fromJson = '${result.name}.fromJsonString'; var toJson = '${result.name}.toJsonString'; - if (isContentStringArray) { + if (result is TypeResultList) { fromJson = '_${_toDartName('${state.prefix}${identifier}FromJsonString')}'; if (!state.resolvedTypes.contains(fromJson)) { state.resolvedTypes.add(fromJson); @@ -1316,24 +1331,15 @@ TypeResult resolveObject( } } + arguments.addAll({ + 'fromJson': refer(fromJson), + 'toJson': refer(toJson), + }); + } + + if (arguments.isNotEmpty) { b.annotations.add( - refer('JsonKey').call( - [], - { - if (hasDifferentName) ...{ - 'name': refer("'$propertyName'"), - }, - if (isContentString || isContentStringArray) ...{ - 'fromJson': refer(fromJson), - 'toJson': refer(toJson), - }, - if (hasExtraJsonKeyValues) ...{ - for (final key in extraJsonKeyValues[propertyName]!.keys) ...{ - key: refer(extraJsonKeyValues[propertyName]![key]!), - }, - }, - }, - ), + refer('JsonKey').call([], arguments), ); } },