Skip to content

Commit 0c80e14

Browse files
authored
Normalization in-consistancy for discriminated union during Serializer/DeSerializer (#3652)
* fix * update * add case
1 parent fb81879 commit 0c80e14

2 files changed

Lines changed: 136 additions & 1 deletion

File tree

packages/typespec-ts/src/modular/emitModels.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,7 @@ export function normalizeModelName(
670670
: "";
671671
const internalModelPrefix =
672672
isPagedResultModel(context, type) || type.isGeneratedName ? "_" : "";
673-
return `${internalModelPrefix}${normalizeName(namespacePrefix + type.name + unionSuffix, nameType, true)}`;
673+
return `${internalModelPrefix}${normalizeName(namespacePrefix + type.name, nameType, true)}${unionSuffix}`;
674674
}
675675

676676
function buildModelPolymorphicType(context: SdkContext, type: SdkModelType) {

packages/typespec-ts/test/modularUnit/scenarios/models/serialization/propertyType.md

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,35 @@ model SimpleModel {
4646
propArrayOfRecordOfUnionOptional?: Record<string | boolean | int32>[];
4747
@encodedName("application/json", "prop_encoded")
4848
propEncoded: string;
49+
propNotNormalizeModel: FOO;
50+
propNormalizeModel: FOOBAR;
51+
propRecordOfUnionArrayNotNormalize: Record<NFVIs[]>;
52+
propUnionArrayNotNormalize: NFVIs[];
53+
propRecordOfUnionNotNormalize: Record<NFVIs>;
54+
}
55+
56+
@discriminator("nfviType")
57+
model NFVIs {
58+
name?: string;
59+
nfviType: string;
60+
}
61+
62+
model AzureCoreNFVIDetails extends NFVIs {
63+
location?: string;
64+
nfviType: "AzureCore";
65+
}
66+
67+
model AzureArcK8sClusterNFVIDetails extends NFVIs {
68+
customLocationId?: string;
69+
nfviType: "AzureArcKubernetes";
70+
}
71+
72+
model FOOBAR{
73+
name?: Record<NFVIs[]>;
74+
}
75+
76+
model FOO {
77+
param?: FOOBAR;
4978
}
5079
5180
@route("/serialize")
@@ -105,6 +134,11 @@ export interface SimpleModel {
105134
propArrayOfRecordOfUnion: Record<string, string | boolean | number>[];
106135
propArrayOfRecordOfUnionOptional?: Record<string, string | boolean | number>[];
107136
propEncoded: string;
137+
propNotNormalizeModel: FOO;
138+
propNormalizeModel: Foobar;
139+
propRecordOfUnionArrayNotNormalize: Record<string, NFVIsUnion[]>;
140+
propUnionArrayNotNormalize: NFVIsUnion[];
141+
propRecordOfUnionNotNormalize: Record<string, NFVIsUnion>;
108142
}
109143

110144
export function simpleModelSerializer(item: SimpleModel): any {
@@ -186,6 +220,15 @@ export function simpleModelSerializer(item: SimpleModel): any {
186220
item["propArrayOfRecordOfUnionOptional"],
187221
),
188222
prop_encoded: item["propEncoded"],
223+
propNotNormalizeModel: fooSerializer(item["propNotNormalizeModel"]),
224+
propNormalizeModel: foobarSerializer(item["propNormalizeModel"]),
225+
propRecordOfUnionArrayNotNormalize: nfvIsUnionArrayRecordSerializer(
226+
item["propRecordOfUnionArrayNotNormalize"],
227+
),
228+
propUnionArrayNotNormalize: nfvIsUnionArraySerializer(item["propUnionArrayNotNormalize"]),
229+
propRecordOfUnionNotNormalize: nfvIsUnionRecordSerializer(
230+
item["propRecordOfUnionNotNormalize"],
231+
),
189232
};
190233
}
191234

@@ -382,4 +425,96 @@ export function _simpleModelPropArrayOfRecordOfUnionOptionalSerializer(
382425
): any {
383426
return item;
384427
}
428+
429+
/** model interface FOO */
430+
export interface FOO {
431+
param?: Foobar;
432+
}
433+
434+
export function fooSerializer(item: FOO): any {
435+
return { param: !item["param"] ? item["param"] : foobarSerializer(item["param"]) };
436+
}
437+
438+
/** model interface Foobar */
439+
export interface Foobar {
440+
name?: Record<string, NFVIsUnion[]>;
441+
}
442+
443+
export function foobarSerializer(item: Foobar): any {
444+
return { name: !item["name"] ? item["name"] : nfvIsUnionArrayRecordSerializer(item["name"]) };
445+
}
446+
447+
export function nfvIsUnionArrayRecordSerializer(
448+
item: Record<string, Array<NFVIsUnion>>,
449+
): Record<string, any> {
450+
const result: Record<string, any> = {};
451+
Object.keys(item).map((key) => {
452+
result[key] = !item[key] ? item[key] : nfvIsUnionArraySerializer(item[key]);
453+
});
454+
return result;
455+
}
456+
457+
export function nfvIsUnionArraySerializer(result: Array<NFVIsUnion>): any[] {
458+
return result.map((item) => {
459+
return nfvIsUnionSerializer(item);
460+
});
461+
}
462+
463+
/** model interface NFVIs */
464+
export interface NFVIs {
465+
name?: string;
466+
nfviType: string;
467+
}
468+
469+
export function nfvIsSerializer(item: NFVIs): any {
470+
return { name: item["name"], nfviType: item["nfviType"] };
471+
}
472+
473+
/** Alias for NFVIsUnion */
474+
export type NFVIsUnion = AzureCoreNfviDetails | AzureArcK8SClusterNfviDetails | NFVIs;
475+
476+
export function nfvIsUnionSerializer(item: NFVIsUnion): any {
477+
switch (item.nfviType) {
478+
case "AzureCore":
479+
return azureCoreNfviDetailsSerializer(item as AzureCoreNfviDetails);
480+
481+
case "AzureArcKubernetes":
482+
return azureArcK8SClusterNfviDetailsSerializer(item as AzureArcK8SClusterNfviDetails);
483+
484+
default:
485+
return nfvIsSerializer(item);
486+
}
487+
}
488+
489+
/** model interface AzureCoreNfviDetails */
490+
export interface AzureCoreNfviDetails extends NFVIs {
491+
location?: string;
492+
nfviType: "AzureCore";
493+
}
494+
495+
export function azureCoreNfviDetailsSerializer(item: AzureCoreNfviDetails): any {
496+
return { name: item["name"], nfviType: item["nfviType"], location: item["location"] };
497+
}
498+
499+
/** model interface AzureArcK8SClusterNfviDetails */
500+
export interface AzureArcK8SClusterNfviDetails extends NFVIs {
501+
customLocationId?: string;
502+
nfviType: "AzureArcKubernetes";
503+
}
504+
505+
export function azureArcK8SClusterNfviDetailsSerializer(item: AzureArcK8SClusterNfviDetails): any {
506+
return {
507+
name: item["name"],
508+
nfviType: item["nfviType"],
509+
customLocationId: item["customLocationId"],
510+
};
511+
}
512+
513+
export function nfvIsUnionRecordSerializer(item: Record<string, NFVIs>): Record<string, any> {
514+
const result: Record<string, any> = {};
515+
Object.keys(item).map((key) => {
516+
result[key] = !item[key] ? item[key] : nfvIsUnionSerializer(item[key]);
517+
});
518+
return result;
519+
}
385520
```

0 commit comments

Comments
 (0)