Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error using the generated class: Expecting a fromJson constructor with exactly one positional parameter #802

Open
bsr203 opened this issue Feb 7, 2023 · 2 comments

Comments

@bsr203
Copy link

bsr203 commented Feb 7, 2023

Hello,

I have issue using a generated class as the json code generator gives below error.

Expecting a `fromJson` constructor with exactly one positional parameter. The only extra parameters allowed are functions of the form `T Function(Object?) fromJsonT` where `T` is a type parameter of the target type.
package:proj/gen/v1/proj/api/core.pb.dart:179:21
    ╷
179 │   factory TimeRange.fromJson($core.String i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) => create()..mergeFromJson(i, r);
    │                     ^^^^^^^^
    ╵
[INFO] Running build completed, took 3.7s

Message:

message TimeRange {
  int32 lower = 1;
  int32 upper = 2;
  TimeUnit unit = 3;
}

enum TimeUnit {
  TIME_UNIT_UNSPECIFIED = 0;
  TIME_UNIT_DAY = 1;
  TIME_UNIT_YEAR = 2;
}

relevant part of the the generated code

TimeRange._() : super();
  ....
  factory TimeRange.fromBuffer($core.List<$core.int> i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) => create()..mergeFromBuffer(i, r);
  factory TimeRange.fromJson($core.String i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) => create()..mergeFromJson(i, r);
  @$core.Deprecated(

I am using the generated type like,

@freezed
class Entity with _$Entity {
  const factory Entity({
    ...
    required api_core.TimeRange dur,
  }) = _Entity;

and the json_serializable is throwing above error.

I use the latest protoc_plugin

Is it possible to provide a constructor with one positional parameter?

Please let me know for any details. Thanks.

@osa1
Copy link
Member

osa1 commented Jun 9, 2023

Are you adding @JsonSerializable() to a protoc_plugin generated class?

@bsr203
Copy link
Author

bsr203 commented Aug 18, 2023

Sorry for the late response. My class is generated with protoc plugin, so not adding@JsonSerializable() . Seems like there is no easy way to do it. see google/json_serializable.dart#1017. Please close it if it is not possible easily.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants