From af494a95a06a0bd4b657c386e713e232cecd39fc Mon Sep 17 00:00:00 2001 From: Nikolas Rimikis Date: Wed, 29 Nov 2023 20:14:09 +0100 Subject: [PATCH] feat(dynamite): flatten nested ofs of the same type Signed-off-by: Nikolas Rimikis --- .../lib/src/models/type_result/some_of.dart | 12 ++ .../lib/nested_ofs.openapi.dart | 184 +++++++++++++----- .../lib/nested_ofs.openapi.json | 10 + .../test/nested_ofs_test.dart | 108 ++++++---- 4 files changed, 232 insertions(+), 82 deletions(-) diff --git a/packages/dynamite/dynamite/lib/src/models/type_result/some_of.dart b/packages/dynamite/dynamite/lib/src/models/type_result/some_of.dart index 1d1aca5347d..e174c4fe54a 100644 --- a/packages/dynamite/dynamite/lib/src/models/type_result/some_of.dart +++ b/packages/dynamite/dynamite/lib/src/models/type_result/some_of.dart @@ -59,6 +59,18 @@ abstract class TypeResultSomeOf extends TypeResult { } BuiltList get _optimizedSubTypes { + final subTypes = BuiltSet.build((final b) { + for (final type in this.subTypes) { + if (type is TypeResultAnyOf && this is TypeResultAnyOf) { + b.addAll(type.optimizedSubTypes); + } else if (type is TypeResultOneOf && this is TypeResultOneOf) { + b.addAll(type.optimizedSubTypes); + } else { + b.add(type); + } + } + }); + final optimized = ListBuilder(); final optimizeNum = subTypes.where(_isNumber).length >= 2; diff --git a/packages/dynamite/dynamite_end_to_end_test/lib/nested_ofs.openapi.dart b/packages/dynamite/dynamite_end_to_end_test/lib/nested_ofs.openapi.dart index 2c0007eae39..1fa6caf22c2 100644 --- a/packages/dynamite/dynamite_end_to_end_test/lib/nested_ofs.openapi.dart +++ b/packages/dynamite/dynamite_end_to_end_test/lib/nested_ofs.openapi.dart @@ -166,16 +166,20 @@ typedef BaseNestedOneOf = ({ BaseAllOf? baseAllOf, BaseAnyOf? baseAnyOf, BaseNestedOneOf3? baseNestedOneOf3, - BaseOneOf? baseOneOf + BaseOneOf1? baseOneOf1, + double? $double }); typedef BaseNestedAnyOf = ({ BaseAllOf? baseAllOf, - BaseAnyOf? baseAnyOf, + BaseAnyOf1? baseAnyOf1, BaseNestedAnyOf3? baseNestedAnyOf3, - BaseOneOf? baseOneOf + BaseOneOf? baseOneOf, + int? $int }); +typedef NestedOptimizedOneOf = ({BaseOneOf1? baseOneOf1, num? $num}); + typedef $BaseOneOf1Double = ({BaseOneOf1? baseOneOf1, double? $double}); extension $BaseOneOf1DoubleExtension on $BaseOneOf1Double { @@ -290,38 +294,40 @@ class _$BaseAnyOf1IntSerializer implements PrimitiveSerializer<$BaseAnyOf1Int> { } } -typedef $BaseAllOfBaseAnyOfBaseNestedOneOf3BaseOneOf = ({ +typedef $BaseAllOfBaseAnyOfBaseNestedOneOf3BaseOneOf1Double = ({ BaseAllOf? baseAllOf, BaseAnyOf? baseAnyOf, BaseNestedOneOf3? baseNestedOneOf3, - BaseOneOf? baseOneOf + BaseOneOf1? baseOneOf1, + double? $double }); -extension $BaseAllOfBaseAnyOfBaseNestedOneOf3BaseOneOfExtension on $BaseAllOfBaseAnyOfBaseNestedOneOf3BaseOneOf { - List get _values => [baseAllOf, baseAnyOf, baseNestedOneOf3, baseOneOf]; +extension $BaseAllOfBaseAnyOfBaseNestedOneOf3BaseOneOf1DoubleExtension + on $BaseAllOfBaseAnyOfBaseNestedOneOf3BaseOneOf1Double { + List get _values => [baseAllOf, baseAnyOf, baseNestedOneOf3, baseOneOf1, $double]; void validateOneOf() => dynamite_utils.validateOneOf(_values); void validateAnyOf() => dynamite_utils.validateAnyOf(_values); - static Serializer<$BaseAllOfBaseAnyOfBaseNestedOneOf3BaseOneOf> get serializer => - const _$BaseAllOfBaseAnyOfBaseNestedOneOf3BaseOneOfSerializer(); - static $BaseAllOfBaseAnyOfBaseNestedOneOf3BaseOneOf fromJson(final Object? json) => + static Serializer<$BaseAllOfBaseAnyOfBaseNestedOneOf3BaseOneOf1Double> get serializer => + const _$BaseAllOfBaseAnyOfBaseNestedOneOf3BaseOneOf1DoubleSerializer(); + static $BaseAllOfBaseAnyOfBaseNestedOneOf3BaseOneOf1Double fromJson(final Object? json) => _jsonSerializers.deserializeWith(serializer, json)!; Object? toJson() => _jsonSerializers.serializeWith(serializer, this); } -class _$BaseAllOfBaseAnyOfBaseNestedOneOf3BaseOneOfSerializer - implements PrimitiveSerializer<$BaseAllOfBaseAnyOfBaseNestedOneOf3BaseOneOf> { - const _$BaseAllOfBaseAnyOfBaseNestedOneOf3BaseOneOfSerializer(); +class _$BaseAllOfBaseAnyOfBaseNestedOneOf3BaseOneOf1DoubleSerializer + implements PrimitiveSerializer<$BaseAllOfBaseAnyOfBaseNestedOneOf3BaseOneOf1Double> { + const _$BaseAllOfBaseAnyOfBaseNestedOneOf3BaseOneOf1DoubleSerializer(); @override - Iterable get types => const [$BaseAllOfBaseAnyOfBaseNestedOneOf3BaseOneOf]; + Iterable get types => const [$BaseAllOfBaseAnyOfBaseNestedOneOf3BaseOneOf1Double]; @override - String get wireName => r'$BaseAllOfBaseAnyOfBaseNestedOneOf3BaseOneOf'; + String get wireName => r'$BaseAllOfBaseAnyOfBaseNestedOneOf3BaseOneOf1Double'; @override Object serialize( final Serializers serializers, - final $BaseAllOfBaseAnyOfBaseNestedOneOf3BaseOneOf object, { + final $BaseAllOfBaseAnyOfBaseNestedOneOf3BaseOneOf1Double object, { final FullType specifiedType = FullType.unspecified, }) { dynamic value; @@ -337,16 +343,20 @@ class _$BaseAllOfBaseAnyOfBaseNestedOneOf3BaseOneOfSerializer if (value != null) { return serializers.serialize(value, specifiedType: const FullType(BaseNestedOneOf3))!; } - value = object.baseOneOf; + value = object.baseOneOf1; if (value != null) { - return serializers.serialize(value, specifiedType: const FullType(BaseOneOf))!; + return serializers.serialize(value, specifiedType: const FullType(BaseOneOf1))!; + } + value = object.$double; + if (value != null) { + return serializers.serialize(value, specifiedType: const FullType(double))!; } // Should not be possible after validation. throw StateError('Tried to serialize without any value.'); } @override - $BaseAllOfBaseAnyOfBaseNestedOneOf3BaseOneOf deserialize( + $BaseAllOfBaseAnyOfBaseNestedOneOf3BaseOneOf1Double deserialize( final Serializers serializers, final Object data, { final FullType specifiedType = FullType.unspecified, @@ -365,47 +375,58 @@ class _$BaseAllOfBaseAnyOfBaseNestedOneOf3BaseOneOfSerializer baseNestedOneOf3 = serializers.deserialize(data, specifiedType: const FullType(BaseNestedOneOf3))! as BaseNestedOneOf3; } catch (_) {} - BaseOneOf? baseOneOf; + BaseOneOf1? baseOneOf1; try { - baseOneOf = - ((serializers.deserialize(data, specifiedType: const FullType(BaseOneOf))! as BaseOneOf)..validateOneOf()); + baseOneOf1 = serializers.deserialize(data, specifiedType: const FullType(BaseOneOf1))! as BaseOneOf1; } catch (_) {} - return (baseAllOf: baseAllOf, baseAnyOf: baseAnyOf, baseNestedOneOf3: baseNestedOneOf3, baseOneOf: baseOneOf); + double? $double; + try { + $double = serializers.deserialize(data, specifiedType: const FullType(double))! as double; + } catch (_) {} + return ( + baseAllOf: baseAllOf, + baseAnyOf: baseAnyOf, + baseNestedOneOf3: baseNestedOneOf3, + baseOneOf1: baseOneOf1, + $double: $double + ); } } -typedef $BaseAllOfBaseAnyOfBaseNestedAnyOf3BaseOneOf = ({ +typedef $BaseAllOfBaseAnyOf1BaseNestedAnyOf3BaseOneOfInt = ({ BaseAllOf? baseAllOf, - BaseAnyOf? baseAnyOf, + BaseAnyOf1? baseAnyOf1, BaseNestedAnyOf3? baseNestedAnyOf3, - BaseOneOf? baseOneOf + BaseOneOf? baseOneOf, + int? $int }); -extension $BaseAllOfBaseAnyOfBaseNestedAnyOf3BaseOneOfExtension on $BaseAllOfBaseAnyOfBaseNestedAnyOf3BaseOneOf { - List get _values => [baseAllOf, baseAnyOf, baseNestedAnyOf3, baseOneOf]; +extension $BaseAllOfBaseAnyOf1BaseNestedAnyOf3BaseOneOfIntExtension + on $BaseAllOfBaseAnyOf1BaseNestedAnyOf3BaseOneOfInt { + List get _values => [baseAllOf, baseAnyOf1, baseNestedAnyOf3, baseOneOf, $int]; void validateOneOf() => dynamite_utils.validateOneOf(_values); void validateAnyOf() => dynamite_utils.validateAnyOf(_values); - static Serializer<$BaseAllOfBaseAnyOfBaseNestedAnyOf3BaseOneOf> get serializer => - const _$BaseAllOfBaseAnyOfBaseNestedAnyOf3BaseOneOfSerializer(); - static $BaseAllOfBaseAnyOfBaseNestedAnyOf3BaseOneOf fromJson(final Object? json) => + static Serializer<$BaseAllOfBaseAnyOf1BaseNestedAnyOf3BaseOneOfInt> get serializer => + const _$BaseAllOfBaseAnyOf1BaseNestedAnyOf3BaseOneOfIntSerializer(); + static $BaseAllOfBaseAnyOf1BaseNestedAnyOf3BaseOneOfInt fromJson(final Object? json) => _jsonSerializers.deserializeWith(serializer, json)!; Object? toJson() => _jsonSerializers.serializeWith(serializer, this); } -class _$BaseAllOfBaseAnyOfBaseNestedAnyOf3BaseOneOfSerializer - implements PrimitiveSerializer<$BaseAllOfBaseAnyOfBaseNestedAnyOf3BaseOneOf> { - const _$BaseAllOfBaseAnyOfBaseNestedAnyOf3BaseOneOfSerializer(); +class _$BaseAllOfBaseAnyOf1BaseNestedAnyOf3BaseOneOfIntSerializer + implements PrimitiveSerializer<$BaseAllOfBaseAnyOf1BaseNestedAnyOf3BaseOneOfInt> { + const _$BaseAllOfBaseAnyOf1BaseNestedAnyOf3BaseOneOfIntSerializer(); @override - Iterable get types => const [$BaseAllOfBaseAnyOfBaseNestedAnyOf3BaseOneOf]; + Iterable get types => const [$BaseAllOfBaseAnyOf1BaseNestedAnyOf3BaseOneOfInt]; @override - String get wireName => r'$BaseAllOfBaseAnyOfBaseNestedAnyOf3BaseOneOf'; + String get wireName => r'$BaseAllOfBaseAnyOf1BaseNestedAnyOf3BaseOneOfInt'; @override Object serialize( final Serializers serializers, - final $BaseAllOfBaseAnyOfBaseNestedAnyOf3BaseOneOf object, { + final $BaseAllOfBaseAnyOf1BaseNestedAnyOf3BaseOneOfInt object, { final FullType specifiedType = FullType.unspecified, }) { dynamic value; @@ -413,9 +434,9 @@ class _$BaseAllOfBaseAnyOfBaseNestedAnyOf3BaseOneOfSerializer if (value != null) { return serializers.serialize(value, specifiedType: const FullType(BaseAllOf))!; } - value = object.baseAnyOf; + value = object.baseAnyOf1; if (value != null) { - return serializers.serialize(value, specifiedType: const FullType(BaseAnyOf))!; + return serializers.serialize(value, specifiedType: const FullType(BaseAnyOf1))!; } value = object.baseNestedAnyOf3; if (value != null) { @@ -425,12 +446,16 @@ class _$BaseAllOfBaseAnyOfBaseNestedAnyOf3BaseOneOfSerializer if (value != null) { return serializers.serialize(value, specifiedType: const FullType(BaseOneOf))!; } + value = object.$int; + if (value != null) { + return serializers.serialize(value, specifiedType: const FullType(int))!; + } // Should not be possible after validation. throw StateError('Tried to serialize without any value.'); } @override - $BaseAllOfBaseAnyOfBaseNestedAnyOf3BaseOneOf deserialize( + $BaseAllOfBaseAnyOf1BaseNestedAnyOf3BaseOneOfInt deserialize( final Serializers serializers, final Object data, { final FullType specifiedType = FullType.unspecified, @@ -439,10 +464,9 @@ class _$BaseAllOfBaseAnyOfBaseNestedAnyOf3BaseOneOfSerializer try { baseAllOf = serializers.deserialize(data, specifiedType: const FullType(BaseAllOf))! as BaseAllOf; } catch (_) {} - BaseAnyOf? baseAnyOf; + BaseAnyOf1? baseAnyOf1; try { - baseAnyOf = - ((serializers.deserialize(data, specifiedType: const FullType(BaseAnyOf))! as BaseAnyOf)..validateAnyOf()); + baseAnyOf1 = serializers.deserialize(data, specifiedType: const FullType(BaseAnyOf1))! as BaseAnyOf1; } catch (_) {} BaseNestedAnyOf3? baseNestedAnyOf3; try { @@ -454,7 +478,74 @@ class _$BaseAllOfBaseAnyOfBaseNestedAnyOf3BaseOneOfSerializer baseOneOf = ((serializers.deserialize(data, specifiedType: const FullType(BaseOneOf))! as BaseOneOf)..validateOneOf()); } catch (_) {} - return (baseAllOf: baseAllOf, baseAnyOf: baseAnyOf, baseNestedAnyOf3: baseNestedAnyOf3, baseOneOf: baseOneOf); + int? $int; + try { + $int = serializers.deserialize(data, specifiedType: const FullType(int))! as int; + } catch (_) {} + return ( + baseAllOf: baseAllOf, + baseAnyOf1: baseAnyOf1, + baseNestedAnyOf3: baseNestedAnyOf3, + baseOneOf: baseOneOf, + $int: $int + ); + } +} + +typedef $BaseOneOf1Num = ({BaseOneOf1? baseOneOf1, num? $num}); + +extension $BaseOneOf1NumExtension on $BaseOneOf1Num { + List get _values => [baseOneOf1, $num]; + void validateOneOf() => dynamite_utils.validateOneOf(_values); + void validateAnyOf() => dynamite_utils.validateAnyOf(_values); + static Serializer<$BaseOneOf1Num> get serializer => const _$BaseOneOf1NumSerializer(); + static $BaseOneOf1Num fromJson(final Object? json) => _jsonSerializers.deserializeWith(serializer, json)!; + Object? toJson() => _jsonSerializers.serializeWith(serializer, this); +} + +class _$BaseOneOf1NumSerializer implements PrimitiveSerializer<$BaseOneOf1Num> { + const _$BaseOneOf1NumSerializer(); + + @override + Iterable get types => const [$BaseOneOf1Num]; + + @override + String get wireName => r'$BaseOneOf1Num'; + + @override + Object serialize( + final Serializers serializers, + final $BaseOneOf1Num object, { + final FullType specifiedType = FullType.unspecified, + }) { + dynamic value; + value = object.baseOneOf1; + if (value != null) { + return serializers.serialize(value, specifiedType: const FullType(BaseOneOf1))!; + } + value = object.$num; + if (value != null) { + return serializers.serialize(value, specifiedType: const FullType(num))!; + } +// Should not be possible after validation. + throw StateError('Tried to serialize without any value.'); + } + + @override + $BaseOneOf1Num deserialize( + final Serializers serializers, + final Object data, { + final FullType specifiedType = FullType.unspecified, + }) { + BaseOneOf1? baseOneOf1; + try { + baseOneOf1 = serializers.deserialize(data, specifiedType: const FullType(BaseOneOf1))! as BaseOneOf1; + } catch (_) {} + num? $num; + try { + $num = serializers.deserialize(data, specifiedType: const FullType(num))! as num; + } catch (_) {} + return (baseOneOf1: baseOneOf1, $num: $num); } } @@ -472,10 +563,11 @@ final Serializers _serializers = (Serializers().toBuilder() ..add(BaseNestedAllOf.serializer) ..addBuilderFactory(const FullType(BaseNestedOneOf3), BaseNestedOneOf3Builder.new) ..add(BaseNestedOneOf3.serializer) - ..add($BaseAllOfBaseAnyOfBaseNestedOneOf3BaseOneOfExtension.serializer) + ..add($BaseAllOfBaseAnyOfBaseNestedOneOf3BaseOneOf1DoubleExtension.serializer) ..addBuilderFactory(const FullType(BaseNestedAnyOf3), BaseNestedAnyOf3Builder.new) ..add(BaseNestedAnyOf3.serializer) - ..add($BaseAllOfBaseAnyOfBaseNestedAnyOf3BaseOneOfExtension.serializer)) + ..add($BaseAllOfBaseAnyOf1BaseNestedAnyOf3BaseOneOfIntExtension.serializer) + ..add($BaseOneOf1NumExtension.serializer)) .build(); final Serializers _jsonSerializers = (_serializers.toBuilder() diff --git a/packages/dynamite/dynamite_end_to_end_test/lib/nested_ofs.openapi.json b/packages/dynamite/dynamite_end_to_end_test/lib/nested_ofs.openapi.json index 2ff5be50147..1e4cee63149 100644 --- a/packages/dynamite/dynamite_end_to_end_test/lib/nested_ofs.openapi.json +++ b/packages/dynamite/dynamite_end_to_end_test/lib/nested_ofs.openapi.json @@ -132,6 +132,16 @@ } } ] + }, + "NestedOptimizedOneOf": { + "oneOf": [ + { + "type": "integer" + }, + { + "$ref": "#/components/schemas/BaseOneOf" + } + ] } } }, diff --git a/packages/dynamite/dynamite_end_to_end_test/test/nested_ofs_test.dart b/packages/dynamite/dynamite_end_to_end_test/test/nested_ofs_test.dart index 47ed382e173..4376981c864 100644 --- a/packages/dynamite/dynamite_end_to_end_test/test/nested_ofs_test.dart +++ b/packages/dynamite/dynamite_end_to_end_test/test/nested_ofs_test.dart @@ -37,7 +37,8 @@ void main() { ..string = 'BaseAllOfAttributeValue' ..attributeAllOf = 'BaseAllOfAttributeAllOfValue', ), - baseOneOf: null, + $double: null, + baseOneOf1: null, baseAnyOf: null, baseNestedOneOf3: null, ); @@ -48,14 +49,12 @@ void main() { }; expect(object.toJson(), equals(json)); - expect($BaseAllOfBaseAnyOfBaseNestedOneOf3BaseOneOfExtension.fromJson(json), equals(object)); + expect($BaseAllOfBaseAnyOfBaseNestedOneOf3BaseOneOf1DoubleExtension.fromJson(json), equals(object)); object = ( baseAllOf: null, - baseOneOf: ( - $double: null, - baseOneOf1: BaseOneOf1((final b) => b..attributeOneOf = 'baseOneOfAttributeOneOfValue'), - ), + $double: null, + baseOneOf1: BaseOneOf1((final b) => b..attributeOneOf = 'baseOneOfAttributeOneOfValue'), baseAnyOf: null, baseNestedOneOf3: null, ); @@ -65,14 +64,12 @@ void main() { }; expect(object.toJson(), equals(json)); - expect($BaseAllOfBaseAnyOfBaseNestedOneOf3BaseOneOfExtension.fromJson(json), equals(object)); + expect($BaseAllOfBaseAnyOfBaseNestedOneOf3BaseOneOf1DoubleExtension.fromJson(json), equals(object)); object = ( baseAllOf: null, - baseOneOf: ( - $double: 0.368966614163742, - baseOneOf1: null, - ), + $double: 0.368966614163742, + baseOneOf1: null, baseAnyOf: null, baseNestedOneOf3: null, ); @@ -80,11 +77,12 @@ void main() { json = 0.368966614163742; expect(object.toJson(), equals(json)); - expect($BaseAllOfBaseAnyOfBaseNestedOneOf3BaseOneOfExtension.fromJson(json), equals(object)); + expect($BaseAllOfBaseAnyOfBaseNestedOneOf3BaseOneOf1DoubleExtension.fromJson(json), equals(object)); object = ( baseAllOf: null, - baseOneOf: null, + $double: null, + baseOneOf1: null, baseAnyOf: ( $int: null, baseAnyOf1: BaseAnyOf1((final b) => b..attributeAnyOf = 'baseOneOfAttributeAnyOfValue'), @@ -97,11 +95,12 @@ void main() { }; expect(object.toJson(), equals(json)); - expect($BaseAllOfBaseAnyOfBaseNestedOneOf3BaseOneOfExtension.fromJson(json), equals(object)); + expect($BaseAllOfBaseAnyOfBaseNestedOneOf3BaseOneOf1DoubleExtension.fromJson(json), equals(object)); object = ( baseAllOf: null, - baseOneOf: null, + $double: null, + baseOneOf1: null, baseAnyOf: ( $int: 838, baseAnyOf1: null, @@ -112,11 +111,12 @@ void main() { json = 838; expect(object.toJson(), equals(json)); - expect($BaseAllOfBaseAnyOfBaseNestedOneOf3BaseOneOfExtension.fromJson(json), equals(object)); + expect($BaseAllOfBaseAnyOfBaseNestedOneOf3BaseOneOf1DoubleExtension.fromJson(json), equals(object)); object = ( baseAllOf: null, - baseOneOf: null, + $double: null, + baseOneOf1: null, baseAnyOf: null, baseNestedOneOf3: BaseNestedOneOf3( (final b) => b..attributeNestedOneOf = 'BaseNestedOneOf3AttributeNestedOneOfValue', @@ -128,7 +128,7 @@ void main() { }; expect(object.toJson(), equals(json)); - expect($BaseAllOfBaseAnyOfBaseNestedOneOf3BaseOneOfExtension.fromJson(json), equals(object)); + expect($BaseAllOfBaseAnyOfBaseNestedOneOf3BaseOneOf1DoubleExtension.fromJson(json), equals(object)); }); test('BaseNestedAnyOf', () { @@ -139,7 +139,8 @@ void main() { ..attributeAllOf = 'BaseAllOfAttributeAllOfValue', ), baseOneOf: null, - baseAnyOf: null, + baseAnyOf1: null, + $int: null, baseNestedAnyOf3: null, ); @@ -149,7 +150,7 @@ void main() { }; expect(object.toJson(), equals(json)); - expect($BaseAllOfBaseAnyOfBaseNestedAnyOf3BaseOneOfExtension.fromJson(json), equals(object)); + expect($BaseAllOfBaseAnyOf1BaseNestedAnyOf3BaseOneOfIntExtension.fromJson(json), equals(object)); object = ( baseAllOf: null, @@ -157,7 +158,8 @@ void main() { $double: null, baseOneOf1: BaseOneOf1((final b) => b..attributeOneOf = 'baseOneOfAttributeOneOfValue'), ), - baseAnyOf: null, + $int: null, + baseAnyOf1: null, baseNestedAnyOf3: null, ); @@ -166,7 +168,7 @@ void main() { }; expect(object.toJson(), equals(json)); - expect($BaseAllOfBaseAnyOfBaseNestedAnyOf3BaseOneOfExtension.fromJson(json), equals(object)); + expect($BaseAllOfBaseAnyOf1BaseNestedAnyOf3BaseOneOfIntExtension.fromJson(json), equals(object)); object = ( baseAllOf: null, @@ -174,22 +176,21 @@ void main() { $double: 0.6945631603643333, baseOneOf1: null, ), - baseAnyOf: null, + $int: null, + baseAnyOf1: null, baseNestedAnyOf3: null, ); json = 0.6945631603643333; expect(object.toJson(), equals(json)); - expect($BaseAllOfBaseAnyOfBaseNestedAnyOf3BaseOneOfExtension.fromJson(json), equals(object)); + expect($BaseAllOfBaseAnyOf1BaseNestedAnyOf3BaseOneOfIntExtension.fromJson(json), equals(object)); object = ( baseAllOf: null, baseOneOf: null, - baseAnyOf: ( - $int: null, - baseAnyOf1: BaseAnyOf1((final b) => b..attributeAnyOf = 'baseOneOfAttributeAnyOfValue'), - ), + $int: null, + baseAnyOf1: BaseAnyOf1((final b) => b..attributeAnyOf = 'baseOneOfAttributeAnyOfValue'), baseNestedAnyOf3: null, ); @@ -198,27 +199,26 @@ void main() { }; expect(object.toJson(), equals(json)); - expect($BaseAllOfBaseAnyOfBaseNestedAnyOf3BaseOneOfExtension.fromJson(json), equals(object)); + expect($BaseAllOfBaseAnyOf1BaseNestedAnyOf3BaseOneOfIntExtension.fromJson(json), equals(object)); object = ( baseAllOf: null, baseOneOf: null, - baseAnyOf: ( - $int: 659, - baseAnyOf1: null, - ), + $int: 659, + baseAnyOf1: null, baseNestedAnyOf3: null, ); json = 659; expect(object.toJson(), equals(json)); - expect($BaseAllOfBaseAnyOfBaseNestedAnyOf3BaseOneOfExtension.fromJson(json), equals(object)); + expect($BaseAllOfBaseAnyOf1BaseNestedAnyOf3BaseOneOfIntExtension.fromJson(json), equals(object)); object = ( baseAllOf: null, baseOneOf: null, - baseAnyOf: null, + $int: null, + baseAnyOf1: null, baseNestedAnyOf3: BaseNestedAnyOf3( (final b) => b..attributeNestedAnyOf = 'BaseNestedOneOf3AttributeNestedAnyOfValue', ), @@ -229,6 +229,42 @@ void main() { }; expect(object.toJson(), equals(json)); - expect($BaseAllOfBaseAnyOfBaseNestedAnyOf3BaseOneOfExtension.fromJson(json), equals(object)); + expect($BaseAllOfBaseAnyOf1BaseNestedAnyOf3BaseOneOfIntExtension.fromJson(json), equals(object)); + }); + + test('NestedOptimizedOneOf', () { + NestedOptimizedOneOf object = ( + $num: 770, + baseOneOf1: null, + ); + + Object? json = 770; + + expect(object.toJson(), equals(json)); + expect($BaseOneOf1NumExtension.fromJson(json), equals(object)); + + object = ( + $num: 0.8345761329689448, + baseOneOf1: null, + ); + + json = 0.8345761329689448; + + expect(object.toJson(), equals(json)); + expect($BaseOneOf1NumExtension.fromJson(json), equals(object)); + + object = ( + $num: null, + baseOneOf1: BaseOneOf1( + (final b) => b..attributeOneOf = 'NestedOptimizedOneOfBaseOneOf1AttributeOneOfValue', + ), + ); + + json = { + 'attribute-oneOf': 'NestedOptimizedOneOfBaseOneOf1AttributeOneOfValue', + }; + + expect(object.toJson(), equals(json)); + expect($BaseOneOf1NumExtension.fromJson(json), equals(object)); }); }