Skip to content

Commit d4d5c3d

Browse files
http-client-java, use wire type for unknown encode (#6244)
user can put any value in `@encode` decorator and apparently emitter cannot support all of them. ``` @encode("whatever-user-wanted") prop: duration; ``` Hence fallback to wire type, with a warning.
1 parent 41efd5f commit d4d5c3d

File tree

8 files changed

+173
-7
lines changed

8 files changed

+173
-7
lines changed

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

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ import {
128128
operationIsMultipleContentTypes,
129129
} from "./operation-utils.js";
130130
import {
131+
BYTES_KNOWN_ENCODING,
132+
DATETIME_KNOWN_ENCODING,
133+
DURATION_KNOWN_ENCODING,
131134
ProcessingCache,
132135
getAccess,
133136
getDurationFormat,
@@ -2054,17 +2057,35 @@ export class CodeModelBuilder {
20542057
return this.processArraySchema(type, nameHint);
20552058

20562059
case "duration":
2057-
return this.processDurationSchema(type, nameHint, getDurationFormat(type));
2060+
if (DURATION_KNOWN_ENCODING.includes(type.encode)) {
2061+
return this.processDurationSchema(type, nameHint, getDurationFormat(type));
2062+
} else {
2063+
reportDiagnostic(this.program, {
2064+
code: "unknown-encode",
2065+
format: { encode: type.encode },
2066+
target: type.__raw ?? NoTarget,
2067+
});
2068+
return this.processBuiltInType(type.wireType, nameHint);
2069+
}
20582070

20592071
case "constant":
20602072
return this.processConstantSchema(type, nameHint);
20612073

20622074
case "utcDateTime":
20632075
case "offsetDateTime":
2064-
if (type.encode === "unixTimestamp") {
2065-
return this.processUnixTimeSchema(type, nameHint);
2076+
if (DATETIME_KNOWN_ENCODING.includes(type.encode)) {
2077+
if (type.encode === "unixTimestamp") {
2078+
return this.processUnixTimeSchema(type, nameHint);
2079+
} else {
2080+
return this.processDateTimeSchema(type, nameHint, type.encode === "rfc7231");
2081+
}
20662082
} else {
2067-
return this.processDateTimeSchema(type, nameHint, type.encode === "rfc7231");
2083+
reportDiagnostic(this.program, {
2084+
code: "unknown-encode",
2085+
format: { encode: type.encode },
2086+
target: type.__raw ?? NoTarget,
2087+
});
2088+
return this.processBuiltInType(type.wireType, nameHint);
20682089
}
20692090
}
20702091
}
@@ -2100,7 +2121,16 @@ export class CodeModelBuilder {
21002121
return this.processDecimalSchema(type, nameHint);
21012122

21022123
case "bytes":
2103-
return this.processByteArraySchema(type, nameHint);
2124+
if (!type.encode || BYTES_KNOWN_ENCODING.includes(type.encode)) {
2125+
return this.processByteArraySchema(type, nameHint);
2126+
} else {
2127+
reportDiagnostic(this.program, {
2128+
code: "unknown-encode",
2129+
format: { encode: type.encode },
2130+
target: type.__raw ?? NoTarget,
2131+
});
2132+
return this.processStringSchema(type, nameHint);
2133+
}
21042134

21052135
case "boolean":
21062136
return this.processBooleanSchema(type, nameHint);

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,12 @@ export const $lib = createTypeSpecLibrary({
166166
default: paramMessage`Header parameter format '${"format"}' is not supported.`,
167167
},
168168
},
169+
"unknown-encode": {
170+
severity: "warning",
171+
messages: {
172+
default: paramMessage`Encode '${"encode"}' is not supported.`,
173+
},
174+
},
169175
},
170176
emitter: {
171177
options: EmitterOptionsSchema as JSONSchemaType<EmitterOptions>,

packages/http-client-java/emitter/src/type-utils.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ import { DurationSchema } from "./common/schemas/time.js";
3232
import { SchemaContext } from "./common/schemas/usage.js";
3333
import { getNamespace } from "./utils.js";
3434

35+
export const DURATION_KNOWN_ENCODING = ["ISO8601", "seconds"];
36+
export const DATETIME_KNOWN_ENCODING = ["rfc3339", "rfc7231", "unixTimestamp"];
37+
export const BYTES_KNOWN_ENCODING = ["base64", "base64url"];
38+
3539
/** Acts as a cache for processing inputs.
3640
*
3741
* If the input is undefined, the output is always undefined.
@@ -342,11 +346,11 @@ export function isArmCommonType(entity: Type): boolean {
342346
}
343347

344348
export function getPropertySerializedName(property: SdkBodyModelPropertyType): string {
345-
// TODO: remove the "property.serializedName" after bug https://github.com/microsoft/typespec/pull/5702 is fixed
349+
// still fallback to "property.name", as for orphan model, serializationOptions.json is undefined
346350
return (
347351
property.serializationOptions.json?.name ??
348352
property.serializationOptions.multipart?.name ??
349-
property.serializedName
353+
property.name
350354
);
351355
}
352356

packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/builtin/BuiltinAsyncClient.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ public final class BuiltinAsyncClient {
9494
* unixTimestamp: Long (Optional)
9595
* base64: byte[] (Optional)
9696
* base64url: Base64Url (Optional)
97+
* unknownDurationFormat: String (Optional)
98+
* unknownDateTimeFormat: String (Optional)
99+
* unknownBytes: String (Optional)
97100
* }
98101
* }
99102
* }
@@ -152,6 +155,9 @@ public Mono<Response<BinaryData>> readWithResponse(String queryParam, String que
152155
* unixTimestamp: Long (Optional)
153156
* base64: byte[] (Optional)
154157
* base64url: Base64Url (Optional)
158+
* unknownDurationFormat: String (Optional)
159+
* unknownDateTimeFormat: String (Optional)
160+
* unknownBytes: String (Optional)
155161
* }
156162
* }
157163
* }

packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/builtin/BuiltinClient.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ public final class BuiltinClient {
9292
* unixTimestamp: Long (Optional)
9393
* base64: byte[] (Optional)
9494
* base64url: Base64Url (Optional)
95+
* unknownDurationFormat: String (Optional)
96+
* unknownDateTimeFormat: String (Optional)
97+
* unknownBytes: String (Optional)
9598
* }
9699
* }
97100
* }
@@ -150,6 +153,9 @@ public Response<BinaryData> readWithResponse(String queryParam, String queryPara
150153
* unixTimestamp: Long (Optional)
151154
* base64: byte[] (Optional)
152155
* base64url: Base64Url (Optional)
156+
* unknownDurationFormat: String (Optional)
157+
* unknownDateTimeFormat: String (Optional)
158+
* unknownBytes: String (Optional)
153159
* }
154160
* }
155161
* }

packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/builtin/implementation/BuiltinOpsImpl.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ Response<Void> writeSync(@HostParam("endpoint") String endpoint,
154154
* unixTimestamp: Long (Optional)
155155
* base64: byte[] (Optional)
156156
* base64url: Base64Url (Optional)
157+
* unknownDurationFormat: String (Optional)
158+
* unknownDateTimeFormat: String (Optional)
159+
* unknownBytes: String (Optional)
157160
* }
158161
* }
159162
* }
@@ -229,6 +232,9 @@ public Mono<Response<BinaryData>> readWithResponseAsync(String queryParam, Strin
229232
* unixTimestamp: Long (Optional)
230233
* base64: byte[] (Optional)
231234
* base64url: Base64Url (Optional)
235+
* unknownDurationFormat: String (Optional)
236+
* unknownDateTimeFormat: String (Optional)
237+
* unknownBytes: String (Optional)
232238
* }
233239
* }
234240
* }
@@ -288,6 +294,9 @@ public Response<BinaryData> readWithResponse(String queryParam, String queryPara
288294
* unixTimestamp: Long (Optional)
289295
* base64: byte[] (Optional)
290296
* base64url: Base64Url (Optional)
297+
* unknownDurationFormat: String (Optional)
298+
* unknownDateTimeFormat: String (Optional)
299+
* unknownBytes: String (Optional)
291300
* }
292301
* }
293302
* }
@@ -345,6 +354,9 @@ public Mono<Response<Void>> writeWithResponseAsync(BinaryData body, RequestOptio
345354
* unixTimestamp: Long (Optional)
346355
* base64: byte[] (Optional)
347356
* base64url: Base64Url (Optional)
357+
* unknownDurationFormat: String (Optional)
358+
* unknownDateTimeFormat: String (Optional)
359+
* unknownBytes: String (Optional)
348360
* }
349361
* }
350362
* }

packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/builtin/models/Encoded.java

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,24 @@ public final class Encoded implements JsonSerializable<Encoded> {
6868
@Generated
6969
private Base64Url base64url;
7070

71+
/*
72+
* The unknownDurationFormat property.
73+
*/
74+
@Generated
75+
private String unknownDurationFormat;
76+
77+
/*
78+
* The unknownDateTimeFormat property.
79+
*/
80+
@Generated
81+
private String unknownDateTimeFormat;
82+
83+
/*
84+
* The unknownBytes property.
85+
*/
86+
@Generated
87+
private String unknownBytes;
88+
7189
/**
7290
* Creates an instance of Encoded class.
7391
*/
@@ -264,6 +282,72 @@ public Encoded setBase64url(byte[] base64url) {
264282
return this;
265283
}
266284

285+
/**
286+
* Get the unknownDurationFormat property: The unknownDurationFormat property.
287+
*
288+
* @return the unknownDurationFormat value.
289+
*/
290+
@Generated
291+
public String getUnknownDurationFormat() {
292+
return this.unknownDurationFormat;
293+
}
294+
295+
/**
296+
* Set the unknownDurationFormat property: The unknownDurationFormat property.
297+
*
298+
* @param unknownDurationFormat the unknownDurationFormat value to set.
299+
* @return the Encoded object itself.
300+
*/
301+
@Generated
302+
public Encoded setUnknownDurationFormat(String unknownDurationFormat) {
303+
this.unknownDurationFormat = unknownDurationFormat;
304+
return this;
305+
}
306+
307+
/**
308+
* Get the unknownDateTimeFormat property: The unknownDateTimeFormat property.
309+
*
310+
* @return the unknownDateTimeFormat value.
311+
*/
312+
@Generated
313+
public String getUnknownDateTimeFormat() {
314+
return this.unknownDateTimeFormat;
315+
}
316+
317+
/**
318+
* Set the unknownDateTimeFormat property: The unknownDateTimeFormat property.
319+
*
320+
* @param unknownDateTimeFormat the unknownDateTimeFormat value to set.
321+
* @return the Encoded object itself.
322+
*/
323+
@Generated
324+
public Encoded setUnknownDateTimeFormat(String unknownDateTimeFormat) {
325+
this.unknownDateTimeFormat = unknownDateTimeFormat;
326+
return this;
327+
}
328+
329+
/**
330+
* Get the unknownBytes property: The unknownBytes property.
331+
*
332+
* @return the unknownBytes value.
333+
*/
334+
@Generated
335+
public String getUnknownBytes() {
336+
return this.unknownBytes;
337+
}
338+
339+
/**
340+
* Set the unknownBytes property: The unknownBytes property.
341+
*
342+
* @param unknownBytes the unknownBytes value to set.
343+
* @return the Encoded object itself.
344+
*/
345+
@Generated
346+
public Encoded setUnknownBytes(String unknownBytes) {
347+
this.unknownBytes = unknownBytes;
348+
return this;
349+
}
350+
267351
/**
268352
* {@inheritDoc}
269353
*/
@@ -279,6 +363,9 @@ public JsonWriter toJson(JsonWriter jsonWriter) throws IOException {
279363
jsonWriter.writeNumberField("unixTimestamp", this.unixTimestamp);
280364
jsonWriter.writeBinaryField("base64", this.base64);
281365
jsonWriter.writeStringField("base64url", Objects.toString(this.base64url, null));
366+
jsonWriter.writeStringField("unknownDurationFormat", this.unknownDurationFormat);
367+
jsonWriter.writeStringField("unknownDateTimeFormat", this.unknownDateTimeFormat);
368+
jsonWriter.writeStringField("unknownBytes", this.unknownBytes);
282369
return jsonWriter.writeEndObject();
283370
}
284371

@@ -315,6 +402,12 @@ public static Encoded fromJson(JsonReader jsonReader) throws IOException {
315402
} else if ("base64url".equals(fieldName)) {
316403
deserializedEncoded.base64url
317404
= reader.getNullable(nonNullReader -> new Base64Url(nonNullReader.getString()));
405+
} else if ("unknownDurationFormat".equals(fieldName)) {
406+
deserializedEncoded.unknownDurationFormat = reader.getString();
407+
} else if ("unknownDateTimeFormat".equals(fieldName)) {
408+
deserializedEncoded.unknownDateTimeFormat = reader.getString();
409+
} else if ("unknownBytes".equals(fieldName)) {
410+
deserializedEncoded.unknownBytes = reader.getString();
318411
} else {
319412
reader.skipChildren();
320413
}

packages/http-client-java/generator/http-client-generator-test/tsp/builtin.tsp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ model Encoded {
5252

5353
@encode(BytesKnownEncoding.base64url)
5454
base64url?: bytes;
55+
56+
@encode("unknown-duration")
57+
unknownDurationFormat?: duration;
58+
59+
@encode("unknown-datetime")
60+
unknownDateTimeFormat?: utcDateTime;
61+
62+
@encode("unknown-bytes")
63+
unknownBytes?: bytes;
5564
}
5665

5766
model Request {

0 commit comments

Comments
 (0)