Browse Source

dynamite: patch discriminator

Signed-off-by: Nikolas Rimikis <rimikis.nikolas@gmail.com>
pull/194/head
Nikolas Rimikis 2 years ago
parent
commit
5079375024
No known key found for this signature in database
GPG Key ID: 85ED1DE9786A4FF2
  1. 21
      packages/dynamite/lib/src/openapi_builder.dart

21
packages/dynamite/lib/src/openapi_builder.dart

@ -1802,13 +1802,26 @@ TypeResult resolveType(
], ],
] else ...[ ] else ...[
if (schema.discriminator != null) ...[ if (schema.discriminator != null) ...[
'if (data is! Map<String, dynamic>) {', 'if (data is! Iterable) {',
"throw StateError('discriminator is only supported for serializing Json like data.');", r"throw StateError('Expected an Iterable but got ${data.runtimeType}');",
'}',
'',
'String? discriminator;',
'',
'final iterator = data.iterator;',
'while (iterator.moveNext()) {',
'final key = iterator.current! as String;',
'iterator.moveNext();',
'final Object? value = iterator.current;',
"if (key == '${schema.discriminator!.propertyName}') {",
'discriminator = value! as String;',
'break;',
'}',
'}', '}',
], ],
for (final result in results) ...[ for (final result in results) ...[
if (schema.discriminator != null) ...[ if (schema.discriminator != null) ...[
"if (data['${schema.discriminator!.propertyName}'] == '${result.name.replaceFirst(state.prefix, '')}'", "if (discriminator == '${result.name.replaceFirst(state.prefix, '')}'",
if (schema.discriminator!.mapping != null && schema.discriminator!.mapping!.isNotEmpty) ...[ if (schema.discriminator!.mapping != null && schema.discriminator!.mapping!.isNotEmpty) ...[
for (final key in schema.discriminator!.mapping!.entries for (final key in schema.discriminator!.mapping!.entries
.where( .where(
@ -1816,7 +1829,7 @@ TypeResult resolveType(
entry.value.endsWith('/${result.name.replaceFirst(state.prefix, '')}'), entry.value.endsWith('/${result.name.replaceFirst(state.prefix, '')}'),
) )
.map((final entry) => entry.key)) ...[ .map((final entry) => entry.key)) ...[
" || data['${schema.discriminator!.propertyName}'] == '$key'", " || discriminator == '$key'",
], ],
') {', ') {',
], ],

Loading…
Cancel
Save