-
Notifications
You must be signed in to change notification settings - Fork 83
feat(typespec-metadata): add apiVersion field using TCGC resolution #5065
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
iscai-msft
wants to merge
19
commits into
Azure:main
Choose a base branch
from
iscai-msft:iscai-msft-add-apiversion-to-metadata
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
0ce39ea
feat(typespec-metadata): add apiVersion field using TCGC resolution
b9bf362
chore: update pnpm-lock.yaml for new TCGC peer dependency
e8302ac
test(typespec-metadata): add e2e tests for apiVersion resolution
14d38da
test(typespec-metadata): rewrite e2e tests to verify emitted metadata…
427b365
chore: add changeset for apiVersion feature
4aa538a
refactor: remove redundant apiVersion unit tests from collector.test.ts
3a5a9e0
style: apply prettier formatting
2a8df3d
fix: revert pnpm-lock.yaml to upstream/main
0e879fb
chore: revert manual lockfile edit (will regenerate in cloud)
75d83ab
chore: regenerate pnpm-lock.yaml for new typespec-metadata deps
897c048
refactor: restructure test tester into ApiTester/MetadataTester/Emitt…
e6112f4
refactor: remove api-version emitter option, use additionalProperties…
034d522
fix: check sdkContext.apiVersion for 'all' before resolving from meta…
a8865ba
revert: restore additionalProperties: false in metadata options schema
1ed30f1
test: set api-version 'all' on typespec-python instead of typespec-me…
ef4b4bc
feat(tcgc): add isPreview to SdkPackageType metadata
75b4fc4
Revert "feat(tcgc): add isPreview to SdkPackageType metadata"
f82a9fb
Merge remote-tracking branch 'upstream/main' into iscai-msft-stunning…
dadf0f5
feat(typespec-metadata): add sdkType field (preview/stable) to metadata
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
.chronus/changes/add-apiversion-to-metadata-2026-07-27-11-34-00.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| changeKind: feature | ||
| packages: | ||
| - "@azure-tools/typespec-metadata" | ||
| --- | ||
|
|
||
| Add `apiVersion` field to `LanguagePackageMetadata` resolved via TCGC's `createSdkContext`. Values: `"all"` when configured for all versions, `"multiple-versions"` for multi-service configs, the actual resolved version string, or `undefined` when unavailable. Also adds `api-version` as a passthrough emitter option. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| import { parse as parseYaml } from "yaml"; | ||
| import { describe, expect, it } from "vitest"; | ||
| import type { MetadataSnapshot } from "../src/metadata.js"; | ||
| import { SimpleTester } from "./tester.js"; | ||
|
|
||
| function emitMetadata(code: string, compilerOptions: Record<string, unknown> = {}) { | ||
| return SimpleTester.emit("@azure-tools/typespec-metadata", {}).compileAndDiagnose(code, { | ||
| compilerOptions: { | ||
| options: { | ||
| "@azure-tools/typespec-python": { | ||
| "package-name": "azure-test-service", | ||
| }, | ||
| ...((compilerOptions.options as Record<string, unknown>) ?? {}), | ||
| }, | ||
| ...Object.fromEntries( | ||
| Object.entries(compilerOptions).filter(([k]) => k !== "options"), | ||
| ), | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| function parseMetadata(outputs: Record<string, string>): MetadataSnapshot { | ||
| const content = Object.values(outputs)[0]; | ||
| return parseYaml(content) as MetadataSnapshot; | ||
| } | ||
|
|
||
| describe("apiVersion in emitted metadata", () => { | ||
| it("single versioned service emits latest version as apiVersion", async () => { | ||
| const [{ outputs }] = await emitMetadata(` | ||
| @service(#{ | ||
| title: "Widget Service", | ||
| }) | ||
| @versioned(WidgetService.Versions) | ||
| namespace WidgetService; | ||
|
|
||
| enum Versions { | ||
| v1, | ||
| v2, | ||
| v3, | ||
| } | ||
|
|
||
| op test(): void; | ||
| `); | ||
|
|
||
| const snapshot = parseMetadata(outputs); | ||
| const pythonMeta = snapshot.languages["python"]; | ||
| expect(pythonMeta).toBeDefined(); | ||
| expect(pythonMeta[0].apiVersion).toBe("v3"); | ||
| }); | ||
|
|
||
| it("service with api-version 'all' emits 'all' as apiVersion", async () => { | ||
| const [{ outputs }] = await SimpleTester.emit("@azure-tools/typespec-metadata", {}).compileAndDiagnose( | ||
| ` | ||
| @service(#{ | ||
| title: "Widget Service", | ||
| }) | ||
| @versioned(WidgetService.Versions) | ||
| namespace WidgetService; | ||
|
|
||
| enum Versions { | ||
| v1, | ||
| v2, | ||
| v3, | ||
| } | ||
|
|
||
| op test(): void; | ||
| `, | ||
| { | ||
| compilerOptions: { | ||
| options: { | ||
| "@azure-tools/typespec-metadata": { "api-version": "all" }, | ||
| "@azure-tools/typespec-python": { | ||
| "package-name": "azure-test-service", | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| ); | ||
|
|
||
| const snapshot = parseMetadata(outputs); | ||
| expect(snapshot.languages["python"][0].apiVersion).toBe("all"); | ||
| }); | ||
|
|
||
| it("service without versioning emits undefined apiVersion", async () => { | ||
| const [{ outputs }] = await emitMetadata(` | ||
| @service(#{ | ||
| title: "Widget Service", | ||
| }) | ||
| namespace WidgetService; | ||
|
|
||
| op test(): void; | ||
| `); | ||
|
|
||
| const snapshot = parseMetadata(outputs); | ||
| expect(snapshot.languages["python"][0].apiVersion).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("multiple services emit 'multiple-versions' as apiVersion", async () => { | ||
| const [{ outputs }] = await emitMetadata(` | ||
| @service | ||
| @versioned(VersionsA) | ||
| namespace ServiceA { | ||
| enum VersionsA { | ||
| av1, | ||
| av2, | ||
| } | ||
| interface AI { | ||
| @route("/aTest") | ||
| aTest(@query("api-version") apiVersion: VersionsA): void; | ||
| } | ||
| } | ||
| @service | ||
| @versioned(VersionsB) | ||
| namespace ServiceB { | ||
| enum VersionsB { | ||
| bv1, | ||
| bv2, | ||
| } | ||
| interface BI { | ||
| @route("/bTest") | ||
| bTest(@query("api-version") apiVersion: VersionsB): void; | ||
| } | ||
| } | ||
| `); | ||
|
|
||
| const snapshot = parseMetadata(outputs); | ||
| const pythonMeta = snapshot.languages["python"]; | ||
| expect(pythonMeta[0].apiVersion).toBe("multiple-versions"); | ||
| }); | ||
|
|
||
| it("apiVersion is applied to all language emitters", async () => { | ||
| const [{ outputs }] = await emitMetadata( | ||
| ` | ||
| @service(#{ | ||
| title: "Widget Service", | ||
| }) | ||
| @versioned(WidgetService.Versions) | ||
| namespace WidgetService; | ||
|
|
||
| enum Versions { | ||
| v1, | ||
| v2, | ||
| } | ||
|
|
||
| op test(): void; | ||
| `, | ||
| { | ||
| options: { | ||
| "@azure-tools/typespec-python": { | ||
| "package-name": "azure-test-service", | ||
| }, | ||
| "@azure-tools/typespec-java": { | ||
| "package-name": "com.azure:azure-test-service", | ||
| }, | ||
| }, | ||
| }, | ||
| ); | ||
|
|
||
| const snapshot = parseMetadata(outputs); | ||
| expect(snapshot.languages["python"][0].apiVersion).toBe("v2"); | ||
| expect(snapshot.languages["java"][0].apiVersion).toBe("v2"); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import { resolvePath } from "@typespec/compiler"; | ||
| import { createTester } from "@typespec/compiler/testing"; | ||
|
|
||
| export const MetadataTester = createTester(resolvePath(import.meta.dirname, ".."), { | ||
|
iscai-msft marked this conversation as resolved.
Outdated
|
||
| libraries: [ | ||
| "@typespec/http", | ||
| "@typespec/rest", | ||
| "@typespec/versioning", | ||
| "@azure-tools/typespec-client-generator-core", | ||
| "@azure-tools/typespec-metadata", | ||
| ], | ||
| }); | ||
|
|
||
| export const SimpleTester = MetadataTester.import( | ||
| "@typespec/http", | ||
| "@typespec/rest", | ||
| "@typespec/versioning", | ||
| "@azure-tools/typespec-client-generator-core", | ||
| ).using("Http", "Rest", "Versioning", "Azure.ClientGenerator.Core"); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be in language metadata or typespec metadata? Is it possible to have different API version value for each language?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you're right, I guess each language could specify different values in tspconfig.yaml, so it shouldn't be the case but it could be
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would you prefer to ignore that possibility and move it to typespec metadata, or keep it where it is in language metadata
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope. Let's keep it in language metadata. It's easier to parse all get the distinct on the consumer side.