From 9a28723be4fc301ac68001541375bdf3d3a5f08b Mon Sep 17 00:00:00 2001 From: jld3103 Date: Thu, 16 Feb 2023 16:48:58 +0100 Subject: [PATCH] dynamite: Fix allOf's --- .../dynamite/lib/src/openapi_builder.dart | 83 ++++++++++--------- 1 file changed, 45 insertions(+), 38 deletions(-) diff --git a/packages/dynamite/lib/src/openapi_builder.dart b/packages/dynamite/lib/src/openapi_builder.dart index 2690f676..a2185a2b 100644 --- a/packages/dynamite/lib/src/openapi_builder.dart +++ b/packages/dynamite/lib/src/openapi_builder.dart @@ -1367,7 +1367,7 @@ TypeResult resolveType( final s = schema.ofs![results.indexOf(result)]; b ..name = fields[result.name] - ..type = refer(_makeNullable(result.name, true)) + ..type = refer(_makeNullable(result.name, !(schema.allOf?.contains(s) ?? false))) ..modifier = FieldModifier.final$ ..docs.addAll(_descriptionToDocs(s.description)); }, @@ -1390,7 +1390,8 @@ TypeResult resolveType( (final b) => b ..name = fields[result.name]! ..toThis = true - ..named = true, + ..named = true + ..required = schema.allOf != null, ), ], ]), @@ -1409,48 +1410,54 @@ TypeResult resolveType( ) ..body = Code( [ - for (final result in results) ...[ - '${result.name}? ${fields[result.name]!};', - ], - for (final result in results) ...[ - if (schema.discriminator != null) ...[ - "if (data['${schema.discriminator!.propertyName}'] == '${result.name.replaceFirst(state.prefix, '')}'", - if (schema.discriminator!.mapping != null && - schema.discriminator!.mapping!.isNotEmpty) ...[ - for (final key in schema.discriminator!.mapping!.entries - .where( - (final entry) => - entry.value.endsWith('/${result.name.replaceFirst(state.prefix, '')}'), - ) - .map((final entry) => entry.key)) ...[ - " || data['${schema.discriminator!.propertyName}'] == '$key'", + if (schema.allOf != null) ...[ + 'return ${state.prefix}$identifier(', + 'data,', + for (final result in results) ...[ + '${fields[result.name]!}: ${result.deserialize('data')},', + ], + ');', + ] else ...[ + for (final result in results) ...[ + '${result.name}? ${fields[result.name]!};', + ], + for (final result in results) ...[ + if (schema.discriminator != null) ...[ + "if (data['${schema.discriminator!.propertyName}'] == '${result.name.replaceFirst(state.prefix, '')}'", + if (schema.discriminator!.mapping != null && + schema.discriminator!.mapping!.isNotEmpty) ...[ + for (final key in schema.discriminator!.mapping!.entries + .where( + (final entry) => + entry.value.endsWith('/${result.name.replaceFirst(state.prefix, '')}'), + ) + .map((final entry) => entry.key)) ...[ + " || data['${schema.discriminator!.propertyName}'] == '$key'", + ], ], + ') {', + ], + 'try {', + '${fields[result.name]!} = ${result.deserialize('data')};', + '} catch (_) {', + if (schema.discriminator != null) ...[ + 'rethrow;', + ], + '}', + if (schema.discriminator != null) ...[ + '}', ], - ') {', ], - 'try {', - '${fields[result.name]!} = ${result.deserialize('data')};', - '} catch (_) {', - if (schema.discriminator != null) ...[ - 'rethrow;', + if (schema.oneOf != null) ...[ + "assert([${fields.values.join(',')}].where((final x) => x != null).length == 1, 'Need oneOf for \$data');", ], - '}', - if (schema.discriminator != null) ...[ - '}', + 'return ${state.prefix}$identifier(', + 'data,', + for (final result in results) ...[ + '${fields[result.name]!}: ${fields[result.name]!},', ], + ');', ], - if (schema.oneOf != null) ...[ - "assert([${fields.values.join(',')}].where((final x) => x != null).length == 1, 'Need oneOf for \$data');", - ], - if (schema.allOf != null) ...[ - "assert([${fields.values.join(',')}].where((final x) => x != null).length == ${fields.length}, 'Need allOf for \$data');", - ], - 'return ${state.prefix}$identifier(', - 'data,', - for (final result in results) ...[ - '${fields[result.name]!}: ${fields[result.name]!},', - ], - ');', ].join(), ); },