Skip to content

Commit 118940b

Browse files
committed
refactor: update @JsonSerializable() Annotations on Classes @ Added @jsonkey(unknownEnumValue: ...) to Enum Fields
1 parent 2b64c87 commit 118940b

24 files changed

+333
-79
lines changed

lib/src/models/auth/permission.dart

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'package:equatable/equatable.dart';
22
import 'package:json_annotation/json_annotation.dart';
3+
import 'package:meta/meta.dart';
34

45
part 'permission.g.dart';
56

@@ -8,7 +9,14 @@ part 'permission.g.dart';
89
///
910
/// Permissions follow the format `resource.action`.
1011
/// {@endtemplate}
11-
@JsonSerializable()
12+
13+
@immutable
14+
@JsonSerializable(
15+
fieldRename: FieldRename.snake,
16+
explicitToJson: true,
17+
includeIfNull: false,
18+
checked: true,
19+
)
1220
class Permission extends Equatable {
1321
/// {@macro permission}
1422
const Permission({required this.name});

lib/src/models/auth/user.dart

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
import 'package:equatable/equatable.dart';
22
import 'package:ht_shared/src/models/auth/user_role.dart';
33
import 'package:json_annotation/json_annotation.dart';
4+
import 'package:meta/meta.dart';
45

56
part 'user.g.dart';
67

78
/// Represents a user within the system.
89
///
910
/// This model holds basic information about the user, including their
1011
/// unique identifier, email (if available), and their assigned [role].
11-
@JsonSerializable()
12+
13+
@immutable
14+
@JsonSerializable(
15+
fieldRename: FieldRename.snake,
16+
explicitToJson: true,
17+
includeIfNull: false,
18+
checked: true,
19+
)
1220
class User extends Equatable {
1321
/// Creates a new [User] instance.
1422
///
@@ -34,6 +42,7 @@ class User extends Equatable {
3442
final String? email;
3543

3644
/// The role of the user.
45+
@JsonKey(unknownEnumValue: JsonKey.nullForUndefinedEnumValue)
3746
final UserRole role;
3847

3948
/// The date and time the user account was created.

lib/src/models/core/feed_item_action.dart

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import 'package:equatable/equatable.dart';
2-
import 'package:ht_shared/ht_shared.dart' show FeedItem;
32
import 'package:ht_shared/src/models/core/content_type.dart';
4-
import 'package:ht_shared/src/models/core/feed_item.dart' show FeedItem;
5-
import 'package:ht_shared/src/models/models.dart' show FeedItem;
6-
// Removed redundant FeedItem imports
73
import 'package:json_annotation/json_annotation.dart';
4+
import 'package:meta/meta.dart';
85

96
part 'feed_item_action.g.dart';
107

@@ -15,7 +12,14 @@ part 'feed_item_action.g.dart';
1512
/// and defined within this library. This allows for exhaustive pattern
1613
/// matching in the UI layer.
1714
/// {@endtemplate}
18-
@JsonSerializable(createFactory: false)
15+
@immutable
16+
@JsonSerializable(
17+
createFactory: false,
18+
fieldRename: FieldRename.snake,
19+
explicitToJson: true,
20+
includeIfNull: false,
21+
checked: true,
22+
)
1923
sealed class FeedItemAction extends Equatable {
2024
/// {@macro feed_item_action}
2125
const FeedItemAction({required this.type});
@@ -66,7 +70,13 @@ Map<String, dynamic> feedItemActionToJson(FeedItemAction action) {
6670
/// {@template open_internal_content}
6771
/// An action to open content that resides within the application.
6872
/// {@endtemplate}
69-
@JsonSerializable()
73+
@immutable
74+
@JsonSerializable(
75+
fieldRename: FieldRename.snake,
76+
explicitToJson: true,
77+
includeIfNull: false,
78+
checked: true,
79+
)
7080
class OpenInternalContent extends FeedItemAction {
7181
/// {@macro open_internal_content}
7282
const OpenInternalContent({
@@ -82,6 +92,7 @@ class OpenInternalContent extends FeedItemAction {
8292
final String contentId;
8393

8494
/// The type of the internal content (e.g., headline, category, source).
95+
@JsonKey(unknownEnumValue: JsonKey.nullForUndefinedEnumValue)
8596
final ContentType contentType;
8697

8798
@override
@@ -99,7 +110,13 @@ class OpenInternalContent extends FeedItemAction {
99110
/// {@template show_interstitial_then_open_internal_content}
100111
/// An action to show an interstitial advertisement, then open internal content.
101112
/// {@endtemplate}
102-
@JsonSerializable()
113+
@immutable
114+
@JsonSerializable(
115+
fieldRename: FieldRename.snake,
116+
explicitToJson: true,
117+
includeIfNull: false,
118+
checked: true,
119+
)
103120
class ShowInterstitialThenOpenInternalContent extends FeedItemAction {
104121
/// {@macro show_interstitial_then_open_internal_content}
105122
const ShowInterstitialThenOpenInternalContent({
@@ -118,6 +135,7 @@ class ShowInterstitialThenOpenInternalContent extends FeedItemAction {
118135
final String contentId;
119136

120137
/// The type of the internal content (e.g., headline, category, source).
138+
@JsonKey(unknownEnumValue: JsonKey.nullForUndefinedEnumValue)
121139
final ContentType contentType;
122140

123141
@override
@@ -134,7 +152,13 @@ class ShowInterstitialThenOpenInternalContent extends FeedItemAction {
134152
/// {@template open_external_url}
135153
/// An action to open an external URL, typically in a web browser.
136154
/// {@endtemplate}
137-
@JsonSerializable()
155+
@immutable
156+
@JsonSerializable(
157+
fieldRename: FieldRename.snake,
158+
explicitToJson: true,
159+
includeIfNull: false,
160+
checked: true,
161+
)
138162
class OpenExternalUrl extends FeedItemAction {
139163
/// {@macro open_external_url}
140164
const OpenExternalUrl({required this.url}) : super(type: 'open_external_url');

lib/src/models/entities/category.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ part 'category.g.dart';
1414
/// The [id] is automatically generated using UUID v4 if not provided.
1515
/// {@endtemplate}
1616
@immutable
17-
@JsonSerializable(explicitToJson: true, includeIfNull: false)
17+
@JsonSerializable(
18+
fieldRename: FieldRename.snake,
19+
explicitToJson: true,
20+
includeIfNull: false,
21+
checked: true,
22+
)
1823
class Category extends FeedItem {
1924
/// {@macro category}
2025
///

lib/src/models/entities/country.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'package:ht_shared/src/models/core/feed_item.dart';
22
import 'package:ht_shared/src/models/core/feed_item_action.dart'
33
show FeedItemAction, feedItemActionFromJson, feedItemActionToJson;
44
import 'package:json_annotation/json_annotation.dart';
5+
import 'package:meta/meta.dart';
56
import 'package:uuid/uuid.dart';
67

78
part 'country.g.dart';
@@ -12,7 +13,13 @@ part 'country.g.dart';
1213
/// Used typically in contexts like selecting a country for news headlines
1314
/// or user profile settings.
1415
/// {@endtemplate}
15-
@JsonSerializable()
16+
@immutable
17+
@JsonSerializable(
18+
fieldRename: FieldRename.snake,
19+
explicitToJson: true,
20+
includeIfNull: false,
21+
checked: true,
22+
)
1623
class Country extends FeedItem {
1724
/// {@macro country}
1825
Country({

lib/src/models/entities/headline.dart

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@ DateTime? _dateTimeFromJson(String? dateString) {
2121
/// Represents a news headline item.
2222
/// {@endtemplate}
2323
@immutable
24-
@JsonSerializable(explicitToJson: true, includeIfNull: false)
24+
@JsonSerializable(
25+
fieldRename: FieldRename.snake,
26+
explicitToJson: true,
27+
includeIfNull: false,
28+
checked: true,
29+
)
2530
class Headline extends FeedItem {
2631
/// {@macro headline}
2732
Headline({
@@ -42,7 +47,7 @@ class Headline extends FeedItem {
4247
action = action,
4348
super(type: 'headline', action: action);
4449

45-
/// Factory method to create a [Headline] instance from a JSON map.
50+
/// Creates a [Headline] instance from a JSON map.
4651
factory Headline.fromJson(Map<String, dynamic> json) =>
4752
_$HeadlineFromJson(json);
4853

@@ -59,11 +64,9 @@ class Headline extends FeedItem {
5964
final String? url;
6065

6166
/// URL to an image associated with the headline.
62-
@JsonKey(name: 'imageUrl')
6367
final String? imageUrl;
6468

6569
/// Date and time when the headline was published.
66-
@JsonKey(name: 'publishedAt', fromJson: _dateTimeFromJson)
6770
final DateTime? publishedAt;
6871

6972
/// Source or origin of the headline.

lib/src/models/entities/source.dart

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'package:ht_shared/src/models/core/feed_item_action.dart'
44
import 'package:ht_shared/src/models/entities/country.dart';
55
import 'package:ht_shared/src/models/entities/source_type.dart';
66
import 'package:json_annotation/json_annotation.dart';
7+
import 'package:meta/meta.dart';
78
import 'package:uuid/uuid.dart';
89

910
part 'source.g.dart';
@@ -13,20 +14,25 @@ part 'source.g.dart';
1314
///
1415
/// Represents a news source.
1516
/// {@endtemplate}
16-
@JsonSerializable(explicitToJson: true, includeIfNull: false)
17+
@immutable
18+
@JsonSerializable(
19+
fieldRename: FieldRename.snake,
20+
explicitToJson: true,
21+
includeIfNull: false,
22+
checked: true,
23+
)
1724
class Source extends FeedItem {
1825
/// {@macro source}
1926
Source({
2027
required this.name,
2128
required this.action,
2229
this.description,
2330
this.url,
24-
this.sourceType, // Changed from _sourceType
31+
this.sourceType,
2532
this.language,
2633
this.headquarters,
2734
String? id,
2835
}) : id = id ?? const Uuid().v4(),
29-
// _sourceType assignment removed, directly using this.sourceType
3036
super(type: 'source', action: action);
3137

3238
/// Factory method to create a [Source] instance from a JSON map.
@@ -48,11 +54,8 @@ class Source extends FeedItem {
4854
/// The type of the source (e.g., newsAgency, blog).
4955
/// If an unknown value is encountered during deserialization,
5056
/// this field will be set to null.
51-
@JsonKey(
52-
// name: 'sourceType' removed as field name now matches JSON key
53-
unknownEnumValue: JsonKey.nullForUndefinedEnumValue,
54-
)
55-
final SourceType? sourceType; // Field renamed to public
57+
@JsonKey(unknownEnumValue: JsonKey.nullForUndefinedEnumValue)
58+
final SourceType? sourceType;
5659

5760
/// The language code of the source (e.g., 'en', 'fr').
5861
final String? language;

lib/src/models/entities/source_type.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import 'package:json_annotation/json_annotation.dart';
22

33
/// Enum representing the type of news source.
4-
@JsonEnum(fieldRename: FieldRename.kebab)
4+
@JsonEnum(fieldRename: FieldRename.snake)
55
enum SourceType {
66
/// A global news agency
77
/// (e.g., Reuters, Associated Press, Agence France-Presse).

lib/src/models/feed_decorators/ad.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'package:ht_shared/src/models/core/feed_item_action.dart'
33
show FeedItemAction, feedItemActionFromJson, feedItemActionToJson;
44
import 'package:ht_shared/src/models/feed_decorators/ad_placement.dart';
55
import 'package:json_annotation/json_annotation.dart';
6+
import 'package:meta/meta.dart';
67
import 'package:uuid/uuid.dart';
78

89
part 'ad.g.dart';
@@ -28,7 +29,13 @@ enum AdType {
2829
/// {@template ad}
2930
/// Represents an advertisement item that can appear in the feed.
3031
/// {@endtemplate}
31-
@JsonSerializable(explicitToJson: true, includeIfNull: false)
32+
@immutable
33+
@JsonSerializable(
34+
fieldRename: FieldRename.snake,
35+
explicitToJson: true,
36+
includeIfNull: false,
37+
checked: true,
38+
)
3239
class Ad extends FeedItem {
3340
/// {@macro ad}
3441
Ad({

lib/src/models/feed_decorators/engagement_content.dart

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'package:ht_shared/src/models/core/feed_item_action.dart'
33
show FeedItemAction, feedItemActionFromJson, feedItemActionToJson;
44
import 'package:ht_shared/src/models/feed_decorators/engagement_content_type.dart';
55
import 'package:json_annotation/json_annotation.dart';
6+
import 'package:meta/meta.dart';
67
import 'package:uuid/uuid.dart';
78

89
part 'engagement_content.g.dart';
@@ -14,7 +15,13 @@ part 'engagement_content.g.dart';
1415
/// or providing feedback. The [engagementContentType] specifies the nature
1516
/// of the call-to-action.
1617
/// {@endtemplate}
17-
@JsonSerializable(explicitToJson: true, includeIfNull: false)
18+
@immutable
19+
@JsonSerializable(
20+
fieldRename: FieldRename.snake,
21+
explicitToJson: true,
22+
includeIfNull: false,
23+
checked: true,
24+
)
1825
class EngagementContent extends FeedItem {
1926
/// {@macro engagement_content}
2027
EngagementContent({
@@ -84,31 +91,21 @@ class EngagementContent extends FeedItem {
8491
EngagementContent copyWith({
8592
String? id,
8693
String? title,
87-
Object? description = const _Sentinel(),
88-
Object? engagementContentType = const _Sentinel(),
89-
Object? callToActionText = const _Sentinel(),
90-
Object? callToActionUrl = const _Sentinel(),
94+
String? description,
95+
EngagementContentType? engagementContentType,
96+
String? callToActionText,
97+
String? callToActionUrl,
9198
FeedItemAction? action,
9299
}) {
93100
return EngagementContent(
94101
id: id ?? this.id,
95102
title: title ?? this.title,
96-
description:
97-
description is _Sentinel ? this.description : description as String?,
98-
engagementContentType: engagementContentType is _Sentinel
99-
? this.engagementContentType
100-
: engagementContentType as EngagementContentType?,
101-
callToActionText: callToActionText is _Sentinel
102-
? this.callToActionText
103-
: callToActionText as String?,
104-
callToActionUrl: callToActionUrl is _Sentinel
105-
? this.callToActionUrl
106-
: callToActionUrl as String?,
103+
description: description ?? this.description,
104+
engagementContentType:
105+
engagementContentType ?? this.engagementContentType,
106+
callToActionText: callToActionText ?? this.callToActionText,
107+
callToActionUrl: callToActionUrl ?? this.callToActionUrl,
107108
action: action ?? this.action,
108109
);
109110
}
110111
}
111-
112-
class _Sentinel {
113-
const _Sentinel();
114-
}

0 commit comments

Comments
 (0)