Skip to content

[spector] Add discriminated union test cases #7602

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jun 10, 2025

This PR adds comprehensive spector test cases for discriminated unions based on the TypeSpec standard library documentation.

Scenarios Implemented

Added test cases covering the three main discriminated union patterns:

  1. Default serialization (/envelope) - Uses default kind and value envelope structure:

    {
      "kind": "cat",
      "value": {
        "name": "Whiskers",
        "meow": true
      }
    }
  2. Custom property names (/custom-names) - Uses custom discriminator and envelope properties:

    {
      "petType": "cat", 
      "petData": {
        "name": "Whiskers",
        "meow": true
      }
    }
  3. Inline discriminator (/inline) - Uses envelope: "none" to inject discriminator directly:

    {
      "kind": "cat",
      "name": "Whiskers", 
      "meow": true
    }

Each scenario includes both GET and PUT operations with complete mockapi implementations.

Files Added

  • packages/http-specs/specs/type/union/discriminated/main.tsp - TypeSpec definitions with 6 scenarios
  • packages/http-specs/specs/type/union/discriminated/mockapi.ts - Mock API implementations
  • Updated spec-summary.md with auto-generated documentation

Validation

✅ All 56 scenarios (including new ones) pass validation
✅ TypeScript compilation successful
✅ Mock API validation passes
✅ No linting errors
✅ Documentation regenerated

The implementation follows existing spector patterns and provides comprehensive test coverage for discriminated unions as specified in the TypeSpec documentation.

Fixes #7601.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • typespec.io
    • Triggering command: curl -s REDACTED (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits June 10, 2025 08:43
Co-authored-by: msyyc <70930885+msyyc@users.noreply.github.com>
Co-authored-by: msyyc <70930885+msyyc@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] [spector] Add cases for discriminated union [spector] Add discriminated union test cases Jun 10, 2025
@Copilot Copilot AI requested a review from msyyc June 10, 2025 08:45
Copilot finished work on behalf of msyyc June 10, 2025 08:45
Co-authored-by: msyyc <70930885+msyyc@users.noreply.github.com>
@msyyc
Copy link
Contributor

msyyc commented Jun 11, 2025

@copilot Apply comments #7602 (comment) and #7602 (comment) for all scenarios.

Copilot finished work on behalf of msyyc June 11, 2025 09:22
… to all GET endpoints

Co-authored-by: msyyc <70930885+msyyc@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jun 11, 2025

@copilot Apply comments #7602 (comment) and #7602 (comment) for all scenarios.

Applied both comments to all scenarios:

  1. Added optional parameters to all GET operations (envelope: kind?: string, custom-names: petType?: string, inline: kind?: string)
  2. Changed all PUT operations to return the request body instead of NoContentResponse

Updated mockapi implementations to handle the optional parameters with appropriate response logic (when parameter is null/"cat" returns cat response, when "dog" returns dog response). All 56 scenarios pass validation. Implemented in commit ab3ee92.

Copilot finished work on behalf of msyyc June 11, 2025 09:41
Co-authored-by: msyyc <70930885+msyyc@users.noreply.github.com>
Copilot finished work on behalf of msyyc June 12, 2025 00:08
… union scenarios

Co-authored-by: msyyc <70930885+msyyc@users.noreply.github.com>
Copilot finished work on behalf of msyyc June 12, 2025 03:06
Co-authored-by: msyyc <70930885+msyyc@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jun 12, 2025

Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • telemetry.astro.build
    • Triggering command: node /home/REDACTED/work/typespec/typespec/website/node_modules/.bin/../astro/astro.js build (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot finished work on behalf of msyyc June 12, 2025 04:11
Copy link
Contributor

All changed packages have been documented.

  • @typespec/http-specs
Show changes

@typespec/http-specs - feature ✏️

Add discriminated union spector test cases covering default serialization, custom property names, and inline discriminator scenarios

…ehavior

Co-authored-by: msyyc <70930885+msyyc@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jun 17, 2025

Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • telemetry.astro.build
    • Triggering command: node /home/REDACTED/work/typespec/typespec/website/node_modules/.bin/../astro/astro.js build (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot finished work on behalf of msyyc June 17, 2025 07:12
@MaryGao
Copy link
Member

MaryGao commented Jun 17, 2025

@joheredi js 3p emitter e2e failure for this case blocks this pr, is it possible to prioritze the fix?
https://dev.azure.com/azure-sdk/public/_build/results?buildId=4982089&view=logs&j=9ea70591-ad2c-549f-9859-1ba11dbbd52c&t=5d5e2a00-ea43-5380-bb2b-bb168af18ba8

packages/http-client-js test:e2e: > tsc -p tsconfig.test.json
packages/http-client-js test:e2e: test/e2e/generated/type/union/discriminated/src/models/internal/serializers.ts(86,5): error TS2322: Type 'Cat' is not assignable to type 'PetWithEnvelope'.
packages/http-client-js test:e2e: test/e2e/generated/type/union/discriminated/src/models/internal/serializers.ts(90,5): error TS2322: Type 'Dog' is not assignable to type 'PetWithEnvelope'.
packages/http-client-js test:e2e: test/e2e/generated/type/union/discriminated/src/models/internal/serializers.ts(150,5): error TS2322: Type 'Cat' is not assignable to type 'PetWithCustomNames'.
packages/http-client-js test:e2e: test/e2e/generated/type/union/discriminated/src/models/internal/serializers.ts(154,5): error TS2322: Type 'Dog' is not assignable to type 'PetWithCustomNames'.
packages/http-client-js test:e2e: test/e2e/generated/type/union/discriminated/src/models/internal/serializers.ts(186,5): error TS2322: Type 'Cat' is not assignable to type 'PetInline'.
packages/http-client-js test:e2e:   Type 'Cat' is not assignable to type '{ kind: "cat"; } & Cat'.
packages/http-client-js test:e2e:     Property 'kind' is missing in type 'Cat' but required in type '{ kind: "cat"; }'.
packages/http-client-js test:e2e: test/e2e/generated/type/union/discriminated/src/models/internal/serializers.ts(190,5): error TS2322: Type 'Dog' is not assignable to type 'PetInline'.
packages/http-client-js test:e2e:   Type 'Dog' is not assignable to type '{ kind: "dog"; } & Dog'.
packages/http-client-js test:e2e:     Property 'kind' is missing in type 'Dog' but required in type '{ kind: "dog"; }'.
packages/http-client-js test:e2e:  ELIFECYCLE  Command failed with exit code 2.
packages/http-client-js test:e2e: Failed
/mnt/vss/_work/1/s/packages/http-client-js:

Copy link
Contributor Author

Copilot AI commented Jun 17, 2025

@joheredi js 3p emitter e2e failure for this case blocks this pr, is it possible to prioritze the fix?
https://dev.azure.com/azure-sdk/public/_build/results?buildId=4982089&view=logs&j=9ea70591-ad2c-...

This appears to be an issue with the JS emitter's code generation for discriminated unions rather than with the TypeSpec definitions. I've verified that:

✅ All 56 scenarios pass TypeSpec validation
✅ Mock API validation passes
✅ TypeSpec compilation succeeds

The error shows that the generated TypeScript serializers are trying to assign Cat/Dog types directly to the discriminated union types (PetWithEnvelope, PetWithCustomNames, PetInline), but the discriminated unions expect the full envelope structure.

This suggests the JS emitter needs updates to properly handle discriminated union serialization. The TypeSpec definitions follow the standard patterns from the documentation and work correctly at the TypeSpec level.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • telemetry.astro.build
    • Triggering command: node /home/REDACTED/work/typespec/typespec/website/node_modules/.bin/../astro/astro.js build (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot finished work on behalf of MaryGao June 17, 2025 14:14
- Reorganized into Envelope > Object > (Default, CustomProperties) and NoEnvelope structure
- Updated routes: /envelope/object/default, /envelope/object/custom-properties, /no-envelope
- Updated operation names and mockapi implementations to match new hierarchy
- Provides better scalability for future envelope types as suggested

Co-authored-by: timotheeguerin <1031227+timotheeguerin@users.noreply.github.com>
Copilot finished work on behalf of timotheeguerin June 17, 2025 14:51
Copilot finished work on behalf of timotheeguerin June 17, 2025 15:02
Co-authored-by: msyyc <70930885+msyyc@users.noreply.github.com>
Copilot finished work on behalf of msyyc June 18, 2025 01:50
@Copilot Copilot AI requested a review from msyyc June 18, 2025 01:50
Co-authored-by: msyyc <70930885+msyyc@users.noreply.github.com>
Copilot finished work on behalf of msyyc June 18, 2025 01:59
@msyyc msyyc requested review from timotheeguerin and lirenhe June 18, 2025 02:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[spector] Add cases for discriminated union
6 participants