Skip to content
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

typespec-autorest - @Openapi.extension emit schema for Types #2306

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Copy link
Member

Choose a reason for hiding this comment

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

I am a tiny bit worried about this change for the value it brings. Wondering if we might be better to just cut of the type conversion for 1.0 so at least using a type is exposed as broken with teh codefix to the user and maybe we add the feature parity in a few month.

Here I am worried that a lot of 3p customers might not pulled the last version and this would be a bigger break(quite confusing I assume as well) with no codefix

Original file line number Diff line number Diff line change
@@ -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
```
11 changes: 10 additions & 1 deletion packages/typespec-autorest/src/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ import {
isStringType,
isTemplateDeclaration,
isTemplateDeclarationOrInstance,
isType,
isVoidType,
joinPaths,
navigateTypesInNamespace,
Expand Down Expand Up @@ -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);
}
}
}
}
Expand Down
74 changes: 74 additions & 0 deletions packages/typespec-autorest/test/openapi-output.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>)
@extension("x-string-literal", typeof "hi")
model Foo {}
model Collection<Item> {
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<Item> {
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(
`
Expand Down
Loading