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

Updates tsp-openapi3 to emit main.tsp even when formatter fails #3794

Merged
merged 3 commits into from
Jul 10, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@typespec/openapi3"
---

Updates tsp-openapi3 to always emit main.tsp when formatting encounters an error.
19 changes: 17 additions & 2 deletions packages/openapi3/src/cli/actions/convert/convert-file.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import oaParser from "@readme/openapi-parser";
import { resolvePath } from "@typespec/compiler";
import { formatTypeSpec, resolvePath } from "@typespec/compiler";
import { OpenAPI3Document } from "../../../types.js";
import { CliHost } from "../../types.js";
import { handleInternalCompilerError } from "../../utils.js";
Expand All @@ -14,15 +14,30 @@ export async function convertAction(host: CliHost, args: ConvertCliArgs) {
const program = transform(model);
let mainTsp: string;
try {
mainTsp = await generateMain(program);
mainTsp = generateMain(program);
} catch (err) {
handleInternalCompilerError(err);
}

let formatError;
// attempt to format the TSP and track if it threw an error
try {
mainTsp = await formatTypeSpec(mainTsp, {
printWidth: 100,
tabWidth: 2,
});
} catch (err) {
formatError = err;
}

if (args["output-dir"]) {
await host.mkdirp(args["output-dir"]);
await host.writeFile(resolvePath(args["output-dir"], "main.tsp"), mainTsp);
}

if (formatError) {
handleInternalCompilerError(formatError);
}
}

function parseOpenApiFile(path: string): Promise<OpenAPI3Document> {
Expand Down
11 changes: 10 additions & 1 deletion packages/openapi3/src/cli/actions/convert/convert.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import { formatTypeSpec } from "@typespec/compiler";
import { OpenAPI3Document } from "../../../types.js";
import { generateMain } from "./generators/generate-main.js";
import { transform } from "./transforms/transforms.js";

export async function convertOpenAPI3Document(document: OpenAPI3Document) {
const program = transform(document);
return await generateMain(program);
const content = generateMain(program);
try {
return await formatTypeSpec(content, {
printWidth: 100,
tabWidth: 2,
});
} catch {
return content;
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { formatTypeSpec } from "@typespec/compiler";
import { TypeSpecProgram } from "../interfaces.js";
import { generateModel } from "./generate-model.js";
import { generateOperation } from "./generate-operation.js";
import { generateServiceInformation } from "./generate-service-info.js";

export async function generateMain(program: TypeSpecProgram): Promise<string> {
const content = `
export function generateMain(program: TypeSpecProgram): string {
return `
import "@typespec/http";
import "@typespec/openapi";
import "@typespec/openapi3";
Expand All @@ -19,9 +18,4 @@ export async function generateMain(program: TypeSpecProgram): Promise<string> {

${program.operations.map(generateOperation).join("\n\n")}
`;

return formatTypeSpec(content, {
printWidth: 100,
tabWidth: 2,
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { formatTypeSpec } from "@typespec/compiler";
import { strictEqual } from "node:assert";
import { it } from "vitest";
import { convertOpenAPI3Document } from "../../src/index.js";

it("should convert an OpenAPI3 document to a formatted TypeSpec program", async () => {
const tsp = await convertOpenAPI3Document({
info: {
title: "Test Service",
version: "1.0.0",
},
openapi: "3.0.0",
paths: {},
components: {
schemas: {
Foo: {
type: "string",
},
},
},
});

strictEqual(
tsp,
await formatTypeSpec(
`
import "@typespec/http";
import "@typespec/openapi";
import "@typespec/openapi3";

using Http;
using OpenAPI;

@service({
title: "Test Service",
})
@info({
version: "1.0.0",
})
namespace TestService;

scalar Foo extends string;
`,
{ printWidth: 100, tabWidth: 2 }
)
);
});
Loading