From d2d21f4993ace08e38ec669e4e343709b8ca9f6f Mon Sep 17 00:00:00 2001 From: ghostbuster91 Date: Fri, 30 Aug 2024 18:15:24 +0200 Subject: [PATCH 1/4] fix: Add missing nano part when encoding timestamp to document --- .../test/src/smithy4s/DocumentSpec.scala | 24 +++++++++++++++++++ .../DocumentEncoderSchemaVisitor.scala | 7 +++--- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/modules/bootstrapped/test/src/smithy4s/DocumentSpec.scala b/modules/bootstrapped/test/src/smithy4s/DocumentSpec.scala index b2d99be50..90f2c1d0c 100644 --- a/modules/bootstrapped/test/src/smithy4s/DocumentSpec.scala +++ b/modules/bootstrapped/test/src/smithy4s/DocumentSpec.scala @@ -581,6 +581,30 @@ class DocumentSpec() extends FunSuite { ) } + test("Document encoder - timestamp epoch seconds with nanos") { + val timestampWithNanos = Timestamp(1716459630L, 5000000) + val result = Document.Encoder + .withExplicitDefaultsEncoding(false) + .fromSchema(TimestampOperationInput.schema) + .encode( + TimestampOperationInput( + timestampWithNanos, + timestampWithNanos, + timestampWithNanos + ) + ) + expect.same( + Document.obj( + "httpDate" -> Document.fromString("Thu, 23 May 2024 10:20:30.005 GMT"), + "dateTime" -> Document.fromString("2024-05-23T10:20:30.005Z"), + "epochSeconds" -> Document.fromBigDecimal( + BigDecimal("1716459630.500000") + ) + ), + result + ) + } + test("Document decoder - timestamp defaults") { val doc = Document.obj() val result = Document.Decoder diff --git a/modules/core/src/smithy4s/internals/DocumentEncoderSchemaVisitor.scala b/modules/core/src/smithy4s/internals/DocumentEncoderSchemaVisitor.scala index 37204c562..d7fdd22fb 100644 --- a/modules/core/src/smithy4s/internals/DocumentEncoderSchemaVisitor.scala +++ b/modules/core/src/smithy4s/internals/DocumentEncoderSchemaVisitor.scala @@ -99,9 +99,10 @@ class DocumentEncoderSchemaVisitor( hints .get(TimestampFormat) .getOrElse(TimestampFormat.EPOCH_SECONDS) match { - case DATE_TIME => ts => DString(ts.format(DATE_TIME)) - case HTTP_DATE => ts => DString(ts.format(HTTP_DATE)) - case EPOCH_SECONDS => ts => DNumber(BigDecimal(ts.epochSecond)) + case DATE_TIME => ts => DString(ts.format(DATE_TIME)) + case HTTP_DATE => ts => DString(ts.format(HTTP_DATE)) + case EPOCH_SECONDS => + ts => DNumber(BigDecimal(s"${ts.epochSecond}.${ts.nano}")) } case PDocument => from(identity) case PFloat => from(float => DNumber(BigDecimal(float.toDouble))) From ad754e39b556f8676d0464b0fb229b0498ed4b91 Mon Sep 17 00:00:00 2001 From: ghostbuster91 Date: Mon, 2 Sep 2024 11:33:30 +0200 Subject: [PATCH 2/4] Rewrite ts to BigDecimal conversion --- .../smithy4s/internals/DocumentEncoderSchemaVisitor.scala | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/modules/core/src/smithy4s/internals/DocumentEncoderSchemaVisitor.scala b/modules/core/src/smithy4s/internals/DocumentEncoderSchemaVisitor.scala index d7fdd22fb..c6b555d02 100644 --- a/modules/core/src/smithy4s/internals/DocumentEncoderSchemaVisitor.scala +++ b/modules/core/src/smithy4s/internals/DocumentEncoderSchemaVisitor.scala @@ -102,7 +102,11 @@ class DocumentEncoderSchemaVisitor( case DATE_TIME => ts => DString(ts.format(DATE_TIME)) case HTTP_DATE => ts => DString(ts.format(HTTP_DATE)) case EPOCH_SECONDS => - ts => DNumber(BigDecimal(s"${ts.epochSecond}.${ts.nano}")) + ts => + val epochSecondsWithNanos = + BigDecimal(ts.epochSecond) + (BigDecimal(ts.nano) * BigDecimal(10) + .pow(-9)) + DNumber(epochSecondsWithNanos) } case PDocument => from(identity) case PFloat => from(float => DNumber(BigDecimal(float.toDouble))) From ff8870b82ac6b40a29aded29038c6003cf080292 Mon Sep 17 00:00:00 2001 From: ghostbuster91 Date: Mon, 2 Sep 2024 14:01:52 +0200 Subject: [PATCH 3/4] Fix test assertion --- modules/bootstrapped/test/src/smithy4s/DocumentSpec.scala | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/modules/bootstrapped/test/src/smithy4s/DocumentSpec.scala b/modules/bootstrapped/test/src/smithy4s/DocumentSpec.scala index 90f2c1d0c..04e835ed3 100644 --- a/modules/bootstrapped/test/src/smithy4s/DocumentSpec.scala +++ b/modules/bootstrapped/test/src/smithy4s/DocumentSpec.scala @@ -582,7 +582,8 @@ class DocumentSpec() extends FunSuite { } test("Document encoder - timestamp epoch seconds with nanos") { - val timestampWithNanos = Timestamp(1716459630L, 5000000) + val timestampWithNanos = + Timestamp(1716459630L, 500_000_000 /* half a second */ ) val result = Document.Encoder .withExplicitDefaultsEncoding(false) .fromSchema(TimestampOperationInput.schema) @@ -595,8 +596,8 @@ class DocumentSpec() extends FunSuite { ) expect.same( Document.obj( - "httpDate" -> Document.fromString("Thu, 23 May 2024 10:20:30.005 GMT"), - "dateTime" -> Document.fromString("2024-05-23T10:20:30.005Z"), + "httpDate" -> Document.fromString("Thu, 23 May 2024 10:20:30.500 GMT"), + "dateTime" -> Document.fromString("2024-05-23T10:20:30.500Z"), "epochSeconds" -> Document.fromBigDecimal( BigDecimal("1716459630.500000") ) From 139ffca05232540ed904e66e7b6e82cabc7ac9ec Mon Sep 17 00:00:00 2001 From: ghostbuster91 Date: Mon, 2 Sep 2024 14:08:53 +0200 Subject: [PATCH 4/4] Fix for scala 2.12 --- modules/bootstrapped/test/src/smithy4s/DocumentSpec.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/bootstrapped/test/src/smithy4s/DocumentSpec.scala b/modules/bootstrapped/test/src/smithy4s/DocumentSpec.scala index 04e835ed3..fa3f749ae 100644 --- a/modules/bootstrapped/test/src/smithy4s/DocumentSpec.scala +++ b/modules/bootstrapped/test/src/smithy4s/DocumentSpec.scala @@ -583,7 +583,7 @@ class DocumentSpec() extends FunSuite { test("Document encoder - timestamp epoch seconds with nanos") { val timestampWithNanos = - Timestamp(1716459630L, 500_000_000 /* half a second */ ) + Timestamp(1716459630L, 500 * 1000 * 1000 /* half a second */ ) val result = Document.Encoder .withExplicitDefaultsEncoding(false) .fromSchema(TimestampOperationInput.schema)