From c2865ef506fdd8326b99e0df35102ce6c46a2ceb Mon Sep 17 00:00:00 2001 From: jld3103 Date: Wed, 21 Dec 2022 15:43:01 +0100 Subject: [PATCH 1/2] dynamite: Make content strings work for other types than json --- packages/dynamite/lib/src/models/schema.dart | 2 +- .../dynamite/lib/src/openapi_builder.dart | 29 ++++++++++--------- .../dynamite/lib/src/type_result/list.dart | 4 +-- .../dynamite/lib/src/type_result/object.dart | 8 ++--- 4 files changed, 22 insertions(+), 21 deletions(-) diff --git a/packages/dynamite/lib/src/models/schema.dart b/packages/dynamite/lib/src/models/schema.dart index bd424564..ffd891a2 100644 --- a/packages/dynamite/lib/src/models/schema.dart +++ b/packages/dynamite/lib/src/models/schema.dart @@ -67,5 +67,5 @@ class Schema { final Discriminator? discriminator; - bool get isJsonString => type == 'string' && contentMediaType == 'application/json' && contentSchema != null; + bool get isContentString => type == 'string' && (contentMediaType?.isNotEmpty ?? false) && contentSchema != null; } diff --git a/packages/dynamite/lib/src/openapi_builder.dart b/packages/dynamite/lib/src/openapi_builder.dart index a5d8a17c..88b66ed0 100644 --- a/packages/dynamite/lib/src/openapi_builder.dart +++ b/packages/dynamite/lib/src/openapi_builder.dart @@ -603,7 +603,7 @@ class OpenAPIBuilder implements Builder { if (nullable) { code.write('if (${_toDartName(parameter.name)} != null) {'); } - final isPlainList = result is TypeResultList && !result.fromJsonString; + final isPlainList = result is TypeResultList && !result.fromContentString; final value = result.encode( result.serialize(_toDartName(parameter.name)), onlyChildren: isPlainList && parameter.in_ == 'query', @@ -1039,7 +1039,7 @@ TypeResult resolveObject( final Schema schema, { required final Map? extraJsonSerializableValues, required final Map>? extraJsonKeyValues, - final bool fromJsonString = false, + final bool fromContentString = false, }) { if (!state.resolvedTypes.contains('${state.prefix}$identifier')) { state.resolvedTypes.add('${state.prefix}$identifier'); @@ -1181,14 +1181,15 @@ TypeResult resolveObject( ], ]); final hasDifferentName = _toDartName(propertyName) != propertyName; - final isJsonString = propertySchema.isJsonString; - final isJsonStringArray = result is TypeResultList && (propertySchema.items?.isJsonString ?? false); + final isContentString = propertySchema.isContentString; + final isContentStringArray = + result is TypeResultList && (propertySchema.items?.isContentString ?? false); final hasExtraJsonKeyValues = extraJsonKeyValues != null && extraJsonKeyValues.containsKey(propertyName); - if (hasDifferentName || isJsonString || isJsonStringArray || hasExtraJsonKeyValues) { + if (hasDifferentName || isContentString || isContentStringArray || hasExtraJsonKeyValues) { var fromJson = '${result.name}.fromJsonString'; var toJson = '${result.name}.toJsonString'; - if (isJsonStringArray) { + if (isContentStringArray) { fromJson = '_${_toDartName('${state.prefix}${identifier}FromJsonString')}'; if (!state.resolvedTypes.contains(fromJson)) { state.resolvedTypes.add(fromJson); @@ -1240,7 +1241,7 @@ TypeResult resolveObject( if (hasDifferentName) ...{ 'name': refer("'$propertyName'"), }, - if (isJsonString || isJsonStringArray) ...{ + if (isContentString || isContentStringArray) ...{ 'fromJson': refer(fromJson), 'toJson': refer(toJson), }, @@ -1263,7 +1264,7 @@ TypeResult resolveObject( } return TypeResultObject( '${state.prefix}$identifier', - fromJsonString: fromJsonString, + fromContentString: fromContentString, ); } @@ -1275,7 +1276,7 @@ TypeResult resolveType( final Map? extraJsonSerializableValues, final Map>? extraJsonKeyValues, final bool ignoreEnum = false, - final bool fromJsonString = false, + final bool fromContentString = false, }) { TypeResult? result; if (schema.ref != null) { @@ -1286,7 +1287,7 @@ TypeResult resolveType( name, spec.components!.schemas![name]!, extraJsonSerializableValues: extraJsonSerializableValues, - fromJsonString: fromJsonString, + fromContentString: fromContentString, ); } else if (schema.ofs != null) { if (!state.resolvedTypes.contains('${state.prefix}$identifier')) { @@ -1482,14 +1483,14 @@ TypeResult resolveType( break; } - if (schema.isJsonString) { + if (schema.isContentString) { result = resolveType( spec, state, identifier, schema.contentSchema!, extraJsonSerializableValues: extraJsonSerializableValues, - fromJsonString: true, + fromContentString: true, ); break; } @@ -1510,7 +1511,7 @@ TypeResult resolveType( result = TypeResultList( 'List<${subResult.name}>', subResult, - fromJsonString: fromJsonString, + fromContentString: fromContentString, ); } else { result = TypeResultList( @@ -1539,7 +1540,7 @@ TypeResult resolveType( schema, extraJsonSerializableValues: extraJsonSerializableValues, extraJsonKeyValues: extraJsonKeyValues, - fromJsonString: fromJsonString, + fromContentString: fromContentString, ); break; } diff --git a/packages/dynamite/lib/src/type_result/list.dart b/packages/dynamite/lib/src/type_result/list.dart index 3b7685de..a5f52da6 100644 --- a/packages/dynamite/lib/src/type_result/list.dart +++ b/packages/dynamite/lib/src/type_result/list.dart @@ -4,11 +4,11 @@ class TypeResultList extends TypeResult { TypeResultList( super.name, this.subType, { - this.fromJsonString = false, + this.fromContentString = false, }); final TypeResult subType; - final bool fromJsonString; + final bool fromContentString; @override String serialize(final String object) => '$object.map((final e) => ${subType.serialize('e')}).toList()'; diff --git a/packages/dynamite/lib/src/type_result/object.dart b/packages/dynamite/lib/src/type_result/object.dart index 65eeb365..f6f4a1fa 100644 --- a/packages/dynamite/lib/src/type_result/object.dart +++ b/packages/dynamite/lib/src/type_result/object.dart @@ -3,14 +3,14 @@ part of '../../dynamite.dart'; class TypeResultObject extends TypeResult { TypeResultObject( super.name, { - this.fromJsonString = false, + this.fromContentString = false, }); - final bool fromJsonString; + final bool fromContentString; @override String serialize(final String object) { - if (fromJsonString) { + if (fromContentString) { return '$name.toJsonString($object)'; } return '$object.toJson()'; @@ -34,7 +34,7 @@ class TypeResultObject extends TypeResult { @override String deserialize(final String object) { - if (fromJsonString) { + if (fromContentString) { return '$name.fromJsonString($object as String)'; } return '$name.fromJson($object as Map)'; From 23987704bf5ff142d3beabcfef78502cd80a24a0 Mon Sep 17 00:00:00 2001 From: jld3103 Date: Wed, 21 Dec 2022 15:48:24 +0100 Subject: [PATCH 2/2] dynamite: Fix content string arrays --- packages/dynamite/lib/src/openapi_builder.dart | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/packages/dynamite/lib/src/openapi_builder.dart b/packages/dynamite/lib/src/openapi_builder.dart index 88b66ed0..14a857c6 100644 --- a/packages/dynamite/lib/src/openapi_builder.dart +++ b/packages/dynamite/lib/src/openapi_builder.dart @@ -1182,8 +1182,7 @@ TypeResult resolveObject( ]); final hasDifferentName = _toDartName(propertyName) != propertyName; final isContentString = propertySchema.isContentString; - final isContentStringArray = - result is TypeResultList && (propertySchema.items?.isContentString ?? false); + final isContentStringArray = isContentString && result is TypeResultList; final hasExtraJsonKeyValues = extraJsonKeyValues != null && extraJsonKeyValues.containsKey(propertyName); if (hasDifferentName || isContentString || isContentStringArray || hasExtraJsonKeyValues) { @@ -1217,7 +1216,7 @@ TypeResult resolveObject( Method( (final b) => b ..name = toJson - ..returns = refer('List?') + ..returns = refer('String?') ..lambda = true ..requiredParameters.addAll([ Parameter( @@ -1227,7 +1226,7 @@ TypeResult resolveObject( ), ]) ..body = Code( - 'data == null ? null : ${result.encode(result.serialize('data'), mimeType: 'application/json', onlyChildren: true)};', + 'data == null ? null : ${result.encode(result.serialize('data'), mimeType: 'application/json')};', ), ), );