From b08a7a45327301713cc04fd84f7dfe8ec71f3d4e Mon Sep 17 00:00:00 2001 From: David Morgan Date: Thu, 14 Aug 2025 10:57:24 +0200 Subject: [PATCH] Stop using source_gen TypeChecker.fromRuntime. --- json_serializable/CHANGELOG.md | 2 ++ json_serializable/lib/src/enum_utils.dart | 8 ++++++-- json_serializable/lib/src/field_helpers.dart | 6 +++++- json_serializable/lib/src/json_enum_generator.dart | 2 +- json_serializable/lib/src/json_key_utils.dart | 3 ++- json_serializable/lib/src/json_literal_generator.dart | 2 +- .../lib/src/json_serializable_generator.dart | 3 ++- .../lib/src/type_helpers/json_converter_helper.dart | 5 ++++- json_serializable/lib/src/type_helpers/json_helper.dart | 3 ++- json_serializable/lib/src/utils.dart | 5 ++++- json_serializable/pubspec.yaml | 2 +- 11 files changed, 30 insertions(+), 11 deletions(-) diff --git a/json_serializable/CHANGELOG.md b/json_serializable/CHANGELOG.md index cbd31784..9e9633d3 100644 --- a/json_serializable/CHANGELOG.md +++ b/json_serializable/CHANGELOG.md @@ -5,6 +5,8 @@ - Require `dart_style: ^3.0.0` - Require `meta: ^1.15.0` - Require `source_helper: ^1.3.6` +- Require `source_gen: ^3.1.0`, stop using deprecated `TypeChecker.fromRuntime` + and use the new `TypeChecker.typeNamed` instead. ## 6.10.0 diff --git a/json_serializable/lib/src/enum_utils.dart b/json_serializable/lib/src/enum_utils.dart index 77518c13..394e5974 100644 --- a/json_serializable/lib/src/enum_utils.dart +++ b/json_serializable/lib/src/enum_utils.dart @@ -83,8 +83,9 @@ Object? _generateEntry({ required JsonEnum jsonEnum, required DartType targetType, }) { - final annotation = const TypeChecker.fromRuntime( + final annotation = const TypeChecker.typeNamed( JsonValue, + inPackage: 'json_annotation', ).firstAnnotationOfExact(field); if (annotation == null) { @@ -144,7 +145,10 @@ Object? _generateEntry({ } } -const _jsonEnumChecker = TypeChecker.fromRuntime(JsonEnum); +const _jsonEnumChecker = TypeChecker.typeNamed( + JsonEnum, + inPackage: 'json_annotation', +); JsonEnum _fromAnnotation(DartObject? dartObject) { if (dartObject == null) { diff --git a/json_serializable/lib/src/field_helpers.dart b/json_serializable/lib/src/field_helpers.dart index c2de4ca5..d2053ac1 100644 --- a/json_serializable/lib/src/field_helpers.dart +++ b/json_serializable/lib/src/field_helpers.dart @@ -107,4 +107,8 @@ List createSortedFieldSet(ClassElement2 element) { return fields.map((fs) => fs.field).toList(growable: false); } -const _dartCoreObjectChecker = TypeChecker.fromRuntime(Object); +const _dartCoreObjectChecker = TypeChecker.typeNamed( + Object, + inPackage: 'core', + inSdk: true, +); diff --git a/json_serializable/lib/src/json_enum_generator.dart b/json_serializable/lib/src/json_enum_generator.dart index 2c6fb353..003a1d5f 100644 --- a/json_serializable/lib/src/json_enum_generator.dart +++ b/json_serializable/lib/src/json_enum_generator.dart @@ -10,7 +10,7 @@ import 'package:source_gen/source_gen.dart'; import 'enum_utils.dart'; class JsonEnumGenerator extends GeneratorForAnnotation { - const JsonEnumGenerator(); + const JsonEnumGenerator() : super(inPackage: 'json_annotation'); @override List generateForAnnotatedElement( diff --git a/json_serializable/lib/src/json_key_utils.dart b/json_serializable/lib/src/json_key_utils.dart index 99ea0284..24cefd62 100644 --- a/json_serializable/lib/src/json_key_utils.dart +++ b/json_serializable/lib/src/json_key_utils.dart @@ -365,6 +365,7 @@ bool _interfaceTypesEqual(DartType a, DartType b) { const jsonKeyNullForUndefinedEnumValueFieldName = 'JsonKey.nullForUndefinedEnumValue'; -final _nullAsUnknownChecker = TypeChecker.fromRuntime( +final _nullAsUnknownChecker = TypeChecker.typeNamed( JsonKey.nullForUndefinedEnumValue.runtimeType, + inPackage: 'json_annotation', ); diff --git a/json_serializable/lib/src/json_literal_generator.dart b/json_serializable/lib/src/json_literal_generator.dart index 9ce34d13..91e351e5 100644 --- a/json_serializable/lib/src/json_literal_generator.dart +++ b/json_serializable/lib/src/json_literal_generator.dart @@ -13,7 +13,7 @@ import 'package:source_gen/source_gen.dart'; import 'package:source_helper/source_helper.dart'; class JsonLiteralGenerator extends GeneratorForAnnotation { - const JsonLiteralGenerator(); + const JsonLiteralGenerator() : super(inPackage: 'json_annotation'); @override Future generateForAnnotatedElement( diff --git a/json_serializable/lib/src/json_serializable_generator.dart b/json_serializable/lib/src/json_serializable_generator.dart index 32b2796b..37966983 100644 --- a/json_serializable/lib/src/json_serializable_generator.dart +++ b/json_serializable/lib/src/json_serializable_generator.dart @@ -22,7 +22,8 @@ class JsonSerializableGenerator JsonSerializable get config => _settings.config.toJsonSerializable(); - JsonSerializableGenerator.fromSettings(this._settings); + JsonSerializableGenerator.fromSettings(this._settings) + : super(inPackage: 'json_annotation'); /// Creates an instance of [JsonSerializableGenerator]. /// diff --git a/json_serializable/lib/src/type_helpers/json_converter_helper.dart b/json_serializable/lib/src/type_helpers/json_converter_helper.dart index 00ba9042..ca19bd43 100644 --- a/json_serializable/lib/src/type_helpers/json_converter_helper.dart +++ b/json_serializable/lib/src/type_helpers/json_converter_helper.dart @@ -331,4 +331,7 @@ _ConverterMatch? _compatibleMatch( return null; } -const _jsonConverterChecker = TypeChecker.fromRuntime(JsonConverter); +const _jsonConverterChecker = TypeChecker.typeNamed( + JsonConverter, + inPackage: 'json_annotation', +); diff --git a/json_serializable/lib/src/type_helpers/json_helper.dart b/json_serializable/lib/src/type_helpers/json_helper.dart index c2cd52a4..56273811 100644 --- a/json_serializable/lib/src/type_helpers/json_helper.dart +++ b/json_serializable/lib/src/type_helpers/json_helper.dart @@ -279,8 +279,9 @@ ClassConfig? _annotation(ClassConfig config, InterfaceType source) { if (source.isEnum) { return null; } - final annotations = const TypeChecker.fromRuntime( + final annotations = const TypeChecker.typeNamed( JsonSerializable, + inPackage: 'json_annotation', ).annotationsOfExact(source.element3, throwOnUnresolved: false).toList(); if (annotations.isEmpty) { diff --git a/json_serializable/lib/src/utils.dart b/json_serializable/lib/src/utils.dart index 132207ef..1f9c833e 100644 --- a/json_serializable/lib/src/utils.dart +++ b/json_serializable/lib/src/utils.dart @@ -12,7 +12,10 @@ import 'package:source_helper/source_helper.dart'; import 'shared_checkers.dart'; import 'type_helpers/config_types.dart'; -const _jsonKeyChecker = TypeChecker.fromRuntime(JsonKey); +const _jsonKeyChecker = TypeChecker.typeNamed( + JsonKey, + inPackage: 'json_annotation', +); /// If an annotation exists on `element` the source is a 'real' field. /// If the result is `null`, check the getter – it is a property. diff --git a/json_serializable/pubspec.yaml b/json_serializable/pubspec.yaml index c6e15f77..b22c0c17 100644 --- a/json_serializable/pubspec.yaml +++ b/json_serializable/pubspec.yaml @@ -28,7 +28,7 @@ dependencies: path: ^1.9.0 pub_semver: ^2.1.4 pubspec_parse: ^1.0.0 - source_gen: ^3.0.0 + source_gen: ^3.1.0 source_helper: ^1.3.6 dev_dependencies: