Skip to content

Commit

Permalink
fixes #274
Browse files Browse the repository at this point in the history
  • Loading branch information
rrousselGit committed Oct 11, 2020
1 parent 11ab8f7 commit acdb8b7
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
2 changes: 2 additions & 0 deletions packages/freezed/CHANGELOG.md
Expand Up @@ -4,6 +4,8 @@
- fixed a bug where a custom `fromJson` constructor still generated the `fromJson`
constructor with json_serializable
(see also https://github.com/rrousselGit/freezed/issues/280)
- fixed a generation issue when `unionKey` contains special characters
(see also https://github.com/rrousselGit/freezed/issues/274)

# 0.12.1

Expand Down
8 changes: 5 additions & 3 deletions packages/freezed/lib/src/freezed_generator.dart
Expand Up @@ -494,10 +494,12 @@ class FreezedGenerator extends ParserGenerator<_GlobalData, Data, Freezed> {
final annotation = const TypeChecker.fromRuntime(Freezed)
.firstAnnotationOf(element, throwOnUnresolved: false);

return Freezed(
unionKey: annotation.getField('unionKey')?.toStringValue() ??
final rawUnionKey = annotation.getField('unionKey')?.toStringValue() ??
configs['union_key']?.toString() ??
'runtimeType',
'runtimeType';

return Freezed(
unionKey: rawUnionKey.replaceAll("'", r"\'").replaceAll(r'$', r'\$'),
);
}

Expand Down
18 changes: 18 additions & 0 deletions packages/freezed/test/integration/json.dart
Expand Up @@ -23,6 +23,24 @@ abstract class Regression280n2 with _$Regression280n2 {
}
}

@Freezed(unionKey: 'ty"\'pe')
abstract class FancyCustomKey with _$FancyCustomKey {
const factory FancyCustomKey.first(int a) = _FancyCustomKeyFirst;
const factory FancyCustomKey.second(int a) = _FancyCustomKeySecond;

factory FancyCustomKey.fromJson(Map<String, dynamic> json) =>
_$FancyCustomKeyFromJson(json);
}

@Freezed(unionKey: r'$type')
abstract class RawCustomKey with _$RawCustomKey {
const factory RawCustomKey.first(int a) = _RawCustomKeyFirst;
const factory RawCustomKey.second(int a) = _RawCustomKeySecond;

factory RawCustomKey.fromJson(Map<String, dynamic> json) =>
_$RawCustomKeyFromJson(json);
}

@Freezed(unionKey: 'type')
abstract class CustomKey with _$CustomKey {
const factory CustomKey.first(int a) = _CustomKeyFirst;
Expand Down
20 changes: 20 additions & 0 deletions packages/freezed/test/json_test.dart
Expand Up @@ -26,6 +26,16 @@ Future<void> main() async {
CustomKey.fromJson(<String, dynamic>{'type': 'second', 'a': 21}),
CustomKey.second(21),
);

expect(
RawCustomKey.fromJson(<String, dynamic>{'\$type': 'first', 'a': 42}),
RawCustomKey.first(42),
);

expect(
FancyCustomKey.fromJson(<String, dynamic>{'ty"\'pe': 'first', 'a': 42}),
FancyCustomKey.first(42),
);
});

test('toJson', () {
Expand All @@ -38,6 +48,16 @@ Future<void> main() async {
CustomKey.second(21).toJson(),
<String, dynamic>{'type': 'second', 'a': 21},
);

expect(
RawCustomKey.first(42).toJson(),
<String, dynamic>{'\$type': 'first', 'a': 42},
);

expect(
FancyCustomKey.first(42).toJson(),
<String, dynamic>{'ty"\'pe': 'first', 'a': 42},
);
});
});

Expand Down

0 comments on commit acdb8b7

Please sign in to comment.