From e92e8e5f0b3d65761ba75d0cebf422cc3f7f3ec6 Mon Sep 17 00:00:00 2001 From: Christopher Radek Date: Tue, 4 Mar 2025 10:04:11 -0800 Subject: [PATCH 1/3] update typespec-autorest `@extension` support to emit schemas for passed in Types. --- packages/typespec-autorest/src/openapi.ts | 11 ++- .../test/openapi-output.test.ts | 74 +++++++++++++++++++ 2 files changed, 84 insertions(+), 1 deletion(-) diff --git a/packages/typespec-autorest/src/openapi.ts b/packages/typespec-autorest/src/openapi.ts index 23130d2c13..228abe50e0 100644 --- a/packages/typespec-autorest/src/openapi.ts +++ b/packages/typespec-autorest/src/openapi.ts @@ -94,6 +94,7 @@ import { isStringType, isTemplateDeclaration, isTemplateDeclarationOrInstance, + isType, isVoidType, joinPaths, navigateTypesInNamespace, @@ -2133,7 +2134,15 @@ export async function getOpenAPIForService( } if (extensions) { for (const key of extensions.keys()) { - emitObject[key] = extensions.get(key); + const value = extensions.get(key); + if (isType(value)) { + emitObject[key] = getSchemaForType(value, { + ignoreMetadataAnnotations: false, + visibility: Visibility.All, + }); + } else { + emitObject[key] = extensions.get(key); + } } } } diff --git a/packages/typespec-autorest/test/openapi-output.test.ts b/packages/typespec-autorest/test/openapi-output.test.ts index b1cb9d0f2e..5f3fcfd9bd 100644 --- a/packages/typespec-autorest/test/openapi-output.test.ts +++ b/packages/typespec-autorest/test/openapi-output.test.ts @@ -829,6 +829,80 @@ describe("typespec-autorest: extension decorator", () => { strictEqual(oapi.parameters.PetId["x-parameter-extension"], "foobaz"); }); + it("emits schemas for Types", async () => { + const oapi = await openApiFor(` + @extension("x-model-expression", { name: string }) + @extension("x-array", string[]) + @extension("x-model-template", Collection) + @extension("x-string-literal", typeof "hi") + model Foo {} + model Collection { + size: int32; + item: Item[]; + } + `); + const Foo = oapi.definitions.Foo; + ok(Foo); + deepStrictEqual(Foo["x-model-expression"], { + type: "object", + required: ["name"], + properties: { + name: { + type: "string", + }, + }, + }); + deepStrictEqual(Foo["x-array"], { + items: { type: "string" }, + type: "array", + }); + deepStrictEqual(Foo["x-model-template"], { + properties: { + item: { + items: { + type: "string", + }, + type: "array", + }, + size: { + format: "int32", + type: "integer", + }, + }, + required: ["size", "item"], + type: "object", + }); + deepStrictEqual(Foo["x-string-literal"], { + type: "string", + enum: ["hi"], + "x-ms-enum": { + modelAsString: false, + }, + }); + }); + + it("emits raw data for Values", async () => { + const oapi = await openApiFor(` + @extension("x-object-value", #{ name: "TypeSpec" }) + @extension("x-tuple", #[ 1 ]) + @extension("x-string-literal", "hi") + model Foo {} + model Collection { + size: int32; + item: Item[]; + } + + model Thing { + name: string; + } + `); + const Foo = oapi.definitions.Foo; + ok(Foo); + deepStrictEqual(Foo["x-object-value"], { name: "TypeSpec" }); + deepStrictEqual(Foo["x-tuple"], [1]); + deepStrictEqual(Foo["x-string-literal"], "hi"); + }); + it("support x-ms-identifiers with null array ", async () => { const oapi = await openApiFor( ` From 1c1b6204fb5363a59f58301d8745a755ad57cd73 Mon Sep 17 00:00:00 2001 From: Christopher Radek Date: Tue, 4 Mar 2025 10:36:54 -0800 Subject: [PATCH 2/3] add changelog --- ...extension-emit-schema-2025-2-4-10-35-45.md | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 .chronus/changes/openapi-extension-emit-schema-2025-2-4-10-35-45.md diff --git a/.chronus/changes/openapi-extension-emit-schema-2025-2-4-10-35-45.md b/.chronus/changes/openapi-extension-emit-schema-2025-2-4-10-35-45.md new file mode 100644 index 0000000000..66aad1b3f3 --- /dev/null +++ b/.chronus/changes/openapi-extension-emit-schema-2025-2-4-10-35-45.md @@ -0,0 +1,24 @@ +--- +changeKind: breaking +packages: + - "@azure-tools/typespec-autorest" +--- + +Updates the `@extension` behavior to emit schemas for passed in Types. Values will continue to be emitted as raw data. Model and Tuple expressions that were previously treated as Values are now treated as Types. + +Now the following TypeSpec: +```tsp +@OpenAPI.extension("x-value", "custom value") +@OpenAPI.extension("x-schema", typeof "custom value") +model Foo {} +``` +emits the following schema: +```yaml +Foo: + type: object + x-value: custom value + x-schema: + type: string + enum: + - custom value +``` From 4198b395d7b706a67028ab1340a900cfc5c0130c Mon Sep 17 00:00:00 2001 From: Christopher Radek Date: Tue, 4 Mar 2025 10:41:29 -0800 Subject: [PATCH 3/3] update core submodule (temp) --- core | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core b/core index d4d5c3dea8..98ee2c8079 160000 --- a/core +++ b/core @@ -1 +1 @@ -Subproject commit d4d5c3dea882e159c73015ae8eedb95a117c5170 +Subproject commit 98ee2c80791716c08ff89db54111642d893a1704