Skip to content

Conversation

@weidongxu-microsoft
Copy link
Contributor

@weidongxu-microsoft weidongxu-microsoft commented Dec 16, 2025

fix #9027

see https://github.com/microsoft/typespec/pull/9218/files#diff-e8b978e5080675f7527d4beb363a9dde604493dbff1db49116ea56c94b2339e1 on difference on generated code.

Currently only supports element as String. (the convert from a subtype to String need some refactor in existing code)

release PR is Azure/autorest.java#3242

@microsoft-github-policy-service microsoft-github-policy-service bot added the emitter:client:java Issue for the Java client emitter: @typespec/http-client-java label Dec 16, 2025
@github-actions
Copy link
Contributor

No changes needing a change description found.

@azure-sdk
Copy link
Collaborator

azure-sdk commented Dec 16, 2025

You can try these changes here

🛝 Playground 🌐 Website 🛝 VSCode Extension

Copy link
Contributor Author

@weidongxu-microsoft weidongxu-microsoft Dec 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not related to this PR/feature.

Removed due to failure in nightly build, because an unreleased commit there breaked this test into 4 tests. Hence test code here won't compile on dev.

Need to be added back in next typespec version bump. #9220

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds support for delimited array encoding (CSV and other formats) on properties in the http-client-java emitter. The implementation allows TypeSpec models to specify how arrays of strings should be serialized as delimited strings in JSON (comma, space, pipe, or newline-delimited) instead of as JSON arrays. The changes span the code generator, model templates, and include both Azure and clientcore test implementations.

Key Changes

  • Introduces ArrayEncoding enum to define delimiter strategies for array serialization
  • Extends ClientModelProperty to include array encoding metadata
  • Updates serialization/deserialization templates to handle delimited string encoding
  • Adds validation to ensure only string element types are supported for array encoding

Reviewed changes

Copilot reviewed 22 out of 23 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
packages/http-client-java/package.json Updates TypeSpec compiler and various dev dependencies to newer versions
packages/http-client-java/package-lock.json Lockfile updates corresponding to package.json changes
packages/http-client-java/generator/http-client-generator-test/src/test/java/encode/array/EncodeArrayTests.java New test suite for array encoding with comma, space, pipe, and newline delimiters
packages/http-client-java/generator/http-client-generator-test/src/test/java/azure/clientgenerator/core/clientlocation/ClientLocationClientTests.java Test file removed (appears to be replaced by generated tests)
packages/http-client-java/generator/http-client-generator-test/src/main/java/encode/array/models/*.java Generated model classes implementing delimiter-based serialization for different array encoding types
packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/StreamSerializationModelTemplate.java Updates toJson/fromJson generation to handle array encoding with delimiter-based string serialization
packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/ClientModelProperty.java Adds arrayEncoding field to track delimiter configuration
packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/ArrayEncoding.java New enum defining delimiter types and their escape sequences
packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/ModelPropertyMapper.java Maps array encoding metadata from TypeSpec model to Java property
packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/extension/model/codemodel/Property.java Adds arrayEncoding field to Property model
packages/http-client-java/generator/http-client-generator-clientcore-test/src/**/*.java Clientcore test implementations mirroring the Azure test structure
packages/http-client-java/emitter/src/lib.ts Adds diagnostic for unsupported non-string array encoding elements
packages/http-client-java/emitter/src/common/client.ts Defines EncodedProperty interface for array encoding metadata
packages/http-client-java/emitter/src/code-model-builder.ts Validates string element type requirement and maps array encoding to code model
Files not reviewed (1)
  • packages/http-client-java/package-lock.json: Language not supported

Comment on lines +757 to +763
methodBlock.ifBlock(propertyValueGetter + " != null", ifBlock -> {
String serializeExpression = propertyValueGetter
+ ".stream().map(element -> element == null ? \"\" : element).collect(Collectors.joining(\""
+ property.getArrayEncoding().getDelimiter() + "\"))";
methodBlock.line("jsonWriter.writeStringField(\"%s\", %s);", serializedName,
serializeExpression);
});
Copy link

Copilot AI Dec 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When serializing array elements, null elements are converted to empty strings with "element == null ? "" : element". However, during deserialization with split(), there's no corresponding logic to convert empty strings back to null. This creates an asymmetry where null array elements become empty strings after a round-trip serialization/deserialization cycle. Consider either preserving null elements differently (e.g., using a special placeholder) or documenting that null elements will be converted to empty strings.

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is valid concern. Had this in mind, particularly when the element type is utcDateTime, where empty string can only be mapped to null (no valid time on "").

However, for string, "" is valid.

@weidongxu-microsoft weidongxu-microsoft changed the title http-client-java, support csv on property http-client-java, support array encoded as CSV on property Dec 16, 2025
@weidongxu-microsoft weidongxu-microsoft marked this pull request as ready for review December 16, 2025 06:40
Comment on lines +54 to +55
jsonWriter.writeStringField("value",
this.value.stream().map(element -> element == null ? "" : element).collect(Collectors.joining(",")));
Copy link
Contributor Author

@weidongxu-microsoft weidongxu-microsoft Dec 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both null and "" would map to same encoded string.

On the other direction, currently we map e.g. "," to ["", ""] for string element.
For other type that extend string, we may choose differently (or even break this behavior on string type too), e.g. "," on utcDateTime/uuid can only be [null, null]

Just hope service knows what it does on these...

Copy link
Member

@haolingdong-msft haolingdong-msft left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good to me. Add a nit. Thanks!

isJsonMergePatch);
} else {
// wireType is String
// at present, only String element is supported
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only String element is supported

nit: Do we want to add check here to mak sure the element type is string? Or could we at least mention we have the on emitter: https://github.com/microsoft/typespec/pull/9218/files#diff-a10993e19db8ea9bd765f4d6abeaff1624f8f9a3842b9203c207f07527ef3d2eR2869
Because I tried to find in the code below that do the check on supporting string element only, but seems did not find it. (happy to be corrected)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

public JsonWriter toJson(JsonWriter jsonWriter) throws IOException {
jsonWriter.writeStartObject();
jsonWriter.writeArrayField("value", this.value, (writer, element) -> writer.writeString(element));
if (this.value != null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generated code looks good to me.

@weidongxu-microsoft weidongxu-microsoft force-pushed the http-client-java_support-csv-on-property branch from bdc3cbd to fd843b1 Compare December 17, 2025 03:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

emitter:client:java Issue for the Java client emitter: @typespec/http-client-java

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[http-client-java] CSV encoding for model properties Implementation

4 participants