From 54ca5a6351f43a29a2385e5a5a133708a4a1c18f Mon Sep 17 00:00:00 2001 From: David Morgan Date: Tue, 21 Oct 2025 13:45:55 +0200 Subject: [PATCH 1/2] Allow an option called `run_only_if_triggered`. --- json_serializable/CHANGELOG.md | 5 +++++ json_serializable/lib/builder.dart | 7 ++++++- json_serializable/pubspec.yaml | 2 +- json_serializable/test/config_test.dart | 5 +++++ 4 files changed, 17 insertions(+), 2 deletions(-) diff --git a/json_serializable/CHANGELOG.md b/json_serializable/CHANGELOG.md index ca0f09f8..acba200b 100644 --- a/json_serializable/CHANGELOG.md +++ b/json_serializable/CHANGELOG.md @@ -1,3 +1,8 @@ +## 6.12.0-wip + +- Allow `run_only_if_triggered` to be specified in `build.yaml` to turn on the + `build_runner` triggers heuristic. + ## 6.11.1 - Allow `build: '>=3.0.0 <5.0.0'`. diff --git a/json_serializable/lib/builder.dart b/json_serializable/lib/builder.dart index 9c90094c..9e39c3d1 100644 --- a/json_serializable/lib/builder.dart +++ b/json_serializable/lib/builder.dart @@ -23,7 +23,12 @@ import 'src/json_part_builder.dart'; /// Not meant to be invoked by hand-authored code. Builder jsonSerializable(BuilderOptions options) { try { - final config = JsonSerializable.fromJson(options.config); + var configJson = options.config; + // Ignore `run_only_if_triggered` if present, it's used by `build_runner`. + if (configJson.containsKey('run_only_if_triggered')) { + configJson = Map.of(configJson)..remove('run_only_if_triggered'); + } + final config = JsonSerializable.fromJson(configJson); return jsonPartBuilder(config: config); } on CheckedFromJsonException catch (e) { final lines = [ diff --git a/json_serializable/pubspec.yaml b/json_serializable/pubspec.yaml index f6884b8c..7ea611b3 100644 --- a/json_serializable/pubspec.yaml +++ b/json_serializable/pubspec.yaml @@ -1,5 +1,5 @@ name: json_serializable -version: 6.11.1 +version: 6.12.0-wip description: >- Automatically generate code for converting to and from JSON by annotating Dart classes. diff --git a/json_serializable/test/config_test.dart b/json_serializable/test/config_test.dart index b29ffac1..0bc55b42 100644 --- a/json_serializable/test/config_test.dart +++ b/json_serializable/test/config_test.dart @@ -36,6 +36,11 @@ void main() { expect(builder, isNotNull); }); + test('triggers config is ignored', () async { + // This key is used by build_runner so it's meaningful for any builder. + jsonSerializable(const BuilderOptions({'run_only_if_triggered': true})); + }); + test('valid, non-default config', () { expect( generatorConfigNonDefaultJson.keys, From 29a672817d46ce9b93d40c2b7253ed76ce7013b0 Mon Sep 17 00:00:00 2001 From: David Morgan Date: Thu, 23 Oct 2025 10:02:02 +0200 Subject: [PATCH 2/2] Address review comments. --- json_serializable/test/config_test.dart | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/json_serializable/test/config_test.dart b/json_serializable/test/config_test.dart index 0bc55b42..64b42e41 100644 --- a/json_serializable/test/config_test.dart +++ b/json_serializable/test/config_test.dart @@ -37,7 +37,8 @@ void main() { }); test('triggers config is ignored', () async { - // This key is used by build_runner so it's meaningful for any builder. + // This key is used by build_runner so it's meaningful for any builder, + // check it doesn't cause json_serializable's config validation to throw. jsonSerializable(const BuilderOptions({'run_only_if_triggered': true})); });