Skip to content

Commit

Permalink
fix: toJSON methods don't respect useDate=false (#935) (#937)
Browse files Browse the repository at this point in the history
  • Loading branch information
roboslone committed Oct 4, 2023
1 parent 318e607 commit acdfb0a
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 21 deletions.
Binary file modified integration/global-this/global-this.bin
Binary file not shown.
8 changes: 1 addition & 7 deletions integration/grpc-js-use-date-false/grpc-js-use-date-false.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export const TimestampMessage = {
toJSON(message: TimestampMessage): unknown {
const obj: any = {};
if (message.timestamp !== undefined) {
obj.timestamp = fromTimestamp(message.timestamp).toISOString();
obj.timestamp = message.timestamp;
}
return obj;
},
Expand Down Expand Up @@ -156,12 +156,6 @@ function toTimestamp(date: Date): Timestamp {
return { seconds, nanos };
}

function fromTimestamp(t: Timestamp): Date {
let millis = (t.seconds || 0) * 1_000;
millis += (t.nanos || 0) / 1_000_000;
return new globalThis.Date(millis);
}

function fromJsonTimestamp(o: any): Timestamp {
if (o instanceof globalThis.Date) {
return toTimestamp(o);
Expand Down
8 changes: 1 addition & 7 deletions integration/use-date-false/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export const Metadata = {
toJSON(message: Metadata): unknown {
const obj: any = {};
if (message.lastEdited !== undefined) {
obj.lastEdited = fromTimestamp(message.lastEdited).toISOString();
obj.lastEdited = message.lastEdited;
}
return obj;
},
Expand Down Expand Up @@ -85,12 +85,6 @@ function toTimestamp(date: Date): Timestamp {
return { seconds, nanos };
}

function fromTimestamp(t: Timestamp): Date {
let millis = (t.seconds || 0) * 1_000;
millis += (t.nanos || 0) / 1_000_000;
return new globalThis.Date(millis);
}

function fromJsonTimestamp(o: any): Timestamp {
if (o instanceof globalThis.Date) {
return toTimestamp(o);
Expand Down
15 changes: 12 additions & 3 deletions integration/use-date-false/use-date-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,23 @@ describe("useDate=false", () => {
const json = Metadata.toJSON({ lastEdited: nov29 });
expect(json).toMatchInlineSnapshot(`
{
"lastEdited": "1973-11-29T21:33:09.234Z",
"lastEdited": {
"nanos": 234567890,
"seconds": 123456789,
},
}
`);
expect(Metadata.fromJSON(json).lastEdited).toMatchInlineSnapshot(`
{
"nanos": 234000000,
"seconds": 123456789.234,
"nanos": 234567890,
"seconds": 123456789,
}
`);
});

it("doesn't lose precision in encoding/decoding", () => {
const d = Metadata.fromJSON(Metadata.toJSON({ lastEdited: nov29 }));
expect(d.lastEdited?.seconds).toStrictEqual(nov29.seconds);
expect(d.lastEdited?.nanos).toStrictEqual(nov29.nanos);
});
});
5 changes: 2 additions & 3 deletions protoc.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# Docker image for protoc
FROM node:17-alpine3.14
FROM node:current-slim
ARG PROTOC_VERSION="3.19.1"
ARG ARCH="x86_64"

RUN apk add bash
RUN apk add gcompat
RUN apt-get update --yes && apt-get install --yes unzip
ADD "https://github.com/protocolbuffers/protobuf/releases/download/v$PROTOC_VERSION/protoc-$PROTOC_VERSION-linux-$ARCH.zip" protoc.zip
RUN mkdir /usr/local/lib/protoc && unzip protoc.zip -d /usr/local/lib/protoc && rm protoc.zip
RUN ln -s /usr/local/lib/protoc/bin/protoc /usr/local/bin/protoc
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1979,7 +1979,7 @@ function generateToJson(
} else if (isTimestamp(field) && options.useDate === DateOption.STRING) {
return code`${from}`;
} else if (isTimestamp(field) && options.useDate === DateOption.TIMESTAMP) {
return code`${utils.fromTimestamp}(${from}).toISOString()`;
return code`${from}`;
} else if (isMapType(ctx, messageDesc, field)) {
// For map types, drill-in and then admittedly re-hard-code our per-value-type logic
const valueType = (typeMap.get(field.typeName)![2] as DescriptorProto).field[1];
Expand Down

0 comments on commit acdfb0a

Please sign in to comment.