Skip to content

Commit 81ec08f

Browse files
authored
feat(cloud_firestore_odm): add support for json_serializable's field rename/property ignore (#9030)
1 parent f3a6bdc commit 81ec08f

File tree

15 files changed

+2498
-87
lines changed

15 files changed

+2498
-87
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
targets:
2+
$default:
3+
builders:
4+
json_serializable:
5+
options:
6+
create_field_map: true
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import 'package:cloud_firestore/cloud_firestore.dart';
2+
import 'package:cloud_firestore_odm/cloud_firestore_odm.dart';
3+
import 'package:json_annotation/json_annotation.dart';
4+
5+
part 'integration.g.dart';
6+
7+
@JsonSerializable()
8+
class EmptyModel {
9+
EmptyModel();
10+
11+
factory EmptyModel.fromJson(Map<String, dynamic> json) =>
12+
_$EmptyModelFromJson(json);
13+
14+
Map<String, dynamic> toJson() => _$EmptyModelToJson(this);
15+
}
16+
17+
@Collection<EmptyModel>('firestore-example-app/test/config')
18+
final emptyModelRef = EmptyModelCollectionReference();
19+
20+
@Collection<ManualJson>('root')
21+
class ManualJson {
22+
ManualJson(this.value);
23+
24+
factory ManualJson.fromJson(Map<String, Object?> json) {
25+
return ManualJson(json['value']! as String);
26+
}
27+
28+
final String value;
29+
30+
Map<String, Object?> toJson() => {'value': value};
31+
}
32+
33+
@Collection<AdvancedJson>('firestore-example-app/test/advanced')
34+
@JsonSerializable(fieldRename: FieldRename.snake)
35+
class AdvancedJson {
36+
AdvancedJson({this.firstName, this.lastName, this.ignored});
37+
38+
final String? firstName;
39+
40+
@JsonKey(name: 'LAST_NAME')
41+
final String? lastName;
42+
43+
@JsonKey(ignore: true)
44+
final String? ignored;
45+
46+
Map<String, Object?> toJson() => _$AdvancedJsonToJson(this);
47+
48+
@override
49+
bool operator ==(Object other) {
50+
return other is AdvancedJson &&
51+
other.lastName == lastName &&
52+
other.firstName == firstName &&
53+
other.ignored == ignored;
54+
}
55+
56+
@override
57+
int get hashCode => Object.hashAll([firstName, lastName, ignored]);
58+
}
59+
60+
// This tests that the generated code compiles
61+
@Collection<_PrivateAdvancedJson>('firestore-example-app/test/private-advanced')
62+
@JsonSerializable(fieldRename: FieldRename.snake)
63+
class _PrivateAdvancedJson {
64+
_PrivateAdvancedJson({
65+
this.firstName,
66+
this.lastName,
67+
// ignore: unused_element
68+
this.ignored,
69+
});
70+
71+
final String? firstName;
72+
73+
@JsonKey(name: 'LAST_NAME')
74+
final String? lastName;
75+
76+
@JsonKey(ignore: true)
77+
final String? ignored;
78+
79+
Map<String, Object?> toJson() => _$PrivateAdvancedJsonToJson(this);
80+
81+
@override
82+
bool operator ==(Object other) {
83+
return other is AdvancedJson &&
84+
other.lastName == lastName &&
85+
other.firstName == firstName &&
86+
other.ignored == ignored;
87+
}
88+
89+
@override
90+
int get hashCode => Object.hashAll([firstName, lastName, ignored]);
91+
}

0 commit comments

Comments
 (0)