Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions json_serializable/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.2.5

* Throw an exception if a duplicate JSON key is detected.

## 0.2.4+1

* Throw a more helpful error when a constructor is missing.
Expand Down
16 changes: 15 additions & 1 deletion json_serializable/lib/src/json_serializable_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class JsonSerializableGenerator
// Explicitly using `LinkedHashMap` – we want these ordered.
var fields = new LinkedHashMap<String, FieldElement>.fromIterable(
fieldsList,
key: (FieldElement f) => f.name);
key: (f) => (f as FieldElement).name);

// Get the constructor to use for the factory

Expand All @@ -116,6 +116,20 @@ class JsonSerializableGenerator
}
}

// Now we check for duplicate JSON keys due to colliding annotations.
// We do this now, since we have a final field list after any pruning done
// by `createFactory`.

fields.values.fold(new Set<String>(), (Set<String> set, fe) {
var jsonKey = _jsonKeyFor(fe).name ?? fe.name;
if (!set.add(jsonKey)) {
throw new InvalidGenerationSourceError(
'More than one field has the JSON key `$jsonKey`.',
todo: 'Check the `JsonKey` annotations on fields.');
}
return set;
});

if (annotation.read('createToJson').boolValue) {
//
// Generate the mixin class
Expand Down
43 changes: 39 additions & 4 deletions json_serializable/test/json_serializable_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,29 @@ abstract class _$OrderSerializerMixin {
});
});

test('reads JsonKey annotations', () async {
var output = await _runForElementNamed('Person');
group('JsonKey', () {
test('works to change the name of a field', () async {
var output = await _runForElementNamed('Person');

expect(output, contains("'h': height,"));
expect(output, contains("..height = json['h']"));
});

expect(output, contains("'h': height,"));
expect(output, contains("..height = json['h']"));
test('works to change the name of a field', () async {
expect(
() => _runForElementNamed('KeyDupesField'),
throwsInvalidGenerationSourceError(
'More than one field has the JSON key `str`.',
'Check the `JsonKey` annotations on fields.'));
});

test('works to change the name of a field', () async {
expect(
() => _runForElementNamed('DupeKeys'),
throwsInvalidGenerationSourceError(
'More than one field has the JSON key `a`.',
'Check the `JsonKey` annotations on fields.'));
});
});

group('includeIfNull', () {
Expand Down Expand Up @@ -366,4 +384,21 @@ class NoCtorClass {

factory TestDoneEvent.fromJson(Map<String, dynamic> json) => null;
}

@JsonSerializable(createFactory: false)
class KeyDupesField {
@JsonKey(name: 'str')
int number;

String str;
}

@JsonSerializable(createFactory: false)
class DupeKeys {
@JsonKey(name: 'a')
int number;

@JsonKey(name: 'a')
String str;
}
''';