Skip to content

Commit 9f01672

Browse files
http-client-java, support array encoded as CSV on property (#9218)
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
1 parent 09ef693 commit 9f01672

37 files changed

Lines changed: 1034 additions & 401 deletions

File tree

packages/http-client-java/emitter/src/code-model-builder.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ import { fail } from "assert";
108108
import pkg from "lodash";
109109
import {
110110
Client as CodeModelClient,
111+
EncodedProperty,
111112
EncodedSchema,
112113
PageableContinuationToken,
113114
Serializable,
@@ -2865,6 +2866,18 @@ export class CodeModelBuilder {
28652866
serializedName: getPropertySerializedName(modelProperty),
28662867
extensions: extensions,
28672868
});
2869+
if (modelProperty.encode) {
2870+
if (schema instanceof ArraySchema) {
2871+
const elementSchema = schema.elementType;
2872+
if (!(elementSchema instanceof StringSchema)) {
2873+
reportDiagnostic(this.program, {
2874+
code: "non-string-array-encoding-element-notsupported",
2875+
target: modelProperty.__raw ?? NoTarget,
2876+
});
2877+
}
2878+
}
2879+
(codeModelProperty as EncodedProperty).arrayEncoding = modelProperty.encode;
2880+
}
28682881

28692882
// xml
28702883
if (modelProperty.serializationOptions.xml) {

packages/http-client-java/emitter/src/common/client.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
Security,
1010
} from "@autorest/codemodel";
1111
import { DeepPartial } from "@azure-tools/codegen";
12+
import { ArrayKnownEncoding } from "@azure-tools/typespec-client-generator-core";
1213
import { XmlSerializationFormat } from "./formats/xml.js";
1314

1415
export interface Client extends Aspect {
@@ -103,6 +104,14 @@ export interface EncodedSchema {
103104
encode?: string;
104105
}
105106

107+
export interface EncodedProperty {
108+
/**
109+
* The encoding of array items.
110+
* The type for SDK would "SdkArrayType" with a "valueType", the type on wire be "string".
111+
*/
112+
arrayEncoding?: ArrayKnownEncoding;
113+
}
114+
106115
export class PageableContinuationToken {
107116
/**
108117
* The parameter of the operation as continuationToken in API request.

packages/http-client-java/emitter/src/lib.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ export const $lib = createTypeSpecLibrary({
119119
default: paramMessage`Constant header '${"headerName"}' is removed from response headers.`,
120120
},
121121
},
122+
"non-string-array-encoding-element-notsupported": {
123+
severity: "error",
124+
messages: {
125+
default: "Element of type other than 'string' is not supported for 'ArrayEncoding'.",
126+
},
127+
},
122128
},
123129
emitter: {
124130
options: EmitterOptionsSchema,

packages/http-client-java/generator/http-client-generator-clientcore-test/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"@typespec/http-client-java-tests": "file:"
2121
},
2222
"overrides": {
23-
"@typespec/compiler": "1.7.0",
23+
"@typespec/compiler": "1.7.1",
2424
"@typespec/http": "1.7.0",
2525
"@typespec/rest": "0.77.0",
2626
"@typespec/versioning": "0.77.0",

packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/array/CommaDelimitedArrayProperty.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
import io.clientcore.core.serialization.json.JsonToken;
88
import io.clientcore.core.serialization.json.JsonWriter;
99
import java.io.IOException;
10+
import java.util.Arrays;
11+
import java.util.LinkedList;
1012
import java.util.List;
13+
import java.util.stream.Collectors;
1114

1215
/**
1316
* The CommaDelimitedArrayProperty model.
@@ -47,7 +50,10 @@ public List<String> getValue() {
4750
@Override
4851
public JsonWriter toJson(JsonWriter jsonWriter) throws IOException {
4952
jsonWriter.writeStartObject();
50-
jsonWriter.writeArrayField("value", this.value, (writer, element) -> writer.writeString(element));
53+
if (this.value != null) {
54+
jsonWriter.writeStringField("value",
55+
this.value.stream().map(element -> element == null ? "" : element).collect(Collectors.joining(",")));
56+
}
5157
return jsonWriter.writeEndObject();
5258
}
5359

@@ -69,7 +75,12 @@ public static CommaDelimitedArrayProperty fromJson(JsonReader jsonReader) throws
6975
reader.nextToken();
7076

7177
if ("value".equals(fieldName)) {
72-
value = reader.readArray(reader1 -> reader1.getString());
78+
String valueEncodedAsString = reader.getString();
79+
value = valueEncodedAsString == null
80+
? null
81+
: valueEncodedAsString.isEmpty()
82+
? new LinkedList<>()
83+
: new LinkedList<>(Arrays.asList(valueEncodedAsString.split(",", -1)));
7384
} else {
7485
reader.skipChildren();
7586
}

packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/array/NewlineDelimitedArrayProperty.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
import io.clientcore.core.serialization.json.JsonToken;
88
import io.clientcore.core.serialization.json.JsonWriter;
99
import java.io.IOException;
10+
import java.util.Arrays;
11+
import java.util.LinkedList;
1012
import java.util.List;
13+
import java.util.stream.Collectors;
1114

1215
/**
1316
* The NewlineDelimitedArrayProperty model.
@@ -47,7 +50,10 @@ public List<String> getValue() {
4750
@Override
4851
public JsonWriter toJson(JsonWriter jsonWriter) throws IOException {
4952
jsonWriter.writeStartObject();
50-
jsonWriter.writeArrayField("value", this.value, (writer, element) -> writer.writeString(element));
53+
if (this.value != null) {
54+
jsonWriter.writeStringField("value",
55+
this.value.stream().map(element -> element == null ? "" : element).collect(Collectors.joining("\n")));
56+
}
5157
return jsonWriter.writeEndObject();
5258
}
5359

@@ -69,7 +75,12 @@ public static NewlineDelimitedArrayProperty fromJson(JsonReader jsonReader) thro
6975
reader.nextToken();
7076

7177
if ("value".equals(fieldName)) {
72-
value = reader.readArray(reader1 -> reader1.getString());
78+
String valueEncodedAsString = reader.getString();
79+
value = valueEncodedAsString == null
80+
? null
81+
: valueEncodedAsString.isEmpty()
82+
? new LinkedList<>()
83+
: new LinkedList<>(Arrays.asList(valueEncodedAsString.split("\n", -1)));
7384
} else {
7485
reader.skipChildren();
7586
}

packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/array/PipeDelimitedArrayProperty.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
import io.clientcore.core.serialization.json.JsonToken;
88
import io.clientcore.core.serialization.json.JsonWriter;
99
import java.io.IOException;
10+
import java.util.Arrays;
11+
import java.util.LinkedList;
1012
import java.util.List;
13+
import java.util.stream.Collectors;
1114

1215
/**
1316
* The PipeDelimitedArrayProperty model.
@@ -47,7 +50,10 @@ public List<String> getValue() {
4750
@Override
4851
public JsonWriter toJson(JsonWriter jsonWriter) throws IOException {
4952
jsonWriter.writeStartObject();
50-
jsonWriter.writeArrayField("value", this.value, (writer, element) -> writer.writeString(element));
53+
if (this.value != null) {
54+
jsonWriter.writeStringField("value",
55+
this.value.stream().map(element -> element == null ? "" : element).collect(Collectors.joining("|")));
56+
}
5157
return jsonWriter.writeEndObject();
5258
}
5359

@@ -69,7 +75,12 @@ public static PipeDelimitedArrayProperty fromJson(JsonReader jsonReader) throws
6975
reader.nextToken();
7076

7177
if ("value".equals(fieldName)) {
72-
value = reader.readArray(reader1 -> reader1.getString());
78+
String valueEncodedAsString = reader.getString();
79+
value = valueEncodedAsString == null
80+
? null
81+
: valueEncodedAsString.isEmpty()
82+
? new LinkedList<>()
83+
: new LinkedList<>(Arrays.asList(valueEncodedAsString.split("\\|", -1)));
7384
} else {
7485
reader.skipChildren();
7586
}

packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/encode/array/SpaceDelimitedArrayProperty.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
import io.clientcore.core.serialization.json.JsonToken;
88
import io.clientcore.core.serialization.json.JsonWriter;
99
import java.io.IOException;
10+
import java.util.Arrays;
11+
import java.util.LinkedList;
1012
import java.util.List;
13+
import java.util.stream.Collectors;
1114

1215
/**
1316
* The SpaceDelimitedArrayProperty model.
@@ -47,7 +50,10 @@ public List<String> getValue() {
4750
@Override
4851
public JsonWriter toJson(JsonWriter jsonWriter) throws IOException {
4952
jsonWriter.writeStartObject();
50-
jsonWriter.writeArrayField("value", this.value, (writer, element) -> writer.writeString(element));
53+
if (this.value != null) {
54+
jsonWriter.writeStringField("value",
55+
this.value.stream().map(element -> element == null ? "" : element).collect(Collectors.joining(" ")));
56+
}
5157
return jsonWriter.writeEndObject();
5258
}
5359

@@ -69,7 +75,12 @@ public static SpaceDelimitedArrayProperty fromJson(JsonReader jsonReader) throws
6975
reader.nextToken();
7076

7177
if ("value".equals(fieldName)) {
72-
value = reader.readArray(reader1 -> reader1.getString());
78+
String valueEncodedAsString = reader.getString();
79+
value = valueEncodedAsString == null
80+
? null
81+
: valueEncodedAsString.isEmpty()
82+
? new LinkedList<>()
83+
: new LinkedList<>(Arrays.asList(valueEncodedAsString.split(" ", -1)));
7384
} else {
7485
reader.skipChildren();
7586
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package encode.array;
5+
6+
import java.util.List;
7+
import org.junit.jupiter.api.Assertions;
8+
import org.junit.jupiter.api.Test;
9+
10+
public final class EncodeArrayTests {
11+
12+
private static final List<String> COLORS = List.of("blue", "red", "green");
13+
14+
private final ArrayClient client = new ArrayClientBuilder().buildArrayClient();
15+
16+
@Test
17+
public void commaDelimitedProperty() {
18+
CommaDelimitedArrayProperty response = client.commaDelimited(new CommaDelimitedArrayProperty(COLORS));
19+
Assertions.assertEquals(COLORS, response.getValue());
20+
}
21+
22+
@Test
23+
public void spaceDelimitedProperty() {
24+
SpaceDelimitedArrayProperty response = client.spaceDelimited(new SpaceDelimitedArrayProperty(COLORS));
25+
Assertions.assertEquals(COLORS, response.getValue());
26+
}
27+
28+
@Test
29+
public void pipeDelimitedProperty() {
30+
PipeDelimitedArrayProperty response = client.pipeDelimited(new PipeDelimitedArrayProperty(COLORS));
31+
Assertions.assertEquals(COLORS, response.getValue());
32+
}
33+
34+
@Test
35+
public void newlineDelimitedProperty() {
36+
NewlineDelimitedArrayProperty response = client.newlineDelimited(new NewlineDelimitedArrayProperty(COLORS));
37+
Assertions.assertEquals(COLORS, response.getValue());
38+
}
39+
}

packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/extension/model/codemodel/Property.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class Property extends Value {
1818
private String summary;
1919
// TypeSpec only
2020
private SerializationFormats serialization;
21+
private String arrayEncoding;
2122
// internal use, not from modelerfour
2223
private ObjectSchema parentSchema;
2324

@@ -161,6 +162,24 @@ public void setSerialization(SerializationFormats serialization) {
161162
this.serialization = serialization;
162163
}
163164

165+
/**
166+
* Gets the array encoding style of this property.
167+
*
168+
* @return The array encoding style of this property.
169+
*/
170+
public String getArrayEncoding() {
171+
return arrayEncoding;
172+
}
173+
174+
/**
175+
* Sets the array encoding style of this property.
176+
*
177+
* @param arrayEncoding The array encoding style of this property.
178+
*/
179+
public void setArrayEncoding(String arrayEncoding) {
180+
this.arrayEncoding = arrayEncoding;
181+
}
182+
164183
@Override
165184
public String getSummary() {
166185
return summary;

0 commit comments

Comments
 (0)