From 489a3b343155ab8d74c1b3ce98e1a977380be56e Mon Sep 17 00:00:00 2001 From: Alexander Khristyukhin Date: Wed, 4 Oct 2023 11:44:29 +0300 Subject: [PATCH] fix: toJSON methods don't respect useDate=false (#935) --- integration/global-this/global-this.bin | Bin 1554 -> 791 bytes .../grpc-js-use-date-false.ts | 8 +------- integration/use-date-false/metadata.ts | 8 +------- integration/use-date-false/use-date-test.ts | 15 ++++++++++++--- protoc.Dockerfile | 5 ++--- src/main.ts | 2 +- 6 files changed, 17 insertions(+), 21 deletions(-) diff --git a/integration/global-this/global-this.bin b/integration/global-this/global-this.bin index 9e29a07b92b5f7336896f491aa68e378f134aac7..4d78dcf07df6fda44a414b084f50a66bbbee936a 100644 GIT binary patch delta 16 XcmbQlGo5XM7%Q`Yums~q=|4;WAshqn delta 23 fcmbQvHi>6~7%Pi_gaq?O=|4 { 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); + }); }); diff --git a/protoc.Dockerfile b/protoc.Dockerfile index 08812dd47..286b16f24 100644 --- a/protoc.Dockerfile +++ b/protoc.Dockerfile @@ -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 diff --git a/src/main.ts b/src/main.ts index 9033b2806..f4e19183b 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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];