Skip to content

Commit 048fb3d

Browse files
committed
test: add integration tests and refine examples
1 parent b9376ff commit 048fb3d

File tree

7 files changed

+803
-370
lines changed

7 files changed

+803
-370
lines changed

example/lib/complex_sealed_class_examples.dart

Lines changed: 24 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -3,77 +3,41 @@ import 'package:json_annotation/json_annotation.dart';
33
part 'complex_sealed_class_examples.g.dart';
44

55
@JsonSerializable(
6-
unionDiscriminator: 'base_base_base_type',
6+
unionDiscriminator: 'organization',
77
)
8-
sealed class BaseBaseBaseType {
9-
BaseBaseBaseType();
8+
sealed class Organization {
9+
final String name;
1010

11-
factory BaseBaseBaseType.fromJson(Map<String, dynamic> json) =>
12-
_$BaseBaseBaseTypeFromJson(json);
11+
Organization({required this.name});
1312

14-
Map<String, dynamic> toJson() => _$BaseBaseBaseTypeToJson(this);
15-
}
13+
factory Organization.fromJson(Map<String, dynamic> json) =>
14+
_$OrganizationFromJson(json);
1615

17-
@JsonSerializable(
18-
unionDiscriminator: 'base_base_type',
19-
)
20-
sealed class BaseBase extends BaseBaseBaseType {
21-
BaseBase();
16+
Map<String, dynamic> toJson() => _$OrganizationToJson(this);
2217
}
2318

2419
@JsonSerializable(
25-
unionDiscriminator: 'base_type',
20+
unionDiscriminator: 'department',
2621
)
27-
sealed class Base extends BaseBase {
28-
Base();
29-
}
22+
sealed class Department extends Organization {
23+
final String departmentHead;
3024

31-
@JsonSerializable()
32-
class FirstBaseImpl extends Base {
33-
FirstBaseImpl(this.value);
25+
Department({
26+
required this.departmentHead,
27+
required super.name,
28+
});
3429

35-
String value;
30+
factory Department.fromJson(Map<String, dynamic> json) =>
31+
_$DepartmentFromJson(json);
3632
}
3733

3834
@JsonSerializable()
39-
class SecondBaseImpl extends Base {
40-
SecondBaseImpl(this.value);
41-
42-
String value;
43-
}
44-
45-
@JsonSerializable()
46-
class BaseBaseImpl extends BaseBase {
47-
BaseBaseImpl(this.value);
48-
49-
String value;
50-
}
51-
52-
@JsonSerializable(
53-
createToJson: false,
54-
)
55-
sealed class SecondBase {
56-
SecondBase();
57-
58-
factory SecondBase.fromJson(Map<String, dynamic> json) =>
59-
_$SecondBaseFromJson(json);
60-
}
61-
62-
@JsonSerializable(
63-
createToJson: false,
64-
)
65-
sealed class ThirdBase {
66-
ThirdBase();
67-
68-
factory ThirdBase.fromJson(Map<String, dynamic> json) =>
69-
_$ThirdBaseFromJson(json);
70-
}
71-
72-
@JsonSerializable(
73-
createToJson: false,
74-
)
75-
class ImplAll implements SecondBase, ThirdBase {
76-
ImplAll(this.value);
77-
78-
String value;
35+
class Team extends Department {
36+
final String teamLead;
37+
38+
Team({
39+
required this.teamLead,
40+
required super.departmentHead,
41+
required super.name,
42+
});
7943
}

example/lib/complex_sealed_class_examples.g.dart

Lines changed: 27 additions & 93 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/lib/sealed_class_example.dart

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,36 @@ import 'package:json_annotation/json_annotation.dart';
33
part 'sealed_class_example.g.dart';
44

55
@JsonSerializable(
6-
unionDiscriminator: 'runtimeType',
6+
unionDiscriminator: 'vehicle_type',
77
unionRename: UnionRename.snake,
88
)
9-
sealed class MySealedClass {
10-
MySealedClass(this.value);
9+
sealed class Vehicle {
10+
final String vehicleID;
1111

12-
factory MySealedClass.fromJson(Map<String, dynamic> json) =>
13-
_$MySealedClassFromJson(json);
12+
Vehicle({required this.vehicleID});
1413

15-
String value;
14+
factory Vehicle.fromJson(Map<String, dynamic> json) =>
15+
_$VehicleFromJson(json);
1616

17-
Map<String, dynamic> toJson() => _$MySealedClassToJson(this);
17+
Map<String, dynamic> toJson() => _$VehicleToJson(this);
1818
}
1919

2020
@JsonSerializable()
21-
class FirstSubtype extends MySealedClass {
22-
final String someAttribute;
21+
class Car extends Vehicle {
22+
final int numberOfDoors;
2323

24-
FirstSubtype(
25-
this.someAttribute,
26-
super.value,
27-
);
24+
Car({
25+
required this.numberOfDoors,
26+
required super.vehicleID,
27+
});
2828
}
2929

3030
@JsonSerializable()
31-
class SecondSubtype extends MySealedClass {
32-
final String someOtherAttribute;
31+
class Bicycle extends Vehicle {
32+
final bool hasBell;
3333

34-
SecondSubtype(
35-
this.someOtherAttribute,
36-
super.value,
37-
);
34+
Bicycle({
35+
required this.hasBell,
36+
required super.vehicleID,
37+
});
3838
}

example/lib/sealed_class_example.g.dart

Lines changed: 25 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)