From 6832fab50ed9bee9543903582d60ae960ba47d94 Mon Sep 17 00:00:00 2001 From: Ding Wang Date: Fri, 17 Nov 2023 13:05:42 -0800 Subject: [PATCH] revert changhes in openapi --- packages/openapi3/src/openapi.ts | 47 +++++++--------------- packages/openapi3/test/output-file.test.ts | 28 ++----------- packages/openapi3/test/test-host.ts | 10 ++--- packages/openapi3/test/versioning.test.ts | 45 +-------------------- 4 files changed, 22 insertions(+), 108 deletions(-) diff --git a/packages/openapi3/src/openapi.ts b/packages/openapi3/src/openapi.ts index 15d6cbb5cb..4d0269e871 100644 --- a/packages/openapi3/src/openapi.ts +++ b/packages/openapi3/src/openapi.ts @@ -205,12 +205,17 @@ function createOAPIEmitter( return { emitOpenAPI }; - function initializeEmitter(service: Service, version?: string, canonicalizedVersion = false) { + function initializeEmitter(service: Service, version?: string) { metadataInfo = createMetadataInfo(program, { canonicalVisibility: Visibility.Read, canShareProperty: (p) => isReadonlyProperty(program, p), }); - visibilityUsage = resolveVisibilityUsage(program, metadataInfo, service.type); + visibilityUsage = resolveVisibilityUsage( + program, + metadataInfo, + service.type, + options.omitUnreachableTypes + ); schemaEmitter = context.getAssetEmitter( class extends OpenAPI3SchemaEmitter { constructor(emitter: AssetEmitter, OpenAPI3EmitterOptions>) { @@ -225,7 +230,7 @@ function createOAPIEmitter( openapi: "3.0.0", info: { title: service.title ?? "(title)", - version: canonicalizedVersion ? "0000-00-00" : version ?? service.version ?? "0000-00-00", + version: version ?? service.version ?? "0000-00-00", description: getDoc(program, service.type), ...getInfo(program, service.type), }, @@ -324,19 +329,8 @@ function createOAPIEmitter( }); } - function checkOptionCanonicaliedVersion(program: Program): boolean { - if (program.getOption("canonicalized-version") === "true") { - return true; - } - - return false; - } - async function emitOpenAPI() { const services = listServices(program); - - const canonicalizedVersion = checkOptionCanonicaliedVersion(program); - if (services.length === 0) { services.push({ type: program.getGlobalNamespaceType() }); } @@ -363,26 +357,16 @@ function createOAPIEmitter( ? { type: projectedProgram.getGlobalNamespaceType() } : getService(program, projectedServiceNs)!, services.length > 1, - record.version, - canonicalizedVersion + record.version ); - - if (canonicalizedVersion) { - break; - } } } } - function resolveOutputFile( - service: Service, - multipleService: boolean, - version?: string, - canonicalizedVersion = false - ): string { + function resolveOutputFile(service: Service, multipleService: boolean, version?: string): string { return interpolatePath(options.outputFile, { "service-name": multipleService ? getNamespaceFullName(service.type) : undefined, - version: canonicalizedVersion ? "CanonicalizedVersion" : version, + version, }); } @@ -625,10 +609,9 @@ function createOAPIEmitter( async function emitOpenAPIFromVersion( service: Service, multipleService: boolean, - version?: string, - canonicalizedVersion = false + version?: string ) { - initializeEmitter(service, version, canonicalizedVersion); + initializeEmitter(service, version); try { const httpService = ignoreDiagnostics(getHttpService(program, service.type)); reportIfNoRoutes(program, httpService.operations); @@ -657,7 +640,7 @@ function createOAPIEmitter( // Write out the OpenAPI document to the output path await emitFile(program, { - path: resolveOutputFile(service, multipleService, version, canonicalizedVersion), + path: resolveOutputFile(service, multipleService, version), content: serializeDocument(root, options.fileType), newLine: options.newLine, }); @@ -1338,7 +1321,7 @@ function createOAPIEmitter( function processUnreferencedSchemas() { const addSchema = (type: Type) => { if ( - visibilityUsage.getUsage(type) === undefined && + visibilityUsage.isUnreachable(type) && !paramModels.has(type) && !shouldInline(program, type) ) { diff --git a/packages/openapi3/test/output-file.test.ts b/packages/openapi3/test/output-file.test.ts index 1918bf8565..be129deda2 100644 --- a/packages/openapi3/test/output-file.test.ts +++ b/packages/openapi3/test/output-file.test.ts @@ -39,19 +39,14 @@ describe("openapi3: output file", () => { beforeEach(async () => { runner = await createOpenAPITestRunner(); }); - async function compileOpenAPI( - options: OpenAPI3EmitterOptions, - code: string = "", - miscOptions: Record = {} - ): Promise { + async function compileOpenAPI(options: OpenAPI3EmitterOptions, code: string = ""): Promise { const diagnostics = await runner.diagnose(code, { noEmit: false, emit: ["@typespec/openapi3"], options: { "@typespec/openapi3": { ...options, "emitter-output-dir": outputDir } }, - miscOptions, }); - expectDiagnosticEmpty(diagnostics.filter((x) => x.code !== "@typespec/http/no-routes")); + expectDiagnosticEmpty(diagnostics); } function expectOutput( @@ -134,27 +129,10 @@ describe("openapi3: output file", () => { ` ); - expectHasOutput(`custom.v1231.${fileType}`); + expectHasOutput(`custom.v1.${fileType}`); expectHasOutput(`custom.v2.${fileType}`); }; }); - - it("create merged file for canonicalized version", async () => { - const miscOptions: Record = { "canonicalized-version": "true" }; - - async () => { - await compileOpenAPI( - {}, - ` - @versioned(Versions) namespace Service1 { - enum Versions {v1, v2} - } - `, - miscOptions - ); - expectHasOutput(`CanonicalizedVersion.${fileType}`); - }; - }); }); }); }); diff --git a/packages/openapi3/test/test-host.ts b/packages/openapi3/test/test-host.ts index 273e659b7c..c238e3695a 100644 --- a/packages/openapi3/test/test-host.ts +++ b/packages/openapi3/test/test-host.ts @@ -53,14 +53,13 @@ export async function diagnoseOpenApiFor(code: string, options: OpenAPI3EmitterO emit: ["@typespec/openapi3"], options: { "@typespec/openapi3": options as any }, }); - return diagnostics.filter((x) => x.code !== "@typespec/http/no-routes"); + return diagnostics; } export async function openApiFor( code: string, versions?: string[], - options: OpenAPI3EmitterOptions = {}, - miscOptions: Record = {} + options: OpenAPI3EmitterOptions = {} ) { const host = await createOpenAPITestHost(); const outPath = resolveVirtualPath("{version}.openapi.json"); @@ -74,14 +73,11 @@ export async function openApiFor( noEmit: false, emit: ["@typespec/openapi3"], options: { "@typespec/openapi3": { ...options, "output-file": outPath } }, - miscOptions, }); - expectDiagnosticEmpty(diagnostics.filter((x) => x.code !== "@typespec/http/no-routes")); + expectDiagnosticEmpty(diagnostics); if (!versions) { return JSON.parse(host.fs.get(resolveVirtualPath("openapi.json"))!); - } else if (miscOptions["canonicalized-version"] === "true") { - return JSON.parse(host.fs.get(resolveVirtualPath("CanonicalizedVersion.openapi.json"))!); } else { const output: any = {}; for (const version of versions) { diff --git a/packages/openapi3/test/versioning.test.ts b/packages/openapi3/test/versioning.test.ts index 42f20ff074..985e6eaaf3 100644 --- a/packages/openapi3/test/versioning.test.ts +++ b/packages/openapi3/test/versioning.test.ts @@ -195,7 +195,7 @@ describe("openapi3: versioning", () => { @added(Versions.v2) model Widget { - @key id: string; + @key id: string; } @error @@ -249,47 +249,4 @@ describe("openapi3: versioning", () => { `); }); }); - - describe("canonicalized version", () => { - it("works with models", async () => { - const miscOptions: Record = { "canonicalized-version": "true" }; - - const Test = await openApiFor( - ` - @versioned(Versions) - @service({title: "My Service"}) - namespace MyService { - enum Versions { - "v1", - "v2", - "v3"} - model Test { - prop1: string; - @added(Versions.v2) prop2: string; - @removed(Versions.v2) prop3: string; - @madeOptional(Versions.v3) prop4?: string; - } - - - } - `, - ["v1", "v2", "v3"], - {}, - miscOptions - ); - - strictEqual(Test.info.version, "0000-00-00"); - - deepStrictEqual(Test.components.schemas.Test, { - type: "object", - properties: { - prop1: { type: "string" }, - prop2: { type: "string" }, - prop3: { type: "string" }, - prop4: { type: "string" }, - }, - required: ["prop1", "prop2", "prop3", "prop4"], - }); - }); - }); });