Skip to content

Commit

Permalink
test(dynamite_end_to_end_test): Add test for oneOf with array
Browse files Browse the repository at this point in the history
Signed-off-by: jld3103 <[email protected]>
  • Loading branch information
provokateurin committed Dec 22, 2023
1 parent 119dfe9 commit 2876480
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -100,6 +101,8 @@ typedef MixedOneOf = ({MixedOneOf1? mixedOneOf1, String? string});

typedef OneOfIntDoubleOther = ({num? $num, String? string});

typedef OneOfIntArrayInt = ({BuiltList<int>? builtListInt, int? $int});

typedef $ObjectOneOf0ObjectOneOf1 = ({ObjectOneOf0? objectOneOf0, ObjectOneOf1? objectOneOf1});

extension $ObjectOneOf0ObjectOneOf1Extension on $ObjectOneOf0ObjectOneOf1 {
Expand Down Expand Up @@ -271,6 +274,64 @@ class _$NumStringSerializer implements PrimitiveSerializer<$NumString> {
}
}

typedef $BuiltListInt = ({BuiltList<int>? builtListInt, int? $int});

extension $BuiltListIntExtension on $BuiltListInt {
List<dynamic> 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<Type> 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<int>? builtListInt;
try {
builtListInt =
serializers.deserialize(data, specifiedType: const FullType(BuiltList, [FullType(int)]))! as BuiltList<int>;
} 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()
Expand All @@ -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<int>.new)
..add($BuiltListIntExtension.serializer))
.build();

@visibleForTesting
Expand Down
14 changes: 14 additions & 0 deletions packages/dynamite/dynamite_end_to_end_test/lib/one_of.openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
]
}
}
},
Expand Down
23 changes: 23 additions & 0 deletions packages/dynamite/dynamite_end_to_end_test/test/one_of_test.dart
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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));
});
}

0 comments on commit 2876480

Please sign in to comment.