diff --git a/packages/dynamite/dynamite_end_to_end_test/lib/one_of.openapi.dart b/packages/dynamite/dynamite_end_to_end_test/lib/one_of.openapi.dart index 8f39cc9c694..217196b8ef5 100644 --- a/packages/dynamite/dynamite_end_to_end_test/lib/one_of.openapi.dart +++ b/packages/dynamite/dynamite_end_to_end_test/lib/one_of.openapi.dart @@ -5,6 +5,7 @@ // ignore_for_file: unused_element // ignore_for_file: no_leading_underscores_for_local_identifiers +import 'package:built_collection/built_collection.dart'; import 'package:built_value/built_value.dart'; import 'package:built_value/serializer.dart'; import 'package:built_value/standard_json_plugin.dart'; @@ -100,6 +101,8 @@ typedef MixedOneOf = ({MixedOneOf1? mixedOneOf1, String? string}); typedef OneOfIntDoubleOther = ({num? $num, String? string}); +typedef OneOfIntArrayInt = ({BuiltList? builtListInt, int? $int}); + typedef $ObjectOneOf0ObjectOneOf1 = ({ObjectOneOf0? objectOneOf0, ObjectOneOf1? objectOneOf1}); extension $ObjectOneOf0ObjectOneOf1Extension on $ObjectOneOf0ObjectOneOf1 { @@ -271,6 +274,64 @@ class _$NumStringSerializer implements PrimitiveSerializer<$NumString> { } } +typedef $BuiltListInt = ({BuiltList? builtListInt, int? $int}); + +extension $BuiltListIntExtension on $BuiltListInt { + List get _values => [builtListInt, $int]; + void validateOneOf() => dynamite_utils.validateOneOf(_values); + void validateAnyOf() => dynamite_utils.validateAnyOf(_values); + static Serializer<$BuiltListInt> get serializer => const _$BuiltListIntSerializer(); + static $BuiltListInt fromJson(Object? json) => jsonSerializers.deserializeWith(serializer, json)!; + Object? toJson() => jsonSerializers.serializeWith(serializer, this); +} + +class _$BuiltListIntSerializer implements PrimitiveSerializer<$BuiltListInt> { + const _$BuiltListIntSerializer(); + + @override + Iterable get types => const [$BuiltListInt]; + + @override + String get wireName => r'$BuiltListInt'; + + @override + Object serialize( + Serializers serializers, + $BuiltListInt object, { + FullType specifiedType = FullType.unspecified, + }) { + dynamic value; + value = object.builtListInt; + if (value != null) { + return serializers.serialize(value, specifiedType: const FullType(BuiltList, [FullType(int)]))!; + } + 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 + $BuiltListInt deserialize( + Serializers serializers, + Object data, { + FullType specifiedType = FullType.unspecified, + }) { + BuiltList? builtListInt; + try { + builtListInt = + serializers.deserialize(data, specifiedType: const FullType(BuiltList, [FullType(int)]))! as BuiltList; + } catch (_) {} + int? $int; + try { + $int = serializers.deserialize(data, specifiedType: const FullType(int))! as int; + } catch (_) {} + return (builtListInt: builtListInt, $int: $int); + } +} + // coverage:ignore-start @visibleForTesting final Serializers serializers = (Serializers().toBuilder() @@ -284,7 +345,9 @@ final Serializers serializers = (Serializers().toBuilder() ..add($MixedOneOf1StringExtension.serializer) ..addBuilderFactory(const FullType(OneObjectOneOf0), OneObjectOneOf0Builder.new) ..add(OneObjectOneOf0.serializer) - ..add($NumStringExtension.serializer)) + ..add($NumStringExtension.serializer) + ..addBuilderFactory(const FullType(BuiltList, [FullType(int)]), ListBuilder.new) + ..add($BuiltListIntExtension.serializer)) .build(); @visibleForTesting diff --git a/packages/dynamite/dynamite_end_to_end_test/lib/one_of.openapi.json b/packages/dynamite/dynamite_end_to_end_test/lib/one_of.openapi.json index e1f9e404ae8..3e557f9f3b9 100644 --- a/packages/dynamite/dynamite_end_to_end_test/lib/one_of.openapi.json +++ b/packages/dynamite/dynamite_end_to_end_test/lib/one_of.openapi.json @@ -117,6 +117,20 @@ "type": "string" } ] + }, + "OneOfIntArrayInt": { + "description": "One of with an integer and an array of integer.", + "oneOf": [ + { + "type": "integer" + }, + { + "type": "array", + "items": { + "type": "integer" + } + } + ] } } }, diff --git a/packages/dynamite/dynamite_end_to_end_test/test/one_of_test.dart b/packages/dynamite/dynamite_end_to_end_test/test/one_of_test.dart index 8069f9a84e4..cb56a341b66 100644 --- a/packages/dynamite/dynamite_end_to_end_test/test/one_of_test.dart +++ b/packages/dynamite/dynamite_end_to_end_test/test/one_of_test.dart @@ -1,3 +1,4 @@ +import 'package:built_collection/built_collection.dart'; import 'package:dynamite_end_to_end_test/one_of.openapi.dart'; import 'package:test/test.dart'; @@ -110,4 +111,26 @@ void main() { expect(object.toJson(), equals(json)); expect($NumStringExtension.fromJson(json), equals(object)); }); + + test('OneOfIntArrayInt', () { + OneOfIntArrayInt object = ( + builtListInt: null, + $int: 2, + ); + + Object? json = 2; + + expect(object.toJson(), equals(json)); + expect($BuiltListIntExtension.fromJson(json), equals(object)); + + object = ( + builtListInt: BuiltList.from([2]), + $int: null, + ); + + json = [2]; + + expect(object.toJson(), equals(json)); + expect($BuiltListIntExtension.fromJson(json), equals(object)); + }); }