diff --git a/json_serializable/lib/src/decode_helper.dart b/json_serializable/lib/src/decode_helper.dart index 7b65ff73..c64c9d1a 100644 --- a/json_serializable/lib/src/decode_helper.dart +++ b/json_serializable/lib/src/decode_helper.dart @@ -308,7 +308,8 @@ mixin DecodeHelper implements HelperCore { /// /// To improve the error details, [unavailableReasons] is checked for the /// unavailable constructor parameter. If the value is not `null`, it is -/// included in the [UnsupportedError] message. +/// included in the [UnsupportedError] message; otherwise a default explanation +/// is provided. /// /// [writableFields] are also populated, but only if they have not already /// been defined by a constructor parameter with the same name. @@ -336,11 +337,10 @@ _ConstructorData _writeConstructorInvocation( 'Cannot populate the required constructor ' 'argument: ${arg.name}.'; - final additionalInfo = unavailableReasons[arg.name]; - - if (additionalInfo != null) { - msg = '$msg $additionalInfo'; - } + final additionalInfo = + unavailableReasons[arg.name] ?? + 'It does not correspond to any field or getter on the class.'; + msg = '$msg $additionalInfo'; throw InvalidGenerationSourceError(msg, element: ctor); } diff --git a/json_serializable/test/json_serializable_test.dart b/json_serializable/test/json_serializable_test.dart index 4c344a65..cca27c7d 100644 --- a/json_serializable/test/json_serializable_test.dart +++ b/json_serializable/test/json_serializable_test.dart @@ -157,6 +157,7 @@ const _expectedAnnotatedTests = { 'Reproduce869NullableGenericTypeWithDefault', 'SameCtorAndJsonKeyDefaultValue', 'SetSupport', + 'StandaloneCtorParamClass', 'SubclassedJsonKey', 'SubType', 'SubTypeWithAnnotatedFieldOverrideExtends', diff --git a/json_serializable/test/src/_json_serializable_test_input.dart b/json_serializable/test/src/_json_serializable_test_input.dart index b4c21d32..1c037624 100644 --- a/json_serializable/test/src/_json_serializable_test_input.dart +++ b/json_serializable/test/src/_json_serializable_test_input.dart @@ -326,6 +326,18 @@ class PrivateFieldCtorClass { PrivateFieldCtorClass(this._privateField); } +@ShouldThrow( + 'Cannot populate the required constructor argument: ' + 'standaloneParam. It does not correspond to any field or getter on the ' + 'class.', + element: 'new', +) +@JsonSerializable() +class StandaloneCtorParamClass { + // ignore: avoid_unused_constructor_parameters + StandaloneCtorParamClass({required int standaloneParam}); +} + @ShouldThrow( 'Error with `@JsonKey` on the `field` field. ' 'Cannot set both `disallowNullValue` and `includeIfNull` to `true`. '