Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Aug 15, 2025

The discriminated union serializers were using the original PascalCase property names from TypeSpec (e.g., DocumentType) in switch statements, but the generated TypeScript interfaces use camelCase property names (e.g., documentType). This caused TypeScript compilation errors:

src/models/models.ts:134:16 - error TS2551: Property 'DocumentType' does not exist on type 'DocumentIngressUnion'. Did you mean 'documentType'?
  Property 'DocumentType' does not exist on type 'Request'.

134   switch (item.DocumentType) {
                   ~~~~~~~~~~~~

Consider this TypeSpec definition:

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

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

Previously, the generated serializer would incorrectly use:

switch (item.DocumentType) { // Error: DocumentType doesn't exist on interface

Now it correctly uses:

switch (item.documentType) { // Correct: uses camelCase property name

Changes made:

  • Updated buildSerializerFunction.ts to use normalizeName(discriminatorProperty.name, NameType.Property) in switch statements
  • Updated buildDeserializerFunction.ts to use normalizeName(discriminatorProperty.name, NameType.Property) in switch statements
  • Added comprehensive test case demonstrating the fix for PascalCase discriminator properties
  • All existing tests continue to pass

The fix ensures that discriminated union serializers and deserializers correctly access discriminator properties using the normalized camelCase names that match the generated TypeScript interface properties.

Fixes #3435.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

@Copilot Copilot AI changed the title [WIP] Verify that the library option is set to true for @azure-rest/livemetrics Fix discriminated union property access to use camelCase property names Aug 15, 2025
@Copilot Copilot AI requested a review from v-jiaodi August 15, 2025 02:58
Copilot finished work on behalf of v-jiaodi August 15, 2025 02:58
@MaryGao MaryGao added the Experiement Experiement PR/issue for AI label Aug 20, 2025

@route("/documents")
interface DocumentService {
op processDocument(@body body: DocumentIngress): void;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot update return value void to DocumentIngress in order to also cover Deserializer case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the return type from void to DocumentIngress and added all the required deserializer functions. This now provides comprehensive test coverage for both serializers and deserializers with discriminated unions. Changes committed in 738008e.

Changed processDocument operation return type from void to DocumentIngress to also test deserializer generation. Added all missing deserializer functions for DocumentIngress, Request, Exception and the union deserializer.

Co-authored-by: v-jiaodi <[email protected]>
Copilot finished work on behalf of v-jiaodi August 26, 2025 08:50
@Copilot Copilot AI requested a review from v-jiaodi August 26, 2025 08:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Experiement Experiement PR/issue for AI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Verify that the library option is set to true for @azure-rest/livemetrics
3 participants