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

How to JSON Transcode Protos With Naming Equal to Generated Typescript Repeated Fields #1465

Closed
rauljordan opened this issue Jun 16, 2020 · 3 comments

Comments

@rauljordan
Copy link

📚 Documentation

Hi all, currently I have this protobuf:

message FooResponse {
  repeated int foo = 1;
}

Which gets compiled into the following typescript definition using proto-gen-ts

export class FooResponse extends jspb.Message {
  getFooList(): Array<int>;
  setFooList(value: Array<int>): void;
  clearFooList(): void;
  addFoo(value?: int, index?: number): APIKey;

  serializeBinary(): Uint8Array;
  toObject(includeInstance?: boolean): FooResponse.AsObject;
  static toObject(includeInstance: boolean, msg: FooResponse): FooResponse.AsObject;
  static serializeBinaryToWriter(message: FooResponse, writer: jspb.BinaryWriter): void;
  static deserializeBinary(bytes: Uint8Array): FooResponse;
  static deserializeBinaryFromReader(message: FooResponse, reader: jspb.BinaryReader): FooResponse;
}

export namespace FooResponse {
  export type AsObject = {
    fooList: Array<int>,
  }
}

I want to be able to use this type in my front-end, but the JSON data returned by my grpc-gateway looks like this:

{ foo: [1, 2, 3] }

Instead of the expected

{ fooList: [1, 2, 3] }

Any way around this? Would I need to implement a custom marshaler just to achieve this desired result?

@achew22
Copy link
Collaborator

achew22 commented Jun 16, 2020

It looks like you're trying to file a bug against the proto-gen-ts project. Unfortunately this is the grpc-gateway project. Closing this bug since it is unrelated to this project, but if I'm wrong please reopen and we can discuss the interaction of grpc-gateway.

Have a good one 🙂

@achew22 achew22 closed this as completed Jun 16, 2020
@rauljordan
Copy link
Author

Hey @achew22 just wondering, isn't this something the gateway can handle? i.e. the JSON transcoding of

message FooResponse {
  repeated int foo = 1;
}

Should have an option of matching the generated protobufs so that it looks like { fooList: [1, 2, 3] } instead of { foo: [1, 2, 3] }? What do you think?

@johanbrandhorst
Copy link
Collaborator

Hi @rauljordan, I think that option would not be something we want to support, since it's not using any of the official protobuf JSON names. I would sooner suggest you find some way to remove the List suffix in your JSON (and it looks like you've raised an issue).

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

3 participants