Skip to content

Fix: Use client.apiVersions as source of truth for API version enum generation#9715

Merged
JoshLove-msft merged 9 commits intomainfrom
copilot/fix-apiversionenum-members
Feb 13, 2026
Merged

Fix: Use client.apiVersions as source of truth for API version enum generation#9715
JoshLove-msft merged 9 commits intomainfrom
copilot/fix-apiversionenum-members

Conversation

Copy link
Contributor

Copilot AI commented Feb 13, 2026

Fix ApiVersionEnum Missing Existing Members ✅

Problem

When adding new API versions to a TypeSpec service, the C# emitter was losing existing versions. The parseApiVersions() function was reading from sdkPackage.enums instead of client.apiVersions, which TCGC correctly populates with all versions from the @versioned enum.

Root Cause

The parseApiVersions() function had logic that:

  1. For multi-service clients, used client.apiVersions
  2. For single-service with API version enum, read from apiVersionEnum.values
  3. Otherwise, fell back to client.apiVersions

User confirmed that:

  • The TypeSpec spec has ALL versions in the @versioned enum
  • TCGC correctly returns all versions in client.apiVersions

But the emitter was ignoring client.apiVersions and reading from sdkPackage.enums instead, which may not have all values populated correctly at that point.

Solution

Simplified parseApiVersions() to always use client.apiVersions as the source of truth:

function parseApiVersions(enums, rootClients) {
  // Always use client.apiVersions as the source of truth.
  return rootClients[0]?.apiVersions ?? [];
}

Changes Made

  • Fixed parseApiVersions() in http-client-csharp emitter
  • Removed incorrect enum-reading logic
  • Removed unused imports (UsageFlags, containsMultiServiceClient)
  • Added comprehensive unit tests
  • Fixed tests to properly use @versioned decorator with namespace wrapper
  • Merged tests into existing describe block (removed duplicate)
  • Simplified code comments to be more concise
  • All tests passing ✅
  • Lint passing ✅
  • Full project builds successfully
  • Code review completed with no issues
  • Verified other emitters don't have this bug

Tests Added

Two new test cases in the existing parseApiVersions describe block in client-model-builder.test.ts:

  1. Test with 3 versions (v1, v2, v3) - verifies all versions are preserved
  2. Test with date versions (2023-01-01, 2024-01-01, 2025-01-01) - verifies order preservation

Both tests follow the pattern from line 208 test: using IsNamespaceNeeded: false and defining a complete namespace with @service and @versioned decorators.

Impact Analysis

  • http-client-csharp: ✅ Fixed
  • http-client-python: ✅ Not affected (already uses client.apiVersions directly)
  • http-client-java: ✅ Not affected (already uses client.apiVersions directly)

Test & Lint Results

✓ All tests passing
✓ Lint passing with no warnings

Files Changed

  • packages/http-client-csharp/emitter/src/lib/client-model-builder.ts (13 deletions, 5 additions)
  • packages/http-client-csharp/emitter/test/Unit/client-model-builder.test.ts (64 additions, 12 deletions)

Ready for merge! 🚀

Original prompt

This section details on the original issue you should resolve

<issue_title>ApiVersionEnum missing existing members</issue_title>
<issue_description>https://github.com/Azure/azure-rest-api-specs/pull/40036/changes is adding a new member, but when generating we lose the previous version.</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 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.

This fixes the issue where API versions were being lost when adding new versions.
The parseApiVersions() function was incorrectly reading from sdkPackage.enums
instead of using client.apiVersions, which is the source of truth from TCGC.

- Removed logic that reads from apiVersionEnum.values
- Simplified parseApiVersions to always use client.apiVersions
- Removed unused imports (UsageFlags, containsMultiServiceClient)

Fixes microsoft/typespec#<issue-number>

Co-authored-by: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com>
@microsoft-github-policy-service microsoft-github-policy-service bot added the emitter:client:csharp Issue for the C# client emitter: @typespec/http-client-csharp label Feb 13, 2026
Co-authored-by: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix ApiVersionEnum missing existing members Fix: Use client.apiVersions as source of truth for API version enum generation Feb 13, 2026
Copilot AI requested a review from JoshLove-msft February 13, 2026 16:31
…ing test patterns

Co-authored-by: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com>
…test patterns

Co-authored-by: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com>
…ir own version enum

Co-authored-by: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com>
Co-authored-by: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com>
@JoshLove-msft
Copy link
Contributor

@copilot the new tests are failing. Please run and ensure that they are passing.

AssertionError: Unexpected diagnostics:
/test/main.tsp:28:7 - error decorator-wrong-target: Cannot apply @versioned decorator to Azure.Csharp.Testing.Versions since it is not assignable to Namespace
❯ expectDiagnosticEmpty node_modules/@typespec/compiler/src/testing/expect.ts:14:5
❯ Object.compile node_modules/@typespec/compiler/src/testing/test-host.ts:62:5
❯ typeSpecCompile emitter/test/Unit/utils/test-util.ts:97:3
95| noEmit: options?.NoEmit ?? true,
96| } as CompilerOptions;
97| await host.compile("./", cliOptions);
| ^
98| return host.program;
99| }
❯ emitter/test/Unit/client-model-builder.test.ts:480:21

…to work correctly

Co-authored-by: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Feb 13, 2026

@copilot the new tests are failing. Please run and ensure that they are passing.

AssertionError: Unexpected diagnostics:
...

Fixed! The tests were failing because @versioned requires a namespace wrapper. Updated both tests to follow the pattern from line 208: defining a complete namespace with @service and @versioned decorators, and using only IsNamespaceNeeded: false. All 10 tests in client-model-builder.test.ts now pass. (1cbf035)

@JoshLove-msft
Copy link
Contributor

@copilot lint is failing
/home/runner/work/typespec/typespec/packages/http-client-csharp/emitter/test/Unit/client-model-builder.test.ts
Error: 445:1 error Describe is used multiple times in the same describe(suite) block vitest/no-identical-title

Co-authored-by: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Feb 13, 2026

@copilot lint is failing
/home/runner/work/typespec/typespec/packages/http-client-csharp/emitter/test/Unit/client-model-builder.test.ts
Error: 445:1 error Describe is used multiple times in the ...

Fixed! Removed the duplicate describe("parseApiVersions") block and merged the two new tests into the existing parseApiVersions describe block. Lint now passes with no errors. (c4b69ef)

@JoshLove-msft JoshLove-msft added this pull request to the merge queue Feb 13, 2026
Merged via the queue into main with commit 7b219f9 Feb 13, 2026
23 checks passed
@JoshLove-msft JoshLove-msft deleted the copilot/fix-apiversionenum-members branch February 13, 2026 20:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

emitter:client:csharp Issue for the C# client emitter: @typespec/http-client-csharp

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ApiVersionEnum missing existing members

3 participants