Skip to content

Commit acdfb0a

Browse files
authored
fix: toJSON methods don't respect useDate=false (#935) (#937)
1 parent 318e607 commit acdfb0a

File tree

6 files changed

+17
-21
lines changed

6 files changed

+17
-21
lines changed
-763 Bytes
Binary file not shown.

integration/grpc-js-use-date-false/grpc-js-use-date-false.ts

+1-7
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export const TimestampMessage = {
5959
toJSON(message: TimestampMessage): unknown {
6060
const obj: any = {};
6161
if (message.timestamp !== undefined) {
62-
obj.timestamp = fromTimestamp(message.timestamp).toISOString();
62+
obj.timestamp = message.timestamp;
6363
}
6464
return obj;
6565
},
@@ -156,12 +156,6 @@ function toTimestamp(date: Date): Timestamp {
156156
return { seconds, nanos };
157157
}
158158

159-
function fromTimestamp(t: Timestamp): Date {
160-
let millis = (t.seconds || 0) * 1_000;
161-
millis += (t.nanos || 0) / 1_000_000;
162-
return new globalThis.Date(millis);
163-
}
164-
165159
function fromJsonTimestamp(o: any): Timestamp {
166160
if (o instanceof globalThis.Date) {
167161
return toTimestamp(o);

integration/use-date-false/metadata.ts

+1-7
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export const Metadata = {
5050
toJSON(message: Metadata): unknown {
5151
const obj: any = {};
5252
if (message.lastEdited !== undefined) {
53-
obj.lastEdited = fromTimestamp(message.lastEdited).toISOString();
53+
obj.lastEdited = message.lastEdited;
5454
}
5555
return obj;
5656
},
@@ -85,12 +85,6 @@ function toTimestamp(date: Date): Timestamp {
8585
return { seconds, nanos };
8686
}
8787

88-
function fromTimestamp(t: Timestamp): Date {
89-
let millis = (t.seconds || 0) * 1_000;
90-
millis += (t.nanos || 0) / 1_000_000;
91-
return new globalThis.Date(millis);
92-
}
93-
9488
function fromJsonTimestamp(o: any): Timestamp {
9589
if (o instanceof globalThis.Date) {
9690
return toTimestamp(o);

integration/use-date-false/use-date-test.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,23 @@ describe("useDate=false", () => {
1919
const json = Metadata.toJSON({ lastEdited: nov29 });
2020
expect(json).toMatchInlineSnapshot(`
2121
{
22-
"lastEdited": "1973-11-29T21:33:09.234Z",
22+
"lastEdited": {
23+
"nanos": 234567890,
24+
"seconds": 123456789,
25+
},
2326
}
2427
`);
2528
expect(Metadata.fromJSON(json).lastEdited).toMatchInlineSnapshot(`
2629
{
27-
"nanos": 234000000,
28-
"seconds": 123456789.234,
30+
"nanos": 234567890,
31+
"seconds": 123456789,
2932
}
3033
`);
3134
});
35+
36+
it("doesn't lose precision in encoding/decoding", () => {
37+
const d = Metadata.fromJSON(Metadata.toJSON({ lastEdited: nov29 }));
38+
expect(d.lastEdited?.seconds).toStrictEqual(nov29.seconds);
39+
expect(d.lastEdited?.nanos).toStrictEqual(nov29.nanos);
40+
});
3241
});

protoc.Dockerfile

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
# Docker image for protoc
2-
FROM node:17-alpine3.14
2+
FROM node:current-slim
33
ARG PROTOC_VERSION="3.19.1"
44
ARG ARCH="x86_64"
55

6-
RUN apk add bash
7-
RUN apk add gcompat
6+
RUN apt-get update --yes && apt-get install --yes unzip
87
ADD "https://github.com/protocolbuffers/protobuf/releases/download/v$PROTOC_VERSION/protoc-$PROTOC_VERSION-linux-$ARCH.zip" protoc.zip
98
RUN mkdir /usr/local/lib/protoc && unzip protoc.zip -d /usr/local/lib/protoc && rm protoc.zip
109
RUN ln -s /usr/local/lib/protoc/bin/protoc /usr/local/bin/protoc

src/main.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1979,7 +1979,7 @@ function generateToJson(
19791979
} else if (isTimestamp(field) && options.useDate === DateOption.STRING) {
19801980
return code`${from}`;
19811981
} else if (isTimestamp(field) && options.useDate === DateOption.TIMESTAMP) {
1982-
return code`${utils.fromTimestamp}(${from}).toISOString()`;
1982+
return code`${from}`;
19831983
} else if (isMapType(ctx, messageDesc, field)) {
19841984
// For map types, drill-in and then admittedly re-hard-code our per-value-type logic
19851985
const valueType = (typeMap.get(field.typeName)![2] as DescriptorProto).field[1];

0 commit comments

Comments
 (0)