From b731a44f54f4384961f6155d2edd6051d555daf0 Mon Sep 17 00:00:00 2001 From: Patrick McElhaney Date: Mon, 6 May 2024 14:24:15 -0400 Subject: [PATCH] fix an issue with recursive types, fixes #890 --- .changeset/little-wolves-play.md | 5 ++++ openapi-example.yaml | 5 +++- src/typescript-generator/script.js | 2 +- test/typescript-generator/script.test.js | 29 ++++++++++++------------ 4 files changed, 25 insertions(+), 16 deletions(-) create mode 100644 .changeset/little-wolves-play.md diff --git a/.changeset/little-wolves-play.md b/.changeset/little-wolves-play.md new file mode 100644 index 00000000..e284866a --- /dev/null +++ b/.changeset/little-wolves-play.md @@ -0,0 +1,5 @@ +--- +"counterfact": patch +--- + +fix invalid TypeScript generated for recursive types diff --git a/openapi-example.yaml b/openapi-example.yaml index 63c2d91a..8f499cad 100644 --- a/openapi-example.yaml +++ b/openapi-example.yaml @@ -67,7 +67,7 @@ paths: content: application/json: schema: - type: string + $ref: "#/components/schemas/Recursive" "400": $ref: "#/components/responses/BadRequest" @@ -94,6 +94,9 @@ components: description: A detailed description of the error required: - message + Recursive: + oneOf: + - { $ref: "#/components/schemas/Recursive" } securitySchemes: basicAuth: diff --git a/src/typescript-generator/script.js b/src/typescript-generator/script.js index b537cb82..fcc39087 100644 --- a/src/typescript-generator/script.js +++ b/src/typescript-generator/script.js @@ -27,7 +27,7 @@ export class Script { firstUniqueName(coder) { for (const name of coder.names()) { - if (!this.imports.has(name) && !this.exports.has(name)) { + if (!this.imports.has(name)) { return name; } } diff --git a/test/typescript-generator/script.test.js b/test/typescript-generator/script.test.js index a64c9180..5e092eb9 100644 --- a/test/typescript-generator/script.test.js +++ b/test/typescript-generator/script.test.js @@ -172,14 +172,9 @@ describe("a Script", () => { it("creates export statements", async () => { const repository = new Repository("/base/path"); - class CoderThatWantsToImportAccount extends Coder { + class AccountCoder extends Coder { *names() { - let index = 0; - - while (true) { - yield `Account${index}`; - index += 1; - } + yield "Account"; } write() { @@ -187,20 +182,26 @@ describe("a Script", () => { } } - const coder = new CoderThatWantsToImportAccount({}); + class AccountTypeCoder extends Coder { + *names() { + yield "AccountType"; + } + + write() { + return "{ }"; + } + } const script = repository.get("export-to-me.ts"); - script.export(coder); - script.exportType(coder); - script.exportDefault(coder); + script.export(new AccountCoder({})); + script.exportType(new AccountTypeCoder({})); await script.finished(); expect(script.exportStatements()).toStrictEqual([ - "export const Account0 = { };", - "export type Account1 = { };", - "export default { };", + "export const Account = { };", + "export type AccountType = { };", ]); });