From bc9493b1cdb30364d27b6102e38cc49a8a890365 Mon Sep 17 00:00:00 2001 From: Weidong Xu Date: Wed, 27 Mar 2024 08:02:18 +0800 Subject: [PATCH] tsp, correct way to write encode on scalar (#2643) --- .../cadl/optional/OptionalAsyncClient.java | 4 + .../com/cadl/optional/OptionalClient.java | 4 + .../implementation/OptionalOpsImpl.java | 8 ++ .../models/AllPropertiesOptional.java | 47 +++++++++++ .../com/cadl/optional/models/Optional.java | 77 ++++++++++++++++++- typespec-tests/tsp/optional.tsp | 5 ++ 6 files changed, 142 insertions(+), 3 deletions(-) diff --git a/typespec-tests/src/main/java/com/cadl/optional/OptionalAsyncClient.java b/typespec-tests/src/main/java/com/cadl/optional/OptionalAsyncClient.java index 37f0e6aa90..6bea22325f 100644 --- a/typespec-tests/src/main/java/com/cadl/optional/OptionalAsyncClient.java +++ b/typespec-tests/src/main/java/com/cadl/optional/OptionalAsyncClient.java @@ -82,6 +82,8 @@ public final class OptionalAsyncClient { * bytesDict (Optional): { * String: byte[] (Required) * } + * epochDateTimeRequiredNullable: Long (Required) + * epochDateTimeNullable: Long (Optional) * } * } *

Response Body Schema

@@ -108,6 +110,8 @@ public final class OptionalAsyncClient { * bytesDict (Optional): { * String: byte[] (Required) * } + * epochDateTimeRequiredNullable: Long (Optional) + * epochDateTimeNullable: Long (Optional) * immutable (Optional): { * stringReadWriteRequired: String (Required) * stringReadOnlyOptional: String (Optional) diff --git a/typespec-tests/src/main/java/com/cadl/optional/OptionalClient.java b/typespec-tests/src/main/java/com/cadl/optional/OptionalClient.java index a95dda084b..fdc12ae969 100644 --- a/typespec-tests/src/main/java/com/cadl/optional/OptionalClient.java +++ b/typespec-tests/src/main/java/com/cadl/optional/OptionalClient.java @@ -80,6 +80,8 @@ public final class OptionalClient { * bytesDict (Optional): { * String: byte[] (Required) * } + * epochDateTimeRequiredNullable: Long (Required) + * epochDateTimeNullable: Long (Optional) * } * } *

Response Body Schema

@@ -106,6 +108,8 @@ public final class OptionalClient { * bytesDict (Optional): { * String: byte[] (Required) * } + * epochDateTimeRequiredNullable: Long (Optional) + * epochDateTimeNullable: Long (Optional) * immutable (Optional): { * stringReadWriteRequired: String (Required) * stringReadOnlyOptional: String (Optional) diff --git a/typespec-tests/src/main/java/com/cadl/optional/implementation/OptionalOpsImpl.java b/typespec-tests/src/main/java/com/cadl/optional/implementation/OptionalOpsImpl.java index 3deaa3ecc3..3b1ba5d2d1 100644 --- a/typespec-tests/src/main/java/com/cadl/optional/implementation/OptionalOpsImpl.java +++ b/typespec-tests/src/main/java/com/cadl/optional/implementation/OptionalOpsImpl.java @@ -129,6 +129,8 @@ Response putSync(@HostParam("endpoint") String endpoint, * bytesDict (Optional): { * String: byte[] (Required) * } + * epochDateTimeRequiredNullable: Long (Required) + * epochDateTimeNullable: Long (Optional) * } * } *

Response Body Schema

@@ -155,6 +157,8 @@ Response putSync(@HostParam("endpoint") String endpoint, * bytesDict (Optional): { * String: byte[] (Required) * } + * epochDateTimeRequiredNullable: Long (Optional) + * epochDateTimeNullable: Long (Optional) * immutable (Optional): { * stringReadWriteRequired: String (Required) * stringReadOnlyOptional: String (Optional) @@ -232,6 +236,8 @@ public Mono> putWithResponseAsync(String requestHeaderRequi * bytesDict (Optional): { * String: byte[] (Required) * } + * epochDateTimeRequiredNullable: Long (Required) + * epochDateTimeNullable: Long (Optional) * } * } *

Response Body Schema

@@ -258,6 +264,8 @@ public Mono> putWithResponseAsync(String requestHeaderRequi * bytesDict (Optional): { * String: byte[] (Required) * } + * epochDateTimeRequiredNullable: Long (Optional) + * epochDateTimeNullable: Long (Optional) * immutable (Optional): { * stringReadWriteRequired: String (Required) * stringReadOnlyOptional: String (Optional) diff --git a/typespec-tests/src/main/java/com/cadl/optional/models/AllPropertiesOptional.java b/typespec-tests/src/main/java/com/cadl/optional/models/AllPropertiesOptional.java index c7ce427ff1..70aeeced17 100644 --- a/typespec-tests/src/main/java/com/cadl/optional/models/AllPropertiesOptional.java +++ b/typespec-tests/src/main/java/com/cadl/optional/models/AllPropertiesOptional.java @@ -13,7 +13,9 @@ import com.azure.json.JsonWriter; import java.io.IOException; import java.time.Duration; +import java.time.Instant; import java.time.OffsetDateTime; +import java.time.ZoneOffset; import java.time.format.DateTimeFormatter; import java.util.List; import java.util.Map; @@ -125,6 +127,18 @@ public final class AllPropertiesOptional implements JsonSerializable bytesDict; + /* + * The epochDateTimeRequiredNullable property. + */ + @Generated + private Long epochDateTimeRequiredNullable; + + /* + * The epochDateTimeNullable property. + */ + @Generated + private Long epochDateTimeNullable; + /* * The immutable property. */ @@ -308,6 +322,32 @@ public Map getBytesDict() { return this.bytesDict; } + /** + * Get the epochDateTimeRequiredNullable property: The epochDateTimeRequiredNullable property. + * + * @return the epochDateTimeRequiredNullable value. + */ + @Generated + public OffsetDateTime getEpochDateTimeRequiredNullable() { + if (this.epochDateTimeRequiredNullable == null) { + return null; + } + return OffsetDateTime.ofInstant(Instant.ofEpochSecond(this.epochDateTimeRequiredNullable), ZoneOffset.UTC); + } + + /** + * Get the epochDateTimeNullable property: The epochDateTimeNullable property. + * + * @return the epochDateTimeNullable value. + */ + @Generated + public OffsetDateTime getEpochDateTimeNullable() { + if (this.epochDateTimeNullable == null) { + return null; + } + return OffsetDateTime.ofInstant(Instant.ofEpochSecond(this.epochDateTimeNullable), ZoneOffset.UTC); + } + /** * Get the immutable property: The immutable property. * @@ -343,6 +383,8 @@ public JsonWriter toJson(JsonWriter jsonWriter) throws IOException { this.dateTime == null ? null : DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(this.dateTime)); jsonWriter.writeArrayField("stringList", this.stringList, (writer, element) -> writer.writeString(element)); jsonWriter.writeMapField("bytesDict", this.bytesDict, (writer, element) -> writer.writeBinary(element)); + jsonWriter.writeNumberField("epochDateTimeRequiredNullable", this.epochDateTimeRequiredNullable); + jsonWriter.writeNumberField("epochDateTimeNullable", this.epochDateTimeNullable); jsonWriter.writeJsonField("immutable", this.immutable); return jsonWriter.writeEndObject(); } @@ -401,6 +443,11 @@ public static AllPropertiesOptional fromJson(JsonReader jsonReader) throws IOExc } else if ("bytesDict".equals(fieldName)) { Map bytesDict = reader.readMap(reader1 -> reader1.getBinary()); deserializedAllPropertiesOptional.bytesDict = bytesDict; + } else if ("epochDateTimeRequiredNullable".equals(fieldName)) { + deserializedAllPropertiesOptional.epochDateTimeRequiredNullable + = reader.getNullable(JsonReader::getLong); + } else if ("epochDateTimeNullable".equals(fieldName)) { + deserializedAllPropertiesOptional.epochDateTimeNullable = reader.getNullable(JsonReader::getLong); } else if ("immutable".equals(fieldName)) { deserializedAllPropertiesOptional.immutable = ImmutableModel.fromJson(reader); } else { diff --git a/typespec-tests/src/main/java/com/cadl/optional/models/Optional.java b/typespec-tests/src/main/java/com/cadl/optional/models/Optional.java index 5ea016e9cd..32f9e38e6d 100644 --- a/typespec-tests/src/main/java/com/cadl/optional/models/Optional.java +++ b/typespec-tests/src/main/java/com/cadl/optional/models/Optional.java @@ -13,7 +13,9 @@ import com.azure.json.JsonWriter; import java.io.IOException; import java.time.Duration; +import java.time.Instant; import java.time.OffsetDateTime; +import java.time.ZoneOffset; import java.time.format.DateTimeFormatter; import java.util.List; import java.util.Map; @@ -125,6 +127,18 @@ public final class Optional implements JsonSerializable { @Generated private Map bytesDict; + /* + * The epochDateTimeRequiredNullable property. + */ + @Generated + private final Long epochDateTimeRequiredNullable; + + /* + * The epochDateTimeNullable property. + */ + @Generated + private Long epochDateTimeNullable; + /** * Creates an instance of Optional class. * @@ -132,14 +146,16 @@ public final class Optional implements JsonSerializable { * @param booleanRequiredNullable the booleanRequiredNullable value to set. * @param stringRequired the stringRequired value to set. * @param stringRequiredNullable the stringRequiredNullable value to set. + * @param epochDateTimeRequiredNullable the epochDateTimeRequiredNullable value to set. */ @Generated public Optional(boolean booleanRequired, Boolean booleanRequiredNullable, String stringRequired, - String stringRequiredNullable) { + String stringRequiredNullable, OffsetDateTime epochDateTimeRequiredNullable) { this.booleanRequired = booleanRequired; this.booleanRequiredNullable = booleanRequiredNullable; this.stringRequired = stringRequired; this.stringRequiredNullable = stringRequiredNullable; + this.epochDateTimeRequiredNullable = epochDateTimeRequiredNullable.toEpochSecond(); } /** @@ -468,6 +484,48 @@ public Optional setBytesDict(Map bytesDict) { return this; } + /** + * Get the epochDateTimeRequiredNullable property: The epochDateTimeRequiredNullable property. + * + * @return the epochDateTimeRequiredNullable value. + */ + @Generated + public OffsetDateTime getEpochDateTimeRequiredNullable() { + if (this.epochDateTimeRequiredNullable == null) { + return null; + } + return OffsetDateTime.ofInstant(Instant.ofEpochSecond(this.epochDateTimeRequiredNullable), ZoneOffset.UTC); + } + + /** + * Get the epochDateTimeNullable property: The epochDateTimeNullable property. + * + * @return the epochDateTimeNullable value. + */ + @Generated + public OffsetDateTime getEpochDateTimeNullable() { + if (this.epochDateTimeNullable == null) { + return null; + } + return OffsetDateTime.ofInstant(Instant.ofEpochSecond(this.epochDateTimeNullable), ZoneOffset.UTC); + } + + /** + * Set the epochDateTimeNullable property: The epochDateTimeNullable property. + * + * @param epochDateTimeNullable the epochDateTimeNullable value to set. + * @return the Optional object itself. + */ + @Generated + public Optional setEpochDateTimeNullable(OffsetDateTime epochDateTimeNullable) { + if (epochDateTimeNullable == null) { + this.epochDateTimeNullable = null; + } else { + this.epochDateTimeNullable = epochDateTimeNullable.toEpochSecond(); + } + return this; + } + /** * {@inheritDoc} */ @@ -479,6 +537,7 @@ public JsonWriter toJson(JsonWriter jsonWriter) throws IOException { jsonWriter.writeBooleanField("booleanRequiredNullable", this.booleanRequiredNullable); jsonWriter.writeStringField("stringRequired", this.stringRequired); jsonWriter.writeStringField("stringRequiredNullable", this.stringRequiredNullable); + jsonWriter.writeNumberField("epochDateTimeRequiredNullable", this.epochDateTimeRequiredNullable); jsonWriter.writeBooleanField("boolean", this.booleanProperty); jsonWriter.writeBooleanField("booleanNullable", this.booleanNullable); jsonWriter.writeStringField("string", this.string); @@ -493,6 +552,7 @@ public JsonWriter toJson(JsonWriter jsonWriter) throws IOException { this.dateTime == null ? null : DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(this.dateTime)); jsonWriter.writeArrayField("stringList", this.stringList, (writer, element) -> writer.writeString(element)); jsonWriter.writeMapField("bytesDict", this.bytesDict, (writer, element) -> writer.writeBinary(element)); + jsonWriter.writeNumberField("epochDateTimeNullable", this.epochDateTimeNullable); return jsonWriter.writeEndObject(); } @@ -511,6 +571,7 @@ public static Optional fromJson(JsonReader jsonReader) throws IOException { Boolean booleanRequiredNullable = null; String stringRequired = null; String stringRequiredNullable = null; + OffsetDateTime epochDateTimeRequiredNullable = null; Boolean booleanProperty = null; Boolean booleanNullable = null; String string = null; @@ -524,6 +585,7 @@ public static Optional fromJson(JsonReader jsonReader) throws IOException { OffsetDateTime dateTime = null; List stringList = null; Map bytesDict = null; + Long epochDateTimeNullable = null; while (reader.nextToken() != JsonToken.END_OBJECT) { String fieldName = reader.getFieldName(); reader.nextToken(); @@ -536,6 +598,12 @@ public static Optional fromJson(JsonReader jsonReader) throws IOException { stringRequired = reader.getString(); } else if ("stringRequiredNullable".equals(fieldName)) { stringRequiredNullable = reader.getString(); + } else if ("epochDateTimeRequiredNullable".equals(fieldName)) { + Long epochDateTimeRequiredNullableHolder = reader.getNullable(JsonReader::getLong); + if (epochDateTimeRequiredNullableHolder != null) { + epochDateTimeRequiredNullable = OffsetDateTime + .ofInstant(Instant.ofEpochSecond(epochDateTimeRequiredNullableHolder), ZoneOffset.UTC); + } } else if ("boolean".equals(fieldName)) { booleanProperty = reader.getNullable(JsonReader::getBoolean); } else if ("booleanNullable".equals(fieldName)) { @@ -562,12 +630,14 @@ public static Optional fromJson(JsonReader jsonReader) throws IOException { stringList = reader.readArray(reader1 -> reader1.getString()); } else if ("bytesDict".equals(fieldName)) { bytesDict = reader.readMap(reader1 -> reader1.getBinary()); + } else if ("epochDateTimeNullable".equals(fieldName)) { + epochDateTimeNullable = reader.getNullable(JsonReader::getLong); } else { reader.skipChildren(); } } - Optional deserializedOptional - = new Optional(booleanRequired, booleanRequiredNullable, stringRequired, stringRequiredNullable); + Optional deserializedOptional = new Optional(booleanRequired, booleanRequiredNullable, stringRequired, + stringRequiredNullable, epochDateTimeRequiredNullable); deserializedOptional.booleanProperty = booleanProperty; deserializedOptional.booleanNullable = booleanNullable; deserializedOptional.string = string; @@ -581,6 +651,7 @@ public static Optional fromJson(JsonReader jsonReader) throws IOException { deserializedOptional.dateTime = dateTime; deserializedOptional.stringList = stringList; deserializedOptional.bytesDict = bytesDict; + deserializedOptional.epochDateTimeNullable = epochDateTimeNullable; return deserializedOptional; }); diff --git a/typespec-tests/tsp/optional.tsp b/typespec-tests/tsp/optional.tsp index 83cedbaf68..43cb8555d4 100644 --- a/typespec-tests/tsp/optional.tsp +++ b/typespec-tests/tsp/optional.tsp @@ -7,6 +7,9 @@ using TypeSpec.Http; }) namespace Cadl.Optional; +@encode(DateTimeKnownEncoding.unixTimestamp, int64) +scalar epochDateTime extends utcDateTime; + model Optional { boolean?: boolean = true; booleanNullable?: boolean | null; @@ -25,6 +28,8 @@ model Optional { dateTime?: utcDateTime; stringList?: string[]; bytesDict?: Record; + epochDateTimeRequiredNullable: epochDateTime | null; + epochDateTimeNullable?: epochDateTime; } model Immutable {