Skip to content

Commit

Permalink
fix an issue with recursive types, fixes #890
Browse files Browse the repository at this point in the history
  • Loading branch information
pmcelhaney committed May 6, 2024
1 parent c4b911e commit b731a44
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/little-wolves-play.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"counterfact": patch
---

fix invalid TypeScript generated for recursive types
5 changes: 4 additions & 1 deletion openapi-example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ paths:
content:
application/json:
schema:
type: string
$ref: "#/components/schemas/Recursive"
"400":
$ref: "#/components/responses/BadRequest"

Expand All @@ -94,6 +94,9 @@ components:
description: A detailed description of the error
required:
- message
Recursive:
oneOf:
- { $ref: "#/components/schemas/Recursive" }

securitySchemes:
basicAuth:
Expand Down
2 changes: 1 addition & 1 deletion src/typescript-generator/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
29 changes: 15 additions & 14 deletions test/typescript-generator/script.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,35 +172,36 @@ 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() {
return "{ }";
}
}

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 = { };",
]);
});

Expand Down

0 comments on commit b731a44

Please sign in to comment.