@azure-tools/typespec-ts 0.46.1-alpha.20251201.1 → 0.47.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +15 -3
- package/LICENSE +21 -0
- package/dist/src/framework/hooks/sdkTypes.d.ts +7 -1
- package/dist/src/framework/hooks/sdkTypes.d.ts.map +1 -1
- package/dist/src/framework/hooks/sdkTypes.js +59 -1
- package/dist/src/framework/hooks/sdkTypes.js.map +1 -1
- package/dist/src/lib.d.ts +19 -1
- package/dist/src/lib.d.ts.map +1 -1
- package/dist/src/lib.js +12 -0
- package/dist/src/lib.js.map +1 -1
- package/dist/src/modular/buildClassicalClient.d.ts.map +1 -1
- package/dist/src/modular/buildClassicalClient.js +84 -6
- package/dist/src/modular/buildClassicalClient.js.map +1 -1
- package/dist/src/modular/emitModels.d.ts.map +1 -1
- package/dist/src/modular/emitModels.js +57 -14
- package/dist/src/modular/emitModels.js.map +1 -1
- package/dist/src/modular/emitSamples.d.ts.map +1 -1
- package/dist/src/modular/emitSamples.js +52 -25
- package/dist/src/modular/emitSamples.js.map +1 -1
- package/dist/src/modular/helpers/operationHelpers.d.ts +5 -4
- package/dist/src/modular/helpers/operationHelpers.d.ts.map +1 -1
- package/dist/src/modular/helpers/operationHelpers.js +113 -23
- package/dist/src/modular/helpers/operationHelpers.js.map +1 -1
- package/dist/src/modular/helpers/typeHelpers.d.ts +3 -1
- package/dist/src/modular/helpers/typeHelpers.d.ts.map +1 -1
- package/dist/src/modular/helpers/typeHelpers.js +7 -3
- package/dist/src/modular/helpers/typeHelpers.js.map +1 -1
- package/dist/src/modular/serialization/buildDeserializerFunction.d.ts +4 -2
- package/dist/src/modular/serialization/buildDeserializerFunction.d.ts.map +1 -1
- package/dist/src/modular/serialization/buildDeserializerFunction.js +60 -17
- package/dist/src/modular/serialization/buildDeserializerFunction.js.map +1 -1
- package/dist/src/modular/serialization/buildSerializerFunction.d.ts +4 -2
- package/dist/src/modular/serialization/buildSerializerFunction.d.ts.map +1 -1
- package/dist/src/modular/serialization/buildSerializerFunction.js +61 -19
- package/dist/src/modular/serialization/buildSerializerFunction.js.map +1 -1
- package/dist/src/modular/serialization/serializeUtils.d.ts +11 -0
- package/dist/src/modular/serialization/serializeUtils.d.ts.map +1 -1
- package/dist/src/modular/serialization/serializeUtils.js +15 -0
- package/dist/src/modular/serialization/serializeUtils.js.map +1 -1
- package/dist/src/modular/static-helpers-metadata.d.ts +5 -0
- package/dist/src/modular/static-helpers-metadata.d.ts.map +1 -1
- package/dist/src/modular/static-helpers-metadata.js +5 -0
- package/dist/src/modular/static-helpers-metadata.js.map +1 -1
- package/dist/src/utils/namespaceUtils.d.ts.map +1 -1
- package/dist/src/utils/namespaceUtils.js +19 -4
- package/dist/src/utils/namespaceUtils.js.map +1 -1
- package/dist/src/utils/operationUtil.d.ts +1 -0
- package/dist/src/utils/operationUtil.d.ts.map +1 -1
- package/dist/src/utils/operationUtil.js +26 -0
- package/dist/src/utils/operationUtil.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +7 -11
- package/src/framework/hooks/sdkTypes.ts +102 -2
- package/src/lib.ts +12 -0
- package/src/modular/buildClassicalClient.ts +116 -6
- package/src/modular/emitModels.ts +72 -19
- package/src/modular/emitSamples.ts +85 -20
- package/src/modular/helpers/operationHelpers.ts +151 -41
- package/src/modular/helpers/typeHelpers.ts +19 -3
- package/src/modular/serialization/buildDeserializerFunction.ts +87 -35
- package/src/modular/serialization/buildSerializerFunction.ts +86 -36
- package/src/modular/serialization/serializeUtils.ts +37 -0
- package/src/modular/static-helpers-metadata.ts +5 -0
- package/src/utils/namespaceUtils.ts +19 -3
- package/src/utils/operationUtil.ts +35 -0
- package/static/static-helpers/serialization/check-prop-undefined.ts +17 -0
|
@@ -34,11 +34,13 @@ import {
|
|
|
34
34
|
} from "../utils/credentialUtils.js";
|
|
35
35
|
import {
|
|
36
36
|
getMethodHierarchiesMap,
|
|
37
|
+
isTenantLevelOperation,
|
|
37
38
|
ServiceOperation
|
|
38
39
|
} from "../utils/operationUtil.js";
|
|
39
40
|
import { getSubscriptionId } from "../transform/transfromRLCOptions.js";
|
|
40
41
|
import { getClientParametersDeclaration } from "./helpers/clientHelpers.js";
|
|
41
42
|
import { getOperationFunction } from "./helpers/operationHelpers.js";
|
|
43
|
+
import { ModelOverrideOptions } from "./serialization/serializeUtils.js";
|
|
42
44
|
|
|
43
45
|
/**
|
|
44
46
|
* Interfaces for samples generations
|
|
@@ -272,6 +274,7 @@ function buildParameterValueMap(example: SdkHttpOperationExample) {
|
|
|
272
274
|
}
|
|
273
275
|
|
|
274
276
|
function prepareExampleValue(
|
|
277
|
+
context: SdkContext,
|
|
275
278
|
name: string,
|
|
276
279
|
value: SdkExampleValue | string,
|
|
277
280
|
isOptional?: boolean,
|
|
@@ -279,7 +282,8 @@ function prepareExampleValue(
|
|
|
279
282
|
): ExampleValue {
|
|
280
283
|
return {
|
|
281
284
|
name: normalizeName(name, NameType.Parameter),
|
|
282
|
-
value:
|
|
285
|
+
value:
|
|
286
|
+
typeof value === "string" ? value : getParameterValue(context, value),
|
|
283
287
|
isOptional: Boolean(isOptional),
|
|
284
288
|
onClient: Boolean(onClient)
|
|
285
289
|
};
|
|
@@ -328,6 +332,7 @@ function prepareExampleParameters(
|
|
|
328
332
|
}
|
|
329
333
|
|
|
330
334
|
let subscriptionIdValue = `"00000000-0000-0000-0000-000000000000"`;
|
|
335
|
+
let isSubscriptionIdAdded = false;
|
|
331
336
|
// required parameters
|
|
332
337
|
for (const param of method.operation.parameters) {
|
|
333
338
|
if (
|
|
@@ -339,6 +344,26 @@ function prepareExampleParameters(
|
|
|
339
344
|
}
|
|
340
345
|
|
|
341
346
|
const exampleValue = parameterMap[param.serializedName];
|
|
347
|
+
|
|
348
|
+
// Handle subscriptionId parameter separately for ARM clients
|
|
349
|
+
// Add it as long as it's the parameter of the method, even no example provided
|
|
350
|
+
if (param.name.toLowerCase() === "subscriptionid" && dpgContext.arm) {
|
|
351
|
+
isSubscriptionIdAdded = true;
|
|
352
|
+
if (exampleValue && exampleValue.value) {
|
|
353
|
+
subscriptionIdValue = getParameterValue(dpgContext, exampleValue.value);
|
|
354
|
+
}
|
|
355
|
+
result.push(
|
|
356
|
+
prepareExampleValue(
|
|
357
|
+
dpgContext,
|
|
358
|
+
param.name,
|
|
359
|
+
subscriptionIdValue,
|
|
360
|
+
param.optional,
|
|
361
|
+
param.onClient
|
|
362
|
+
)
|
|
363
|
+
);
|
|
364
|
+
continue;
|
|
365
|
+
}
|
|
366
|
+
|
|
342
367
|
if (!exampleValue || !exampleValue.value) {
|
|
343
368
|
// report diagnostic if required parameter is missing
|
|
344
369
|
reportDiagnostic(dpgContext.program, {
|
|
@@ -352,16 +377,9 @@ function prepareExampleParameters(
|
|
|
352
377
|
continue;
|
|
353
378
|
}
|
|
354
379
|
|
|
355
|
-
if (
|
|
356
|
-
param.name.toLowerCase() === "subscriptionid" &&
|
|
357
|
-
dpgContext.arm &&
|
|
358
|
-
exampleValue
|
|
359
|
-
) {
|
|
360
|
-
subscriptionIdValue = getParameterValue(exampleValue.value);
|
|
361
|
-
continue;
|
|
362
|
-
}
|
|
363
380
|
result.push(
|
|
364
381
|
prepareExampleValue(
|
|
382
|
+
dpgContext,
|
|
365
383
|
exampleValue.parameter.name,
|
|
366
384
|
exampleValue.value,
|
|
367
385
|
param.optional,
|
|
@@ -369,12 +387,26 @@ function prepareExampleParameters(
|
|
|
369
387
|
)
|
|
370
388
|
);
|
|
371
389
|
}
|
|
372
|
-
|
|
373
|
-
|
|
390
|
+
|
|
391
|
+
// If client-level subscriptionId is needed on the client for this method, then add it
|
|
392
|
+
// For example, Operations_List
|
|
393
|
+
if (
|
|
394
|
+
dpgContext.arm &&
|
|
395
|
+
getSubscriptionId(dpgContext) &&
|
|
396
|
+
!isSubscriptionIdAdded &&
|
|
397
|
+
!isTenantLevelOperation(method, topLevelClient)
|
|
398
|
+
) {
|
|
374
399
|
result.push(
|
|
375
|
-
prepareExampleValue(
|
|
400
|
+
prepareExampleValue(
|
|
401
|
+
dpgContext,
|
|
402
|
+
"subscriptionId",
|
|
403
|
+
subscriptionIdValue,
|
|
404
|
+
false,
|
|
405
|
+
true
|
|
406
|
+
)
|
|
376
407
|
);
|
|
377
408
|
}
|
|
409
|
+
|
|
378
410
|
// required/optional body parameters
|
|
379
411
|
const bodyParam = method.operation.bodyParam;
|
|
380
412
|
const bodySerializeName = bodyParam?.serializedName;
|
|
@@ -392,6 +424,7 @@ function prepareExampleParameters(
|
|
|
392
424
|
}
|
|
393
425
|
result.push(
|
|
394
426
|
prepareExampleValue(
|
|
427
|
+
dpgContext,
|
|
395
428
|
prop.name,
|
|
396
429
|
propExample,
|
|
397
430
|
prop.optional,
|
|
@@ -402,6 +435,7 @@ function prepareExampleParameters(
|
|
|
402
435
|
} else {
|
|
403
436
|
result.push(
|
|
404
437
|
prepareExampleValue(
|
|
438
|
+
dpgContext,
|
|
405
439
|
bodyParam.name,
|
|
406
440
|
bodyExample.value,
|
|
407
441
|
bodyParam.optional,
|
|
@@ -422,6 +456,7 @@ function prepareExampleParameters(
|
|
|
422
456
|
.forEach((param) => {
|
|
423
457
|
result.push(
|
|
424
458
|
prepareExampleValue(
|
|
459
|
+
dpgContext,
|
|
425
460
|
param.parameter.name,
|
|
426
461
|
param.value,
|
|
427
462
|
true,
|
|
@@ -471,7 +506,13 @@ function getCredentialExampleValue(
|
|
|
471
506
|
return undefined;
|
|
472
507
|
}
|
|
473
508
|
|
|
474
|
-
function getParameterValue(
|
|
509
|
+
function getParameterValue(
|
|
510
|
+
context: SdkContext,
|
|
511
|
+
value: SdkExampleValue,
|
|
512
|
+
options?: {
|
|
513
|
+
overrides?: ModelOverrideOptions;
|
|
514
|
+
}
|
|
515
|
+
): string {
|
|
475
516
|
let retValue = `{} as any`;
|
|
476
517
|
switch (value.kind) {
|
|
477
518
|
case "string": {
|
|
@@ -509,21 +550,43 @@ function getParameterValue(value: SdkExampleValue): string {
|
|
|
509
550
|
break;
|
|
510
551
|
case "dict":
|
|
511
552
|
case "model": {
|
|
512
|
-
const mapper = buildPropertyNameMapper(
|
|
553
|
+
const mapper = buildPropertyNameMapper(
|
|
554
|
+
context,
|
|
555
|
+
value.type,
|
|
556
|
+
options?.overrides
|
|
557
|
+
);
|
|
513
558
|
const values = [];
|
|
514
559
|
const additionalPropertiesValue =
|
|
515
560
|
value.kind === "model" ? (value.additionalPropertiesValue ?? {}) : {};
|
|
516
561
|
for (const propName in {
|
|
517
562
|
...value.value
|
|
518
563
|
}) {
|
|
564
|
+
let property;
|
|
565
|
+
if (value.type.kind === "model") {
|
|
566
|
+
property = value.type.properties.find((p) => p.name === propName);
|
|
567
|
+
}
|
|
519
568
|
const propValue = value.value[propName];
|
|
520
569
|
if (propValue === undefined || propValue === null) {
|
|
521
570
|
continue;
|
|
522
571
|
}
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
572
|
+
let propRetValue;
|
|
573
|
+
|
|
574
|
+
if (property?.flatten && property.type.kind === "model") {
|
|
575
|
+
const paramValue = getParameterValue(context, propValue, {
|
|
576
|
+
overrides: {
|
|
577
|
+
propertyRenames:
|
|
578
|
+
useContext("sdkTypes").flattenProperties.get(property)
|
|
579
|
+
?.conflictMap
|
|
580
|
+
}
|
|
581
|
+
});
|
|
582
|
+
propRetValue =
|
|
583
|
+
paramValue.length > 2 ? paramValue.slice(1, -1) : undefined;
|
|
584
|
+
} else {
|
|
585
|
+
propRetValue =
|
|
586
|
+
`"${mapper.get(propName) ?? propName}": ` +
|
|
587
|
+
getParameterValue(context, propValue);
|
|
588
|
+
}
|
|
589
|
+
if (propRetValue) values.push(propRetValue);
|
|
527
590
|
}
|
|
528
591
|
const additionalBags = [];
|
|
529
592
|
for (const propName in {
|
|
@@ -535,7 +598,7 @@ function getParameterValue(value: SdkExampleValue): string {
|
|
|
535
598
|
}
|
|
536
599
|
const propRetValue =
|
|
537
600
|
`"${mapper.get(propName) ?? propName}": ` +
|
|
538
|
-
getParameterValue(propValue);
|
|
601
|
+
getParameterValue(context, propValue);
|
|
539
602
|
additionalBags.push(propRetValue);
|
|
540
603
|
}
|
|
541
604
|
if (additionalBags.length > 0) {
|
|
@@ -551,7 +614,9 @@ function getParameterValue(value: SdkExampleValue): string {
|
|
|
551
614
|
break;
|
|
552
615
|
}
|
|
553
616
|
case "array": {
|
|
554
|
-
const valuesArr = value.value.map(
|
|
617
|
+
const valuesArr = value.value.map((item) =>
|
|
618
|
+
getParameterValue(context, item)
|
|
619
|
+
);
|
|
555
620
|
retValue = `[${valuesArr.join(", ")}]`;
|
|
556
621
|
break;
|
|
557
622
|
}
|
|
@@ -24,8 +24,10 @@ import {
|
|
|
24
24
|
ServiceOperation
|
|
25
25
|
} from "../../utils/operationUtil.js";
|
|
26
26
|
import {
|
|
27
|
+
getPropertyWithOverrides,
|
|
27
28
|
isNormalUnion,
|
|
28
|
-
isSpecialHandledUnion
|
|
29
|
+
isSpecialHandledUnion,
|
|
30
|
+
ModelOverrideOptions
|
|
29
31
|
} from "../serialization/serializeUtils.js";
|
|
30
32
|
import {
|
|
31
33
|
getDocsFromDescription,
|
|
@@ -33,8 +35,14 @@ import {
|
|
|
33
35
|
} from "./docsHelpers.js";
|
|
34
36
|
import { AzurePollingDependencies } from "../external-dependencies.js";
|
|
35
37
|
import { NameType, normalizeName } from "@azure-tools/rlc-common";
|
|
36
|
-
import {
|
|
37
|
-
|
|
38
|
+
import {
|
|
39
|
+
buildModelDeserializer,
|
|
40
|
+
buildPropertyDeserializer
|
|
41
|
+
} from "../serialization/buildDeserializerFunction.js";
|
|
42
|
+
import {
|
|
43
|
+
buildModelSerializer,
|
|
44
|
+
buildPropertySerializer
|
|
45
|
+
} from "../serialization/buildSerializerFunction.js";
|
|
38
46
|
import { refkey } from "../../framework/refkey.js";
|
|
39
47
|
import { reportDiagnostic } from "../../lib.js";
|
|
40
48
|
import { resolveReference } from "../../framework/reference.js";
|
|
@@ -64,6 +72,7 @@ import {
|
|
|
64
72
|
SdkType
|
|
65
73
|
} from "@azure-tools/typespec-client-generator-core";
|
|
66
74
|
import { isMetadata } from "@typespec/http";
|
|
75
|
+
import { useContext } from "../../contextManager.js";
|
|
67
76
|
|
|
68
77
|
export function getSendPrivateFunction(
|
|
69
78
|
dpgContext: SdkContext,
|
|
@@ -221,8 +230,10 @@ export function getDeserializePrivateFunction(
|
|
|
221
230
|
const deserializeFunctionName = buildModelDeserializer(
|
|
222
231
|
context,
|
|
223
232
|
deserializedType,
|
|
224
|
-
|
|
225
|
-
|
|
233
|
+
{
|
|
234
|
+
nameOnly: true,
|
|
235
|
+
skipDiscriminatedUnionSuffix: false
|
|
236
|
+
}
|
|
226
237
|
);
|
|
227
238
|
if (deserializeFunctionName) {
|
|
228
239
|
statements.push(`return ${deserializeFunctionName}(${deserializedRoot})`);
|
|
@@ -277,8 +288,10 @@ function getExceptionDetails(
|
|
|
277
288
|
const deserializeFunctionName = buildModelDeserializer(
|
|
278
289
|
context,
|
|
279
290
|
exception.type,
|
|
280
|
-
|
|
281
|
-
|
|
291
|
+
{
|
|
292
|
+
nameOnly: true,
|
|
293
|
+
skipDiscriminatedUnionSuffix: false
|
|
294
|
+
}
|
|
282
295
|
);
|
|
283
296
|
if (
|
|
284
297
|
!deserializeFunctionName ||
|
|
@@ -809,8 +822,10 @@ function buildBodyParameter(
|
|
|
809
822
|
const serializerFunctionName = buildModelSerializer(
|
|
810
823
|
context,
|
|
811
824
|
getNullableValidType(bodyParameter.type),
|
|
812
|
-
|
|
813
|
-
|
|
825
|
+
{
|
|
826
|
+
nameOnly: true,
|
|
827
|
+
skipDiscriminatedUnionSuffix: false
|
|
828
|
+
}
|
|
814
829
|
);
|
|
815
830
|
|
|
816
831
|
const bodyParamName = normalizeName(
|
|
@@ -1153,11 +1168,48 @@ function getNullableCheck(name: string, type: SdkType) {
|
|
|
1153
1168
|
return `${name} === null ? null :`;
|
|
1154
1169
|
}
|
|
1155
1170
|
|
|
1156
|
-
|
|
1171
|
+
function getSerializationExpressionForFlatten(
|
|
1157
1172
|
context: SdkContext,
|
|
1158
1173
|
property: SdkModelPropertyType,
|
|
1159
1174
|
propertyPath: string
|
|
1160
1175
|
): string {
|
|
1176
|
+
const serializeFunctionName = buildPropertySerializer(context, property, {
|
|
1177
|
+
nameOnly: true,
|
|
1178
|
+
skipDiscriminatedUnionSuffix: false
|
|
1179
|
+
});
|
|
1180
|
+
if (!serializeFunctionName) {
|
|
1181
|
+
return property.optional ? `undefined` : `{}`;
|
|
1182
|
+
}
|
|
1183
|
+
const validProps = getAllProperties(
|
|
1184
|
+
context,
|
|
1185
|
+
property.type,
|
|
1186
|
+
getAllAncestors(property.type)
|
|
1187
|
+
).filter(
|
|
1188
|
+
(p) =>
|
|
1189
|
+
p.kind === "property" &&
|
|
1190
|
+
!isReadOnly(p) &&
|
|
1191
|
+
!isMetadata(context.program, p.__raw!)
|
|
1192
|
+
);
|
|
1193
|
+
if (validProps.length === 0) {
|
|
1194
|
+
return `undefined`;
|
|
1195
|
+
}
|
|
1196
|
+
const optionalPrefix = property.optional
|
|
1197
|
+
? `${resolveReference(SerializationHelpers.areAllPropsUndefined)}(${propertyPath}, [${validProps
|
|
1198
|
+
.map((p) => `"${p.name}"`)
|
|
1199
|
+
.join(", ")}]) ? undefined : `
|
|
1200
|
+
: "";
|
|
1201
|
+
return `${optionalPrefix}${serializeFunctionName}(${propertyPath})`;
|
|
1202
|
+
}
|
|
1203
|
+
|
|
1204
|
+
export function getSerializationExpression(
|
|
1205
|
+
context: SdkContext,
|
|
1206
|
+
property: SdkModelPropertyType,
|
|
1207
|
+
propertyPath: string,
|
|
1208
|
+
enableFlatten: boolean = true
|
|
1209
|
+
): string {
|
|
1210
|
+
if (property.flatten && enableFlatten) {
|
|
1211
|
+
return getSerializationExpressionForFlatten(context, property, "item");
|
|
1212
|
+
}
|
|
1161
1213
|
const dot = propertyPath.endsWith("?") ? "." : "";
|
|
1162
1214
|
const propertyPathWithDot = `${propertyPath ? `${propertyPath}${dot}` : `${dot}`}`;
|
|
1163
1215
|
const nullOrUndefinedPrefix = getPropertySerializationPrefix(
|
|
@@ -1174,8 +1226,10 @@ export function getSerializationExpression(
|
|
|
1174
1226
|
const serializeFunctionName = buildModelSerializer(
|
|
1175
1227
|
context,
|
|
1176
1228
|
getNullableValidType(property.type),
|
|
1177
|
-
|
|
1178
|
-
|
|
1229
|
+
{
|
|
1230
|
+
nameOnly: true,
|
|
1231
|
+
skipDiscriminatedUnionSuffix: false
|
|
1232
|
+
}
|
|
1179
1233
|
);
|
|
1180
1234
|
if (serializeFunctionName) {
|
|
1181
1235
|
return `${nullOrUndefinedPrefix}${serializeFunctionName}(${propertyFullName})`;
|
|
@@ -1196,7 +1250,9 @@ export function getSerializationExpression(
|
|
|
1196
1250
|
export function getRequestModelProperties(
|
|
1197
1251
|
context: SdkContext,
|
|
1198
1252
|
modelPropertyType: SdkModelType & { optional?: boolean },
|
|
1199
|
-
propertyPath: string = "body"
|
|
1253
|
+
propertyPath: string = "body",
|
|
1254
|
+
overrides?: ModelOverrideOptions,
|
|
1255
|
+
enableFlatten: boolean = true
|
|
1200
1256
|
): Array<[string, string]> {
|
|
1201
1257
|
const props: [string, string][] = [];
|
|
1202
1258
|
const allParents = getAllAncestors(modelPropertyType);
|
|
@@ -1205,16 +1261,17 @@ export function getRequestModelProperties(
|
|
|
1205
1261
|
if (properties.length <= 0) {
|
|
1206
1262
|
return [];
|
|
1207
1263
|
}
|
|
1208
|
-
for (const
|
|
1209
|
-
if (
|
|
1264
|
+
for (const prop of properties) {
|
|
1265
|
+
if (prop.kind === "property" && isReadOnly(prop)) {
|
|
1210
1266
|
continue;
|
|
1211
1267
|
}
|
|
1212
|
-
if (isMetadata(context.program,
|
|
1268
|
+
if (isMetadata(context.program, prop.__raw!)) {
|
|
1213
1269
|
continue;
|
|
1214
1270
|
}
|
|
1271
|
+
const property = getPropertyWithOverrides(prop, overrides);
|
|
1215
1272
|
props.push([
|
|
1216
1273
|
getPropertySerializedName(property)!,
|
|
1217
|
-
getSerializationExpression(context, property, propertyPath)
|
|
1274
|
+
getSerializationExpression(context, property, propertyPath, enableFlatten)
|
|
1218
1275
|
]);
|
|
1219
1276
|
}
|
|
1220
1277
|
|
|
@@ -1229,12 +1286,16 @@ export function getRequestModelProperties(
|
|
|
1229
1286
|
export function getRequestModelMapping(
|
|
1230
1287
|
context: SdkContext,
|
|
1231
1288
|
modelPropertyType: SdkModelType & { optional?: boolean },
|
|
1232
|
-
propertyPath: string = "body"
|
|
1289
|
+
propertyPath: string = "body",
|
|
1290
|
+
overrides?: ModelOverrideOptions,
|
|
1291
|
+
enableFlatten: boolean = true
|
|
1233
1292
|
): string[] {
|
|
1234
1293
|
return getRequestModelProperties(
|
|
1235
1294
|
context,
|
|
1236
1295
|
modelPropertyType,
|
|
1237
|
-
propertyPath
|
|
1296
|
+
propertyPath,
|
|
1297
|
+
overrides,
|
|
1298
|
+
enableFlatten
|
|
1238
1299
|
).map(([name, value]) => `"${name}": ${value}`);
|
|
1239
1300
|
}
|
|
1240
1301
|
|
|
@@ -1255,16 +1316,19 @@ function getPropertySerializedName(
|
|
|
1255
1316
|
export function getResponseMapping(
|
|
1256
1317
|
context: SdkContext,
|
|
1257
1318
|
type: SdkType,
|
|
1258
|
-
propertyPath: string = "result.body"
|
|
1319
|
+
propertyPath: string = "result.body",
|
|
1320
|
+
overrides?: ModelOverrideOptions,
|
|
1321
|
+
enableFlatten: boolean = true
|
|
1259
1322
|
) {
|
|
1260
1323
|
const allParents = type.kind === "model" ? getAllAncestors(type) : [];
|
|
1261
1324
|
const properties =
|
|
1262
1325
|
type.kind === "model" ? getAllProperties(context, type, allParents) : [];
|
|
1263
1326
|
const props: string[] = [];
|
|
1264
|
-
for (const
|
|
1265
|
-
if (isMetadata(context.program,
|
|
1327
|
+
for (const prop of properties) {
|
|
1328
|
+
if (isMetadata(context.program, prop.__raw!)) {
|
|
1266
1329
|
continue;
|
|
1267
1330
|
}
|
|
1331
|
+
const property = getPropertyWithOverrides(prop, overrides);
|
|
1268
1332
|
const dot = propertyPath.endsWith("?") ? "." : "";
|
|
1269
1333
|
const serializedName = getPropertySerializedName(property);
|
|
1270
1334
|
const restValue = `${
|
|
@@ -1275,17 +1339,29 @@ export function getResponseMapping(
|
|
|
1275
1339
|
property.optional || isTypeNullable(property.type)
|
|
1276
1340
|
? `!${restValue}? ${restValue}: `
|
|
1277
1341
|
: "";
|
|
1278
|
-
const
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1342
|
+
const flattenContext =
|
|
1343
|
+
useContext("sdkTypes").flattenProperties.get(property);
|
|
1344
|
+
const isSupportedFlatten = flattenContext && enableFlatten;
|
|
1345
|
+
const deserializeFunctionName = isSupportedFlatten
|
|
1346
|
+
? buildPropertyDeserializer(context, property, {
|
|
1347
|
+
nameOnly: true,
|
|
1348
|
+
skipDiscriminatedUnionSuffix: false
|
|
1349
|
+
})
|
|
1350
|
+
: buildModelDeserializer(context, getNullableValidType(property.type), {
|
|
1351
|
+
nameOnly: true,
|
|
1352
|
+
skipDiscriminatedUnionSuffix: false
|
|
1353
|
+
});
|
|
1284
1354
|
const propertyName = normalizeModelPropertyName(context, property);
|
|
1285
1355
|
if (deserializeFunctionName) {
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1356
|
+
if (isSupportedFlatten) {
|
|
1357
|
+
props.push(
|
|
1358
|
+
`...${nullOrUndefinedPrefix}${deserializeFunctionName}(${restValue})`
|
|
1359
|
+
);
|
|
1360
|
+
} else {
|
|
1361
|
+
props.push(
|
|
1362
|
+
`${propertyName}: ${nullOrUndefinedPrefix}${deserializeFunctionName}(${restValue})`
|
|
1363
|
+
);
|
|
1364
|
+
}
|
|
1289
1365
|
} else if (isAzureCoreErrorType(context.program, property.type.__raw)) {
|
|
1290
1366
|
props.push(`${propertyName}: ${nullOrUndefinedPrefix}${restValue}`);
|
|
1291
1367
|
} else {
|
|
@@ -1343,8 +1419,10 @@ export function serializeRequestValue(
|
|
|
1343
1419
|
const serializeFunctionName = buildModelSerializer(
|
|
1344
1420
|
context,
|
|
1345
1421
|
getNullableValidType(type.valueType),
|
|
1346
|
-
|
|
1347
|
-
|
|
1422
|
+
{
|
|
1423
|
+
nameOnly: true,
|
|
1424
|
+
skipDiscriminatedUnionSuffix: false
|
|
1425
|
+
}
|
|
1348
1426
|
);
|
|
1349
1427
|
if (serializeFunctionName) {
|
|
1350
1428
|
return `${prefix}.map((p: any) => { return ${elementNullOrUndefinedPrefix}${serializeFunctionName}(p)})`;
|
|
@@ -1451,8 +1529,10 @@ export function deserializeResponseValue(
|
|
|
1451
1529
|
? buildModelDeserializer(
|
|
1452
1530
|
context,
|
|
1453
1531
|
getNullableValidType(type.valueType),
|
|
1454
|
-
|
|
1455
|
-
|
|
1532
|
+
{
|
|
1533
|
+
nameOnly: true,
|
|
1534
|
+
skipDiscriminatedUnionSuffix: false
|
|
1535
|
+
}
|
|
1456
1536
|
)
|
|
1457
1537
|
: undefined;
|
|
1458
1538
|
if (deserializeFunctionName) {
|
|
@@ -1468,6 +1548,38 @@ export function deserializeResponseValue(
|
|
|
1468
1548
|
return restValue;
|
|
1469
1549
|
}
|
|
1470
1550
|
}
|
|
1551
|
+
case "dict": {
|
|
1552
|
+
const prefix = nullOrUndefinedPrefix + restValue;
|
|
1553
|
+
let elementNullOrUndefinedPrefix = "";
|
|
1554
|
+
if (
|
|
1555
|
+
type.valueType &&
|
|
1556
|
+
(isTypeNullable(type.valueType) || getOptionalForType(type.valueType))
|
|
1557
|
+
) {
|
|
1558
|
+
elementNullOrUndefinedPrefix = "!p ? p :";
|
|
1559
|
+
}
|
|
1560
|
+
const deserializeFunctionName = type.valueType
|
|
1561
|
+
? buildModelDeserializer(
|
|
1562
|
+
context,
|
|
1563
|
+
getNullableValidType(type.valueType),
|
|
1564
|
+
{
|
|
1565
|
+
nameOnly: true,
|
|
1566
|
+
skipDiscriminatedUnionSuffix: false
|
|
1567
|
+
}
|
|
1568
|
+
)
|
|
1569
|
+
: undefined;
|
|
1570
|
+
if (deserializeFunctionName) {
|
|
1571
|
+
return `Object.fromEntries(Object.entries(${prefix}).map(([k, p]: [string, any]) => [k, ${elementNullOrUndefinedPrefix}${deserializeFunctionName}(p)]))`;
|
|
1572
|
+
} else if (
|
|
1573
|
+
type.valueType &&
|
|
1574
|
+
isAzureCoreErrorType(context.program, type.valueType.__raw)
|
|
1575
|
+
) {
|
|
1576
|
+
return `Object.fromEntries(Object.entries(${prefix}).map(([k, p]: [string, any]) => [k, ${elementNullOrUndefinedPrefix}p]))`;
|
|
1577
|
+
} else if (type.valueType) {
|
|
1578
|
+
return `Object.fromEntries(Object.entries(${prefix}).map(([k, p]: [string, any]) => [k, ${elementNullOrUndefinedPrefix}${deserializeResponseValue(context, type.valueType, "p", getEncodeForType(type.valueType))}]))`;
|
|
1579
|
+
} else {
|
|
1580
|
+
return restValue;
|
|
1581
|
+
}
|
|
1582
|
+
}
|
|
1471
1583
|
case "bytes":
|
|
1472
1584
|
if (format !== "binary" && format !== "bytes") {
|
|
1473
1585
|
return `typeof ${restValue} === 'string'
|
|
@@ -1480,12 +1592,10 @@ export function deserializeResponseValue(
|
|
|
1480
1592
|
return `${restValue}`;
|
|
1481
1593
|
} else if (isSpecialHandledUnion(type)) {
|
|
1482
1594
|
const deserializeFunctionName = type
|
|
1483
|
-
? buildModelDeserializer(
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
true
|
|
1488
|
-
)
|
|
1595
|
+
? buildModelDeserializer(context, getNullableValidType(type), {
|
|
1596
|
+
nameOnly: true,
|
|
1597
|
+
skipDiscriminatedUnionSuffix: false
|
|
1598
|
+
})
|
|
1489
1599
|
: undefined;
|
|
1490
1600
|
if (deserializeFunctionName) {
|
|
1491
1601
|
return `${deserializeFunctionName}(${restValue})`;
|
|
@@ -7,6 +7,12 @@ import {
|
|
|
7
7
|
SdkModelType,
|
|
8
8
|
SdkType
|
|
9
9
|
} from "@azure-tools/typespec-client-generator-core";
|
|
10
|
+
import {
|
|
11
|
+
getPropertyWithOverrides,
|
|
12
|
+
ModelOverrideOptions
|
|
13
|
+
} from "../serialization/serializeUtils.js";
|
|
14
|
+
import { getAllAncestors, getAllProperties } from "./operationHelpers.js";
|
|
15
|
+
import { SdkContext } from "../../utils/interfaces.js";
|
|
10
16
|
|
|
11
17
|
export function getDirectSubtypes(type: SdkModelType) {
|
|
12
18
|
if (!type.discriminatedSubtypes) {
|
|
@@ -93,15 +99,25 @@ export function isCredentialType(
|
|
|
93
99
|
* Builds a property name mapper between the serializedName and the name of the property.
|
|
94
100
|
* Return empty map if the type is not a model.
|
|
95
101
|
*/
|
|
96
|
-
export function buildPropertyNameMapper(
|
|
102
|
+
export function buildPropertyNameMapper(
|
|
103
|
+
context: SdkContext,
|
|
104
|
+
model: SdkType,
|
|
105
|
+
overrides?: ModelOverrideOptions
|
|
106
|
+
) {
|
|
97
107
|
const mapper = new Map<string, string>();
|
|
98
108
|
if (model.kind !== "model") {
|
|
99
109
|
return mapper;
|
|
100
110
|
}
|
|
101
|
-
|
|
102
|
-
|
|
111
|
+
const allProperties = getAllProperties(
|
|
112
|
+
context,
|
|
113
|
+
model,
|
|
114
|
+
getAllAncestors(model)
|
|
115
|
+
);
|
|
116
|
+
for (const p of allProperties) {
|
|
117
|
+
if (p.kind !== "property") {
|
|
103
118
|
continue;
|
|
104
119
|
}
|
|
120
|
+
const prop = getPropertyWithOverrides(p, overrides);
|
|
105
121
|
mapper.set(
|
|
106
122
|
prop.serializationOptions.json?.name || prop.name,
|
|
107
123
|
normalizeName(prop.name, NameType.Property)
|