From 231858ecf7912f8c145c75570622b69501487a6e Mon Sep 17 00:00:00 2001 From: Timothee Guerin Date: Thu, 9 May 2024 13:32:13 -0700 Subject: [PATCH 1/6] @summary --- packages/typespec-autorest/src/openapi.ts | 5 +++++ packages/typespec-autorest/test/models.test.ts | 15 +++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/packages/typespec-autorest/src/openapi.ts b/packages/typespec-autorest/src/openapi.ts index 551d992085..6ff8709150 100644 --- a/packages/typespec-autorest/src/openapi.ts +++ b/packages/typespec-autorest/src/openapi.ts @@ -1740,6 +1740,11 @@ export async function getOpenAPIForService( newTarget.description = docStr; } + const title = getSummary(program, typespecType); + if (title) { + target.title = title; + } + const formatStr = getFormat(program, typespecType); if (isString && formatStr) { const allowedStringFormats = [ diff --git a/packages/typespec-autorest/test/models.test.ts b/packages/typespec-autorest/test/models.test.ts index 42337fc209..237bcad7d6 100644 --- a/packages/typespec-autorest/test/models.test.ts +++ b/packages/typespec-autorest/test/models.test.ts @@ -90,6 +90,21 @@ describe("typespec-autorest: model definitions", () => { }); }); + it("using @summary sets the title on definitions and", async () => { + const res = await oapiForModel( + "Foo", + ` + @summary("FooModel") + model Foo { + @summary("YProp") + y: int32; + }; + ` + ); + strictEqual(res.defs.Foo.title, "FooModel"); + strictEqual(res.defs.Foo.properties.y.title, "YProp"); + }); + it("uses json name specified via @encodedName", async () => { const res = await oapiForModel( "Foo", From ae3d4fb9bd1039a71c9e751e5c1410c5cafc396b Mon Sep 17 00:00:00 2001 From: Timothee Guerin Date: Thu, 9 May 2024 13:32:18 -0700 Subject: [PATCH 2/6] , --- packages/typespec-autorest/test/models.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/typespec-autorest/test/models.test.ts b/packages/typespec-autorest/test/models.test.ts index 237bcad7d6..0917935912 100644 --- a/packages/typespec-autorest/test/models.test.ts +++ b/packages/typespec-autorest/test/models.test.ts @@ -90,7 +90,7 @@ describe("typespec-autorest: model definitions", () => { }); }); - it("using @summary sets the title on definitions and", async () => { + it("using @summary sets the title on definitions and properties", async () => { const res = await oapiForModel( "Foo", ` From 434097f1253521851861d33b8e9904af3b33542d Mon Sep 17 00:00:00 2001 From: Timothee Guerin Date: Thu, 9 May 2024 13:42:05 -0700 Subject: [PATCH 3/6] Create summary-is-title-2024-4-9-20-35-33.md --- .chronus/changes/summary-is-title-2024-4-9-20-35-33.md | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 .chronus/changes/summary-is-title-2024-4-9-20-35-33.md diff --git a/.chronus/changes/summary-is-title-2024-4-9-20-35-33.md b/.chronus/changes/summary-is-title-2024-4-9-20-35-33.md new file mode 100644 index 0000000000..abb482045f --- /dev/null +++ b/.chronus/changes/summary-is-title-2024-4-9-20-35-33.md @@ -0,0 +1,8 @@ +--- +# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking +changeKind: fix +packages: + - "@azure-tools/typespec-autorest" +--- + +`@summary` sets the title of definitions From a8c50cc89b05dd7950e13cfc063ece519dd92cf0 Mon Sep 17 00:00:00 2001 From: Timothee Guerin Date: Thu, 9 May 2024 13:42:15 -0700 Subject: [PATCH 4/6] Update summary-is-title-2024-4-9-20-35-33.md --- .chronus/changes/summary-is-title-2024-4-9-20-35-33.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.chronus/changes/summary-is-title-2024-4-9-20-35-33.md b/.chronus/changes/summary-is-title-2024-4-9-20-35-33.md index abb482045f..73a41a6c3e 100644 --- a/.chronus/changes/summary-is-title-2024-4-9-20-35-33.md +++ b/.chronus/changes/summary-is-title-2024-4-9-20-35-33.md @@ -1,6 +1,6 @@ --- # Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking -changeKind: fix +changeKind: feature packages: - "@azure-tools/typespec-autorest" --- From f47d785646634680abe6d7168c800eb9ad2c070f Mon Sep 17 00:00:00 2001 From: Timothee Guerin Date: Thu, 9 May 2024 14:47:39 -0700 Subject: [PATCH 5/6] test --- packages/typespec-autorest/src/openapi.ts | 29 +++++++++++++------ .../src/openapi2-document.ts | 2 +- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/packages/typespec-autorest/src/openapi.ts b/packages/typespec-autorest/src/openapi.ts index 1db5a48eba..3e7dfb6ff4 100644 --- a/packages/typespec-autorest/src/openapi.ts +++ b/packages/typespec-autorest/src/openapi.ts @@ -43,6 +43,7 @@ import { Value, compilerAssert, createDiagnosticCollector, + explainStringTemplateNotSerializable, getAllTags, getDirectoryPath, getDiscriminator, @@ -84,7 +85,6 @@ import { navigateTypesInNamespace, resolveEncodedName, resolvePath, - stringTemplateToString, } from "@typespec/compiler"; import { TwoLevelMap } from "@typespec/compiler/utils"; import { @@ -1566,7 +1566,7 @@ export async function getOpenAPIForService( modelSchema.required = [propertyName]; } } - + applySummary(model, modelSchema); applyExternalDocs(model, modelSchema); for (const prop of model.properties.values()) { @@ -1589,7 +1589,6 @@ export async function getOpenAPIForService( const clientName = getClientName(context, prop); const description = getDoc(program, prop); - // if this property is a discriminator property, remove it to keep autorest validation happy if (model.baseModel) { const { propertyName } = getDiscriminator(program, model.baseModel) || {}; @@ -1617,6 +1616,7 @@ export async function getOpenAPIForService( if (description) { property.description = description; } + applySummary(prop, property); if (prop.defaultValue && !("$ref" in property)) { property.default = getDefaultValue(prop.defaultValue); @@ -1690,9 +1690,10 @@ export async function getOpenAPIForService( function resolveProperty(prop: ModelProperty, context: SchemaContext): OpenAPI2SchemaProperty { let propSchema; - if (prop.type.kind === "Enum" && prop.default) { + console.log("Test this is reported"); + if (prop.type.kind === "Enum" && prop.defaultValue) { propSchema = getSchemaForEnum(prop.type); - } else if (prop.type.kind === "Union" && prop.default) { + } else if (prop.type.kind === "Union" && prop.defaultValue) { const [asEnum, _] = getUnionAsEnum(prop.type); if (asEnum) { propSchema = getSchemaForUnionEnum(prop.type, asEnum); @@ -1908,6 +1909,12 @@ export async function getOpenAPIForService( } } + function applySummary(typespecType: Type, target: { title?: string }) { + const summary = getSummary(program, typespecType); + if (summary) { + target.title = summary; + } + } function applyExternalDocs(typespecType: Type, target: Record) { const externalDocs = getExternalDocs(program, typespecType); if (externalDocs) { @@ -1954,12 +1961,16 @@ export async function getOpenAPIForService( } function getSchemaForStringTemplate(stringTemplate: StringTemplate) { - const [value, diagnostics] = stringTemplateToString(stringTemplate); - if (diagnostics.length > 0) { - program.reportDiagnostics(diagnostics.map((x) => ({ ...x, severity: "warning" }))); + if (stringTemplate.stringValue === undefined) { + program.reportDiagnostics( + explainStringTemplateNotSerializable(stringTemplate).map((x) => ({ + ...x, + severity: "warning", + })) + ); return { type: "string" }; } - return { type: "string", enum: [value] }; + return { type: "string", enum: [stringTemplate.stringValue] }; } // Map an TypeSpec type to an OA schema. Returns undefined when the resulting // OA schema is just a regular object schema. diff --git a/packages/typespec-autorest/src/openapi2-document.ts b/packages/typespec-autorest/src/openapi2-document.ts index c87a8cd2dd..fba900eabd 100644 --- a/packages/typespec-autorest/src/openapi2-document.ts +++ b/packages/typespec-autorest/src/openapi2-document.ts @@ -110,7 +110,7 @@ export type JsonType = "array" | "boolean" | "integer" | "number" | "object" | " * Autorest allows a few properties to be next to $ref of a property. */ export type OpenAPI2SchemaRefProperty = Ref & - Pick & { + Pick & { /** * Provide a different name to be used in the client. */ From 5fb65715f041d5a4b6c9b33ce1eb41efe9dd2b90 Mon Sep 17 00:00:00 2001 From: Timothee Guerin Date: Thu, 9 May 2024 14:50:57 -0700 Subject: [PATCH 6/6] fix --- packages/typespec-autorest/src/openapi.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/typespec-autorest/src/openapi.ts b/packages/typespec-autorest/src/openapi.ts index 3e7dfb6ff4..81ae4ffd34 100644 --- a/packages/typespec-autorest/src/openapi.ts +++ b/packages/typespec-autorest/src/openapi.ts @@ -1690,7 +1690,6 @@ export async function getOpenAPIForService( function resolveProperty(prop: ModelProperty, context: SchemaContext): OpenAPI2SchemaProperty { let propSchema; - console.log("Test this is reported"); if (prop.type.kind === "Enum" && prop.defaultValue) { propSchema = getSchemaForEnum(prop.type); } else if (prop.type.kind === "Union" && prop.defaultValue) {