Skip to content

Commit

Permalink
Wrapping / Unwrapping Value well-known type in request/response seria…
Browse files Browse the repository at this point in the history
…lizers (#951)

Values of type google.protobuf.Value where not correctly wrapped/unwrapped when
directly passed to a service (as a request or response).
  • Loading branch information
moufmouf authored Oct 14, 2023
1 parent 3137b46 commit d30ffa2
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
15 changes: 15 additions & 0 deletions integration/grpc-js/grpc-js-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,21 @@ describe("grpc-js-test", () => {
expect(response).toStrictEqual(timestamp);
});

client.value({ foo: "bar" }, (error: ServiceError | null, response: any) => {
expect(error).toBeNull();
expect(response).toEqual({ foo: "bar" });
});

client.value(12, (error: ServiceError | null, response: any) => {
expect(error).toBeNull();
expect(response).toEqual(12);
});

client.struct({ foo: "bar" }, (error: ServiceError | null, response: any) => {
expect(error).toBeNull();
expect(response).toEqual({ foo: "bar" });
});

const serverStreamingCall = client.serverStreaming({ timestamp });
serverStreamingCall.on("data", (response) => {
expect(response.timestamp).toEqual(timestamp);
Expand Down
8 changes: 4 additions & 4 deletions integration/grpc-js/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,10 @@ export const TestService = {
path: "/simple.Test/Value",
requestStream: false,
responseStream: false,
requestSerialize: (value: any | undefined) => Buffer.from(Value.encode(value).finish()),
requestDeserialize: (value: Buffer) => Value.decode(value),
responseSerialize: (value: any | undefined) => Buffer.from(Value.encode(value).finish()),
responseDeserialize: (value: Buffer) => Value.decode(value),
requestSerialize: (value: any | undefined) => Buffer.from(Value.encode(Value.wrap(value)).finish()),
requestDeserialize: (value: Buffer) => Value.unwrap(Value.decode(value)),
responseSerialize: (value: any | undefined) => Buffer.from(Value.encode(Value.wrap(value)).finish()),
responseDeserialize: (value: Buffer) => Value.unwrap(Value.decode(value)),
},
listValue: {
path: "/simple.Test/ListValue",
Expand Down
4 changes: 2 additions & 2 deletions src/encode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function generateEncoder(ctx: Context, typeName: string): Code {
return code`${TimestampValue}.encode(${value}).finish()`;
}

if (name == "Struct") {
if (name == "Struct" || name == "Value") {
const StructType = impProto(ctx.options, "google/protobuf/struct", name);
return code`${StructType}.encode(${StructType}.wrap(value)).finish()`;
}
Expand Down Expand Up @@ -75,7 +75,7 @@ export function generateDecoder(ctx: Context, typeName: string): Code {
return decoder;
}

if (name == "Struct" || name == "ListValue") {
if (name == "Struct" || name == "ListValue" || name == "Value") {
TypeValue = impProto(ctx.options, "google/protobuf/struct", name);
return code`${TypeValue}.unwrap(${TypeValue}.decode(value))`;
}
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,7 @@ export function wrapperTypeName(typeName: string): string | undefined {
case ".google.protobuf.ListValue":
case ".google.protobuf.Timestamp":
case ".google.protobuf.Struct":
case ".google.protobuf.Value":
return typeName.split(".")[3];
default:
return undefined;
Expand Down

0 comments on commit d30ffa2

Please sign in to comment.