Skip to content

New warnings with json_serializable>=6.11.0 (conflicting parameters) #1289

@walsha2

Description

@walsha2

Describe the bug

This only started happening since json_serializable>=6.11.0. A whole host of warnings are now reported when running code generation. No other changes. Currently using freezed==3.2.0

Nothing breaks, the code still generates the same way, but there are now lots of warnings.

To Reproduce

Take the following:

@freezed
abstract class MyResponse with _$MyResponse {
  const MyResponse._();

  const factory MyResponse({
    @JsonKey(name: 'docs_url')
    @Default('http://localhost:8000/docs')
    String docsUrl,
  }) = _MyResponse;

  factory MyResponse.fromJson(Map<String, dynamic> json) =>
      _$MyResponseFromJson(json);
}

Then generate code:

dart run build_runner build --delete-conflicting-outputs

Will yield the following warning:

W Field `docsUrl` has conflicting `JsonKey.name` annotations: 
both constructor parameter and class field have this annotation. 
Using constructor parameter value.

Here is the generated code:

mixin _$MyResponse {

@JsonKey(name: 'docs_url') String get docsUrl;
/// Create a copy of MyResponse
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
@pragma('vm:prefer-inline')
$MyResponseCopyWith<MyResponse> get copyWith => _$MyResponseCopyWithImpl<MyResponse>(this as MyResponse, _$identity);

  /// Serializes this MyResponse to a JSON map.
  Map<String, dynamic> toJson();


@override
bool operator ==(Object other) {
  return identical(this, other) || (other.runtimeType == runtimeType&&other is MyResponse&&(identical(other.docsUrl, docsUrl) || other.docsUrl == docsUrl));
}

@JsonKey(includeFromJson: false, includeToJson: false)
@override
int get hashCode => Object.hash(runtimeType,docsUrl);

@override
String toString() {
  return 'MyResponse(docsUrl: $docsUrl)';
}
}

This is true for other keys as well, e.g. JsonKey.includeIfNull. The new version of json_serializable is now reporting this as a warning.

Expected behavior

No warnings to be emitted.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions