Skip to content

Commit

Permalink
chore: add test for alias type
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusfccp committed Dec 17, 2024
1 parent f97850f commit c755d53
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion json_annotation/lib/src/json_value.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class JsonValue {
/// Optional values that can be used when deserializing.
///
/// The elements of [aliases] must be either [String] or [int].
final Set<Object?> aliases;
final Set<Object> aliases;

const JsonValue(this.value, {this.aliases = const {}});
}
2 changes: 1 addition & 1 deletion json_serializable/lib/src/enum_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ List<Object?> _generateAliases({
final targetTypeCode = typeToCode(targetType);
throw InvalidGenerationSourceError(
'The `JsonValue` annotation on `$targetTypeCode.${field.name}` aliases '
'should all be of type String, or int.',
'should all be of type String or int.',
element: field,
);
}
Expand Down
1 change: 1 addition & 0 deletions json_serializable/test/json_serializable_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Future<void> main() async {
jsonEnumTestReader,
const JsonEnumGenerator(),
expectedAnnotatedTests: {
'EnumAliasNotSupportType',
'EnumValueIssue1147',
'EnumValueNotAField',
'EnumValueNotSupportType',
Expand Down
10 changes: 10 additions & 0 deletions json_serializable/test/src/_json_enum_test_input.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@
import 'package:json_annotation/json_annotation.dart';
import 'package:source_gen_test/annotations.dart';

@ShouldThrow(
'The `JsonValue` annotation on `EnumAliasNotSupportType.one` aliases should '
'all be of type String or int.',
)
@JsonEnum(alwaysCreate: true)
enum EnumAliasNotSupportType {
@JsonValue('one', aliases: {#one})
one,
}

@ShouldGenerate(r'''
// ignore: unused_element
const _$EnumValueIssue1147EnumMap = {
Expand Down

0 comments on commit c755d53

Please sign in to comment.