Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ function buildPolymorphicDeserializer(
});

statements.push(`
switch (item.${type.discriminatorProperty.name}) {
switch (item.${normalizeName(type.discriminatorProperty.name, NameType.Property)}) {
${cases.join("\n")}
default:
return item;
Expand Down Expand Up @@ -232,7 +232,7 @@ function buildDiscriminatedUnionDeserializer(
`);
}
output.push(`
switch (item.${type.discriminatorProperty?.name}) {
switch (item.${type.discriminatorProperty ? normalizeName(type.discriminatorProperty.name, NameType.Property) : "unknown"}) {
${cases.join("\n")}
default:
return ${baseDeserializerName}(item);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ function buildPolymorphicSerializer(
});

statements.push(`
switch (item.${type.discriminatorProperty.name}) {
switch (item.${normalizeName(type.discriminatorProperty.name, NameType.Property)}) {
${cases.join("\n")}
default:
return item;
Expand Down Expand Up @@ -239,7 +239,7 @@ function buildDiscriminatedUnionSerializer(
`);
}
output.push(`
switch (item.${type.discriminatorProperty?.name}) {
switch (item.${type.discriminatorProperty ? normalizeName(type.discriminatorProperty.name, NameType.Property) : "unknown"}) {
${cases.join("\n")}
default:
return ${baseSerializerName}(item);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 +267,228 @@ export function discountTypeProductSkuDeserializer(
skuId: item["skuId"],
};
}
```

# Should correctly handle PascalCase discriminator property names

Verify that discriminated union serializers correctly use camelCase property names when accessing discriminator properties in TypeScript interfaces, even when the TypeSpec property name is PascalCase.

## TypeSpec

This is tsp definition.

```tsp
@doc("Document type")
union DocumentType {
string,
Request: "Request",
RemoteDependency: "RemoteDependency",
Exception: "Exception",
Event: "Event",
Trace: "Trace",
Unknown: "Unknown",
}

@discriminator("DocumentType")
model DocumentIngress {
DocumentType: DocumentType;
DocumentStreamIds?: string[];
Properties?: string[];
}

model Request extends DocumentIngress {
DocumentType: DocumentType.Request;
Name?: string;
Url?: string;
}

model Exception extends DocumentIngress {
DocumentType: DocumentType.Exception;
ExceptionType?: string;
ExceptionMessage?: string;
}

@route("/documents")
interface DocumentService {
op processDocument(@body body: DocumentIngress): DocumentIngress;
}
```

Should ignore the warning `@azure-tools/typespec-ts/property-name-normalized`:

```yaml
mustEmptyDiagnostic: false
```

## Provide generated models and its serializer

Generated Models.

```ts models
/** model interface DocumentIngress */
export interface DocumentIngress {
documentType: DocumentType;
documentStreamIds?: string[];
properties?: string[];
}

export function documentIngressSerializer(item: DocumentIngress): any {
return {
DocumentType: item["documentType"],
DocumentStreamIds: !item["documentStreamIds"]
? item["documentStreamIds"]
: item["documentStreamIds"].map((p: any) => {
return p;
}),
Properties: !item["properties"]
? item["properties"]
: item["properties"].map((p: any) => {
return p;
}),
};
}

export function documentIngressDeserializer(item: any): DocumentIngress {
return {
documentType: item["DocumentType"],
documentStreamIds: !item["DocumentStreamIds"]
? item["DocumentStreamIds"]
: item["DocumentStreamIds"].map((p: any) => {
return p;
}),
properties: !item["Properties"]
? item["Properties"]
: item["Properties"].map((p: any) => {
return p;
}),
};
}

/** Alias for DocumentIngressUnion */
export type DocumentIngressUnion = Request | Exception | DocumentIngress;

export function documentIngressUnionSerializer(
item: DocumentIngressUnion,
): any {
switch (item.documentType) {
case "Request":
return requestSerializer(item as Request);

case "Exception":
return exceptionSerializer(item as Exception);

default:
return documentIngressSerializer(item);
}
}

export function documentIngressUnionDeserializer(
item: any,
): DocumentIngressUnion {
switch (item.documentType) {
case "Request":
return requestDeserializer(item as Request);

case "Exception":
return exceptionDeserializer(item as Exception);

default:
return documentIngressDeserializer(item);
}
}

/** Document type */
export type DocumentType =
| "Request"
| "RemoteDependency"
| "Exception"
| "Event"
| "Trace"
| "Unknown";

/** model interface Request */
export interface Request extends DocumentIngress {
documentType: "Request";
name?: string;
url?: string;
}

export function requestSerializer(item: Request): any {
return {
DocumentType: item["documentType"],
DocumentStreamIds: !item["documentStreamIds"]
? item["documentStreamIds"]
: item["documentStreamIds"].map((p: any) => {
return p;
}),
Properties: !item["properties"]
? item["properties"]
: item["properties"].map((p: any) => {
return p;
}),
Name: item["name"],
Url: item["url"],
};
}

export function requestDeserializer(item: any): Request {
return {
documentType: item["DocumentType"],
documentStreamIds: !item["DocumentStreamIds"]
? item["DocumentStreamIds"]
: item["DocumentStreamIds"].map((p: any) => {
return p;
}),
properties: !item["Properties"]
? item["Properties"]
: item["Properties"].map((p: any) => {
return p;
}),
name: item["Name"],
url: item["Url"],
};
}

/** model interface Exception */
export interface Exception extends DocumentIngress {
documentType: "Exception";
exceptionType?: string;
exceptionMessage?: string;
}

export function exceptionSerializer(item: Exception): any {
return {
DocumentType: item["documentType"],
DocumentStreamIds: !item["documentStreamIds"]
? item["documentStreamIds"]
: item["documentStreamIds"].map((p: any) => {
return p;
}),
Properties: !item["properties"]
? item["properties"]
: item["properties"].map((p: any) => {
return p;
}),
ExceptionType: item["exceptionType"],
ExceptionMessage: item["exceptionMessage"],
};
}

export function exceptionDeserializer(item: any): Exception {
return {
documentType: item["DocumentType"],
documentStreamIds: !item["DocumentStreamIds"]
? item["DocumentStreamIds"]
: item["DocumentStreamIds"].map((p: any) => {
return p;
}),
properties: !item["Properties"]
? item["Properties"]
: item["Properties"].map((p: any) => {
return p;
}),
exceptionType: item["ExceptionType"],
exceptionMessage: item["ExceptionMessage"],
};
}
```
Loading