Skip to content

Commit

Permalink
revert changhes in openapi
Browse files Browse the repository at this point in the history
  • Loading branch information
Ding Wang committed Nov 17, 2023
1 parent 7cca21c commit 6832fab
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 108 deletions.
47 changes: 15 additions & 32 deletions packages/openapi3/src/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Record<string, any>, OpenAPI3EmitterOptions>) {
Expand All @@ -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),
},
Expand Down Expand Up @@ -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() });
}
Expand All @@ -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,
});
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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,
});
Expand Down Expand Up @@ -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)
) {
Expand Down
28 changes: 3 additions & 25 deletions packages/openapi3/test/output-file.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,14 @@ describe("openapi3: output file", () => {
beforeEach(async () => {
runner = await createOpenAPITestRunner();
});
async function compileOpenAPI(
options: OpenAPI3EmitterOptions,
code: string = "",
miscOptions: Record<string, unknown> = {}
): Promise<void> {
async function compileOpenAPI(options: OpenAPI3EmitterOptions, code: string = ""): Promise<void> {
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(
Expand Down Expand Up @@ -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<string, unknown> = { "canonicalized-version": "true" };

async () => {
await compileOpenAPI(
{},
`
@versioned(Versions) namespace Service1 {
enum Versions {v1, v2}
}
`,
miscOptions
);
expectHasOutput(`CanonicalizedVersion.${fileType}`);
};
});
});
});
});
Expand Down
10 changes: 3 additions & 7 deletions packages/openapi3/test/test-host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown> = {}
options: OpenAPI3EmitterOptions = {}
) {
const host = await createOpenAPITestHost();
const outPath = resolveVirtualPath("{version}.openapi.json");
Expand All @@ -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) {
Expand Down
45 changes: 1 addition & 44 deletions packages/openapi3/test/versioning.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ describe("openapi3: versioning", () => {
@added(Versions.v2)
model Widget {
@key id: string;
@key id: string;
}
@error
Expand Down Expand Up @@ -249,47 +249,4 @@ describe("openapi3: versioning", () => {
`);
});
});

describe("canonicalized version", () => {
it("works with models", async () => {
const miscOptions: Record<string, unknown> = { "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"],
});
});
});
});

0 comments on commit 6832fab

Please sign in to comment.