diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/examplemodel/LiteralNode.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/examplemodel/LiteralNode.java index ff195807f2e..68542070d62 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/examplemodel/LiteralNode.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/examplemodel/LiteralNode.java @@ -10,10 +10,16 @@ */ public class LiteralNode extends ExampleNode { + private final IType wireType; private String literalsValue; public LiteralNode(IType clientType, Object objectValue) { + this(clientType, clientType, objectValue); + } + + public LiteralNode(IType clientType, IType wireType, Object objectValue) { super(clientType, objectValue); + this.wireType = wireType; } public String getLiteralsValue() { @@ -24,4 +30,8 @@ public LiteralNode setLiteralsValue(String literalsValue) { this.literalsValue = literalsValue; return this; } + + public IType getWireType() { + return wireType; + } } diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/example/ModelExampleWriter.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/example/ModelExampleWriter.java index 894f120e671..0308eb463df 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/example/ModelExampleWriter.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/example/ModelExampleWriter.java @@ -120,8 +120,14 @@ private void addEqualsAssertion(String expected, String code, boolean booleanAss public void accept(ExampleNode node, String getterCode) { if (node instanceof LiteralNode) { node.getClientType().addImportsTo(imports, false); - - addEqualsAssertion(node.getClientType().defaultValueExpression(((LiteralNode) node).getLiteralsValue()), + IType wireType = ((LiteralNode) node).getWireType(); + + addEqualsAssertion( + wireType.convertToClientType(node.getObjectValue() == null + ? wireType.defaultValueExpression() + : wireType.defaultValueExpression( + // We are already using wireType, thus wire value should be used, instead of client value. + String.valueOf(node.getObjectValue()))), getterCode, node.getClientType().asNullable() == ClassType.BOOLEAN); } else if (node instanceof ObjectNode) { // additionalProperties diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/util/ModelExampleUtil.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/util/ModelExampleUtil.java index c3ef86ab5ba..f28ae9d85e1 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/util/ModelExampleUtil.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/util/ModelExampleUtil.java @@ -204,7 +204,7 @@ public static ExampleNode parseNode(IType type, IType wireType, Object objectVal */ private static ExampleNode defaultNode(IType clientType, IType wireType, Object exampleValue) { ExampleNode node; - LiteralNode literalNode = new LiteralNode(clientType, exampleValue); + LiteralNode literalNode = new LiteralNode(clientType, wireType, exampleValue); node = literalNode; if (exampleValue != null) { @@ -292,7 +292,8 @@ public static ExampleNode parseNodeFromParameter(ProxyMethodExample example, Met if (ClassType.CONTEXT.equals(methodParameter.getClientMethodParameter().getClientType())) { node = new LiteralNode(ClassType.CONTEXT, "").setLiteralsValue(""); } else { - node = new LiteralNode(methodParameter.getClientMethodParameter().getClientType(), null); + node = new LiteralNode(methodParameter.getClientMethodParameter().getClientType(), + methodParameter.getClientMethodParameter().getWireType(), null); } } else { node = parseNodeFromMethodParameter(methodParameter, exampleValue); diff --git a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/util/ModelTestCaseUtil.java b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/util/ModelTestCaseUtil.java index b0dc2bfdfd3..b37adeda6b0 100644 --- a/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/util/ModelTestCaseUtil.java +++ b/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/util/ModelTestCaseUtil.java @@ -109,7 +109,8 @@ public static Object jsonFromType(int depth, IType type) { } else if (type == ClassType.STRING) { return randomString(); } else if (type.asNullable() == ClassType.UNIX_TIME_LONG) { - return RANDOM.nextLong() & Long.MAX_VALUE; + // use nextInt to avoid exceeding unixTime limit + return RANDOM.nextInt() & Long.MAX_VALUE; } else if (type == ClassType.DATE_TIME) { return randomDateTime().toString(); } else if (type == ClassType.DATE_TIME_RFC_1123) { diff --git a/packages/http-client-java/generator/http-client-generator-mgmt/src/main/java/com/microsoft/typespec/http/client/generator/mgmt/mapper/ExampleParser.java b/packages/http-client-java/generator/http-client-generator-mgmt/src/main/java/com/microsoft/typespec/http/client/generator/mgmt/mapper/ExampleParser.java index 5ef974e3ddd..1e88b64d3d4 100644 --- a/packages/http-client-java/generator/http-client-generator-mgmt/src/main/java/com/microsoft/typespec/http/client/generator/mgmt/mapper/ExampleParser.java +++ b/packages/http-client-java/generator/http-client-generator-mgmt/src/main/java/com/microsoft/typespec/http/client/generator/mgmt/mapper/ExampleParser.java @@ -534,7 +534,7 @@ private static ExampleNode parseNodeFromModelProperty(ProxyMethodExample example ProxyMethodExample.ParameterValue parameterValue = ModelExampleUtil.findParameter(example, serializedName); ExampleNode node; if (parameterValue == null) { - node = new LiteralNode(modelProperty.getClientType(), null); + node = new LiteralNode(modelProperty.getClientType(), modelProperty.getWireType(), null); } else { List jsonPropertyNames = modelProperty.getSerializedNames(); @@ -544,7 +544,7 @@ private static ExampleNode parseNodeFromModelProperty(ProxyMethodExample example node = ModelExampleUtil.parseNode(modelProperty.getClientType(), modelProperty.getWireType(), childObjectValue); } else { - node = new LiteralNode(modelProperty.getClientType(), null); + node = new LiteralNode(modelProperty.getClientType(), modelProperty.getWireType(), null); } } return node; diff --git a/packages/http-client-java/generator/http-client-generator-test/.gitignore b/packages/http-client-java/generator/http-client-generator-test/.gitignore new file mode 100644 index 00000000000..c726c45c8bd --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-test/.gitignore @@ -0,0 +1,2 @@ +# ignore ARM generated tests with random mock values +src/test/java/tsptest/**/generated diff --git a/packages/http-client-java/generator/http-client-generator-test/Generate.ps1 b/packages/http-client-java/generator/http-client-generator-test/Generate.ps1 index d344a462f98..4b810f82693 100644 --- a/packages/http-client-java/generator/http-client-generator-test/Generate.ps1 +++ b/packages/http-client-java/generator/http-client-generator-test/Generate.ps1 @@ -94,8 +94,6 @@ $generateScript = { $tspOptions += " --option ""@typespec/http-client-java.float32-as-double=false""" $tspOptions += " --option ""@typespec/http-client-java.uuid-as-string=false""" } elseif ($tspFile -match "tsp[\\/]arm-stream-style-serialization.tsp") { - # for mgmt, do not generate tests due to random mock values - $tspOptions += " --option ""@typespec/http-client-java.generate-tests=false""" # test service-name $tspOptions += " --option ""@typespec/http-client-java.service-name=Arm Resource Provider""" # test property-include-always diff --git a/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/armstreamstyleserialization/models/Builtin.java b/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/armstreamstyleserialization/models/Builtin.java new file mode 100644 index 00000000000..6ed4912ec3f --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/armstreamstyleserialization/models/Builtin.java @@ -0,0 +1,441 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +// Code generated by Microsoft (R) TypeSpec Code Generator. + +package tsptest.armstreamstyleserialization.models; + +import com.azure.core.annotation.Immutable; +import com.azure.core.util.CoreUtils; +import com.azure.core.util.logging.ClientLogger; +import com.azure.json.JsonReader; +import com.azure.json.JsonSerializable; +import com.azure.json.JsonToken; +import com.azure.json.JsonWriter; +import java.io.IOException; +import java.math.BigDecimal; +import java.time.Duration; +import java.time.LocalDate; +import java.time.OffsetDateTime; +import java.time.format.DateTimeFormatter; +import java.util.List; +import java.util.Map; +import java.util.Objects; + +/** + * The Builtin model. + */ +@Immutable +public final class Builtin implements JsonSerializable { + /* + * The boolean property. + */ + private boolean booleanProperty; + + /* + * The string property. + */ + private String string; + + /* + * The bytes property. + */ + private byte[] bytes; + + /* + * The int property. + */ + private int intProperty; + + /* + * The safeint property. + */ + private long safeint; + + /* + * The decimal property. + */ + private BigDecimal decimal; + + /* + * The long property. + */ + private long longProperty; + + /* + * The float property. + */ + private double floatProperty; + + /* + * The double property. + */ + private double doubleProperty; + + /* + * The duration property. + */ + private Duration duration; + + /* + * The date property. + */ + private LocalDate date; + + /* + * The dateTime property. + */ + private OffsetDateTime dateTime; + + /* + * The stringList property. + */ + private List stringList; + + /* + * The bytesDict property. + */ + private Map bytesDict; + + /* + * The url property. + */ + private String url; + + /* + * The nullableFloatDict property. + */ + private Map nullableFloatDict; + + /* + * The encoded property. + */ + private Encoded encoded; + + /* + * The uuid property. + */ + private String uuid; + + /** + * Creates an instance of Builtin class. + */ + private Builtin() { + } + + /** + * Get the booleanProperty property: The boolean property. + * + * @return the booleanProperty value. + */ + public boolean booleanProperty() { + return this.booleanProperty; + } + + /** + * Get the string property: The string property. + * + * @return the string value. + */ + public String string() { + return this.string; + } + + /** + * Get the bytes property: The bytes property. + * + * @return the bytes value. + */ + public byte[] bytes() { + return CoreUtils.clone(this.bytes); + } + + /** + * Get the intProperty property: The int property. + * + * @return the intProperty value. + */ + public int intProperty() { + return this.intProperty; + } + + /** + * Get the safeint property: The safeint property. + * + * @return the safeint value. + */ + public long safeint() { + return this.safeint; + } + + /** + * Get the decimal property: The decimal property. + * + * @return the decimal value. + */ + public BigDecimal decimal() { + return this.decimal; + } + + /** + * Get the longProperty property: The long property. + * + * @return the longProperty value. + */ + public long longProperty() { + return this.longProperty; + } + + /** + * Get the floatProperty property: The float property. + * + * @return the floatProperty value. + */ + public double floatProperty() { + return this.floatProperty; + } + + /** + * Get the doubleProperty property: The double property. + * + * @return the doubleProperty value. + */ + public double doubleProperty() { + return this.doubleProperty; + } + + /** + * Get the duration property: The duration property. + * + * @return the duration value. + */ + public Duration duration() { + return this.duration; + } + + /** + * Get the date property: The date property. + * + * @return the date value. + */ + public LocalDate date() { + return this.date; + } + + /** + * Get the dateTime property: The dateTime property. + * + * @return the dateTime value. + */ + public OffsetDateTime dateTime() { + return this.dateTime; + } + + /** + * Get the stringList property: The stringList property. + * + * @return the stringList value. + */ + public List stringList() { + return this.stringList; + } + + /** + * Get the bytesDict property: The bytesDict property. + * + * @return the bytesDict value. + */ + public Map bytesDict() { + return this.bytesDict; + } + + /** + * Get the url property: The url property. + * + * @return the url value. + */ + public String url() { + return this.url; + } + + /** + * Get the nullableFloatDict property: The nullableFloatDict property. + * + * @return the nullableFloatDict value. + */ + public Map nullableFloatDict() { + return this.nullableFloatDict; + } + + /** + * Get the encoded property: The encoded property. + * + * @return the encoded value. + */ + public Encoded encoded() { + return this.encoded; + } + + /** + * Get the uuid property: The uuid property. + * + * @return the uuid value. + */ + public String uuid() { + return this.uuid; + } + + /** + * Validates the instance. + * + * @throws IllegalArgumentException thrown if the instance is not valid. + */ + public void validate() { + if (string() == null) { + throw LOGGER.atError() + .log(new IllegalArgumentException("Missing required property string in model Builtin")); + } + if (bytes() == null) { + throw LOGGER.atError() + .log(new IllegalArgumentException("Missing required property bytes in model Builtin")); + } + if (decimal() == null) { + throw LOGGER.atError() + .log(new IllegalArgumentException("Missing required property decimal in model Builtin")); + } + if (duration() == null) { + throw LOGGER.atError() + .log(new IllegalArgumentException("Missing required property duration in model Builtin")); + } + if (date() == null) { + throw LOGGER.atError().log(new IllegalArgumentException("Missing required property date in model Builtin")); + } + if (dateTime() == null) { + throw LOGGER.atError() + .log(new IllegalArgumentException("Missing required property dateTime in model Builtin")); + } + if (stringList() == null) { + throw LOGGER.atError() + .log(new IllegalArgumentException("Missing required property stringList in model Builtin")); + } + if (bytesDict() == null) { + throw LOGGER.atError() + .log(new IllegalArgumentException("Missing required property bytesDict in model Builtin")); + } + if (url() == null) { + throw LOGGER.atError().log(new IllegalArgumentException("Missing required property url in model Builtin")); + } + if (nullableFloatDict() == null) { + throw LOGGER.atError() + .log(new IllegalArgumentException("Missing required property nullableFloatDict in model Builtin")); + } + if (encoded() == null) { + throw LOGGER.atError() + .log(new IllegalArgumentException("Missing required property encoded in model Builtin")); + } else { + encoded().validate(); + } + if (uuid() == null) { + throw LOGGER.atError().log(new IllegalArgumentException("Missing required property uuid in model Builtin")); + } + } + + private static final ClientLogger LOGGER = new ClientLogger(Builtin.class); + + /** + * {@inheritDoc} + */ + @Override + public JsonWriter toJson(JsonWriter jsonWriter) throws IOException { + jsonWriter.writeStartObject(); + jsonWriter.writeBooleanField("boolean", this.booleanProperty); + jsonWriter.writeStringField("string", this.string); + jsonWriter.writeBinaryField("bytes", this.bytes); + jsonWriter.writeIntField("int", this.intProperty); + jsonWriter.writeLongField("safeint", this.safeint); + jsonWriter.writeNumberField("decimal", this.decimal); + jsonWriter.writeLongField("long", this.longProperty); + jsonWriter.writeDoubleField("float", this.floatProperty); + jsonWriter.writeDoubleField("double", this.doubleProperty); + jsonWriter.writeStringField("duration", CoreUtils.durationToStringWithDays(this.duration)); + jsonWriter.writeStringField("date", Objects.toString(this.date, null)); + jsonWriter.writeStringField("dateTime", + 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.writeStringField("url", this.url); + jsonWriter.writeMapField("nullableFloatDict", this.nullableFloatDict, + (writer, element) -> writer.writeNumber(element)); + jsonWriter.writeJsonField("encoded", this.encoded); + jsonWriter.writeStringField("uuid", this.uuid); + return jsonWriter.writeEndObject(); + } + + /** + * Reads an instance of Builtin from the JsonReader. + * + * @param jsonReader The JsonReader being read. + * @return An instance of Builtin if the JsonReader was pointing to an instance of it, or null if it was pointing to + * JSON null. + * @throws IllegalStateException If the deserialized JSON object was missing any required properties. + * @throws IOException If an error occurs while reading the Builtin. + */ + public static Builtin fromJson(JsonReader jsonReader) throws IOException { + return jsonReader.readObject(reader -> { + Builtin deserializedBuiltin = new Builtin(); + while (reader.nextToken() != JsonToken.END_OBJECT) { + String fieldName = reader.getFieldName(); + reader.nextToken(); + + if ("boolean".equals(fieldName)) { + deserializedBuiltin.booleanProperty = reader.getBoolean(); + } else if ("string".equals(fieldName)) { + deserializedBuiltin.string = reader.getString(); + } else if ("bytes".equals(fieldName)) { + deserializedBuiltin.bytes = reader.getBinary(); + } else if ("int".equals(fieldName)) { + deserializedBuiltin.intProperty = reader.getInt(); + } else if ("safeint".equals(fieldName)) { + deserializedBuiltin.safeint = reader.getLong(); + } else if ("decimal".equals(fieldName)) { + deserializedBuiltin.decimal + = reader.getNullable(nonNullReader -> new BigDecimal(nonNullReader.getString())); + } else if ("long".equals(fieldName)) { + deserializedBuiltin.longProperty = reader.getLong(); + } else if ("float".equals(fieldName)) { + deserializedBuiltin.floatProperty = reader.getDouble(); + } else if ("double".equals(fieldName)) { + deserializedBuiltin.doubleProperty = reader.getDouble(); + } else if ("duration".equals(fieldName)) { + deserializedBuiltin.duration + = reader.getNullable(nonNullReader -> Duration.parse(nonNullReader.getString())); + } else if ("date".equals(fieldName)) { + deserializedBuiltin.date + = reader.getNullable(nonNullReader -> LocalDate.parse(nonNullReader.getString())); + } else if ("dateTime".equals(fieldName)) { + deserializedBuiltin.dateTime = reader + .getNullable(nonNullReader -> CoreUtils.parseBestOffsetDateTime(nonNullReader.getString())); + } else if ("stringList".equals(fieldName)) { + List stringList = reader.readArray(reader1 -> reader1.getString()); + deserializedBuiltin.stringList = stringList; + } else if ("bytesDict".equals(fieldName)) { + Map bytesDict = reader.readMap(reader1 -> reader1.getBinary()); + deserializedBuiltin.bytesDict = bytesDict; + } else if ("url".equals(fieldName)) { + deserializedBuiltin.url = reader.getString(); + } else if ("nullableFloatDict".equals(fieldName)) { + Map nullableFloatDict + = reader.readMap(reader1 -> reader1.getNullable(JsonReader::getDouble)); + deserializedBuiltin.nullableFloatDict = nullableFloatDict; + } else if ("encoded".equals(fieldName)) { + deserializedBuiltin.encoded = Encoded.fromJson(reader); + } else if ("uuid".equals(fieldName)) { + deserializedBuiltin.uuid = reader.getString(); + } else { + reader.skipChildren(); + } + } + + return deserializedBuiltin; + }); + } +} diff --git a/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/armstreamstyleserialization/models/Encoded.java b/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/armstreamstyleserialization/models/Encoded.java new file mode 100644 index 00000000000..9d2f092a839 --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/armstreamstyleserialization/models/Encoded.java @@ -0,0 +1,265 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +// Code generated by Microsoft (R) TypeSpec Code Generator. + +package tsptest.armstreamstyleserialization.models; + +import com.azure.core.annotation.Immutable; +import com.azure.core.util.Base64Url; +import com.azure.core.util.CoreUtils; +import com.azure.core.util.DateTimeRfc1123; +import com.azure.json.JsonReader; +import com.azure.json.JsonSerializable; +import com.azure.json.JsonToken; +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.Objects; + +/** + * The Encoded model. + */ +@Immutable +public final class Encoded implements JsonSerializable { + private static final byte[] EMPTY_BYTE_ARRAY = new byte[0]; + + /* + * The timeInSeconds property. + */ + private Long timeInSeconds; + + /* + * The timeInSecondsFraction property. + */ + private Double timeInSecondsFraction; + + /* + * The dateTime property. + */ + private OffsetDateTime dateTime; + + /* + * The dateTimeRfc7231 property. + */ + private DateTimeRfc1123 dateTimeRfc7231; + + /* + * The unixTimestamp property. + */ + private Long unixTimestamp; + + /* + * The base64 property. + */ + private byte[] base64; + + /* + * The base64url property. + */ + private Base64Url base64url; + + /* + * The unknownDurationFormat property. + */ + private String unknownDurationFormat; + + /* + * The unknownDateTimeFormat property. + */ + private String unknownDateTimeFormat; + + /* + * The unknownBytes property. + */ + private String unknownBytes; + + /** + * Creates an instance of Encoded class. + */ + private Encoded() { + } + + /** + * Get the timeInSeconds property: The timeInSeconds property. + * + * @return the timeInSeconds value. + */ + public Duration timeInSeconds() { + if (this.timeInSeconds == null) { + return null; + } + return Duration.ofSeconds(this.timeInSeconds); + } + + /** + * Get the timeInSecondsFraction property: The timeInSecondsFraction property. + * + * @return the timeInSecondsFraction value. + */ + public Duration timeInSecondsFraction() { + if (this.timeInSecondsFraction == null) { + return null; + } + return Duration.ofNanos((long) (this.timeInSecondsFraction * 1000_000_000L)); + } + + /** + * Get the dateTime property: The dateTime property. + * + * @return the dateTime value. + */ + public OffsetDateTime dateTime() { + return this.dateTime; + } + + /** + * Get the dateTimeRfc7231 property: The dateTimeRfc7231 property. + * + * @return the dateTimeRfc7231 value. + */ + public OffsetDateTime dateTimeRfc7231() { + if (this.dateTimeRfc7231 == null) { + return null; + } + return this.dateTimeRfc7231.getDateTime(); + } + + /** + * Get the unixTimestamp property: The unixTimestamp property. + * + * @return the unixTimestamp value. + */ + public OffsetDateTime unixTimestamp() { + if (this.unixTimestamp == null) { + return null; + } + return OffsetDateTime.ofInstant(Instant.ofEpochSecond(this.unixTimestamp), ZoneOffset.UTC); + } + + /** + * Get the base64 property: The base64 property. + * + * @return the base64 value. + */ + public byte[] base64() { + return CoreUtils.clone(this.base64); + } + + /** + * Get the base64url property: The base64url property. + * + * @return the base64url value. + */ + public byte[] base64url() { + if (this.base64url == null) { + return EMPTY_BYTE_ARRAY; + } + return this.base64url.decodedBytes(); + } + + /** + * Get the unknownDurationFormat property: The unknownDurationFormat property. + * + * @return the unknownDurationFormat value. + */ + public String unknownDurationFormat() { + return this.unknownDurationFormat; + } + + /** + * Get the unknownDateTimeFormat property: The unknownDateTimeFormat property. + * + * @return the unknownDateTimeFormat value. + */ + public String unknownDateTimeFormat() { + return this.unknownDateTimeFormat; + } + + /** + * Get the unknownBytes property: The unknownBytes property. + * + * @return the unknownBytes value. + */ + public String unknownBytes() { + return this.unknownBytes; + } + + /** + * Validates the instance. + * + * @throws IllegalArgumentException thrown if the instance is not valid. + */ + public void validate() { + } + + /** + * {@inheritDoc} + */ + @Override + public JsonWriter toJson(JsonWriter jsonWriter) throws IOException { + jsonWriter.writeStartObject(); + jsonWriter.writeNumberField("timeInSeconds", this.timeInSeconds); + jsonWriter.writeNumberField("timeInSecondsFraction", this.timeInSecondsFraction); + jsonWriter.writeStringField("dateTime", + this.dateTime == null ? null : DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(this.dateTime)); + jsonWriter.writeStringField("dateTimeRfc7231", Objects.toString(this.dateTimeRfc7231, null)); + jsonWriter.writeNumberField("unixTimestamp", this.unixTimestamp); + jsonWriter.writeBinaryField("base64", this.base64); + jsonWriter.writeStringField("base64url", Objects.toString(this.base64url, null)); + jsonWriter.writeStringField("unknownDurationFormat", this.unknownDurationFormat); + jsonWriter.writeStringField("unknownDateTimeFormat", this.unknownDateTimeFormat); + jsonWriter.writeStringField("unknownBytes", this.unknownBytes); + return jsonWriter.writeEndObject(); + } + + /** + * Reads an instance of Encoded from the JsonReader. + * + * @param jsonReader The JsonReader being read. + * @return An instance of Encoded if the JsonReader was pointing to an instance of it, or null if it was pointing to + * JSON null. + * @throws IOException If an error occurs while reading the Encoded. + */ + public static Encoded fromJson(JsonReader jsonReader) throws IOException { + return jsonReader.readObject(reader -> { + Encoded deserializedEncoded = new Encoded(); + while (reader.nextToken() != JsonToken.END_OBJECT) { + String fieldName = reader.getFieldName(); + reader.nextToken(); + + if ("timeInSeconds".equals(fieldName)) { + deserializedEncoded.timeInSeconds = reader.getNullable(JsonReader::getLong); + } else if ("timeInSecondsFraction".equals(fieldName)) { + deserializedEncoded.timeInSecondsFraction = reader.getNullable(JsonReader::getDouble); + } else if ("dateTime".equals(fieldName)) { + deserializedEncoded.dateTime = reader + .getNullable(nonNullReader -> CoreUtils.parseBestOffsetDateTime(nonNullReader.getString())); + } else if ("dateTimeRfc7231".equals(fieldName)) { + deserializedEncoded.dateTimeRfc7231 + = reader.getNullable(nonNullReader -> new DateTimeRfc1123(nonNullReader.getString())); + } else if ("unixTimestamp".equals(fieldName)) { + deserializedEncoded.unixTimestamp = reader.getNullable(JsonReader::getLong); + } else if ("base64".equals(fieldName)) { + deserializedEncoded.base64 = reader.getBinary(); + } else if ("base64url".equals(fieldName)) { + deserializedEncoded.base64url + = reader.getNullable(nonNullReader -> new Base64Url(nonNullReader.getString())); + } else if ("unknownDurationFormat".equals(fieldName)) { + deserializedEncoded.unknownDurationFormat = reader.getString(); + } else if ("unknownDateTimeFormat".equals(fieldName)) { + deserializedEncoded.unknownDateTimeFormat = reader.getString(); + } else if ("unknownBytes".equals(fieldName)) { + deserializedEncoded.unknownBytes = reader.getString(); + } else { + reader.skipChildren(); + } + } + + return deserializedEncoded; + }); + } +} diff --git a/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/armstreamstyleserialization/models/TopLevelArmResourceProperties.java b/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/armstreamstyleserialization/models/TopLevelArmResourceProperties.java index 36bb497b5be..28cb04d6533 100644 --- a/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/armstreamstyleserialization/models/TopLevelArmResourceProperties.java +++ b/packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/armstreamstyleserialization/models/TopLevelArmResourceProperties.java @@ -5,6 +5,7 @@ package tsptest.armstreamstyleserialization.models; import com.azure.core.annotation.Immutable; +import com.azure.core.util.logging.ClientLogger; import com.azure.json.JsonReader; import com.azure.json.JsonSerializable; import com.azure.json.JsonToken; @@ -21,6 +22,11 @@ public final class TopLevelArmResourceProperties implements JsonSerializable { @doc("Top Level Arm Resource Properties.") model TopLevelArmResourceProperties { description?: string; + builtin: Builtin; } @error @@ -286,6 +287,61 @@ model ResultData { prop2: string; } +model Builtin { + boolean: boolean; + string: string; + bytes: bytes; + int: int32; + safeint: safeint; + decimal: decimal; + long: int64; + float: float32; + double: float64; + duration: duration; + date: plainDate; + dateTime: utcDateTime; + stringList: string[]; + bytesDict: Record; + url: url; + nullableFloatDict: Record; + encoded: Encoded; + uuid: Azure.Core.uuid; +} + +@encode(DurationKnownEncoding.seconds, float32) +scalar myDuration extends duration; + +model Encoded { + @encode(DurationKnownEncoding.seconds, int32) + timeInSeconds?: duration; + + timeInSecondsFraction?: myDuration; + + @encode(DateTimeKnownEncoding.rfc3339) + dateTime?: utcDateTime; + + @encode(DateTimeKnownEncoding.rfc7231) + dateTimeRfc7231?: utcDateTime; + + @encode(DateTimeKnownEncoding.unixTimestamp, int64) + unixTimestamp?: utcDateTime; + + @encode(BytesKnownEncoding.base64) + base64?: bytes; + + @encode(BytesKnownEncoding.base64url) + base64url?: bytes; + + @encode("unknown-duration") + unknownDurationFormat?: duration; + + @encode("unknown-datetime") + unknownDateTimeFormat?: utcDateTime; + + @encode("unknown-bytes") + unknownBytes?: bytes; +} + @@flattenProperty(Fish.properties); @@flattenProperty(FishProperties.tailProperties); @@flattenProperty(Fish.anotherProperties);