Skip to content

Commit

Permalink
Fix immutable arrays (#1643)
Browse files Browse the repository at this point in the history
  • Loading branch information
drwpow committed Apr 29, 2024
1 parent c7a930c commit 5b97fb8
Show file tree
Hide file tree
Showing 8 changed files with 2,150 additions and 2,112 deletions.
2 changes: 1 addition & 1 deletion packages/openapi-fetch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,6 @@
"openapi-typescript-fetch": "^1.1.3",
"superagent": "^9.0.0",
"typescript": "^5.4.5",
"vitest": "^1.5.0"
"vitest": "^1.5.2"
}
}
2,054 changes: 1,027 additions & 1,027 deletions packages/openapi-typescript/examples/github-api-export-type-immutable.ts

Large diffs are not rendered by default.

2,054 changes: 1,027 additions & 1,027 deletions packages/openapi-typescript/examples/github-api-immutable.ts

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions packages/openapi-typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"typescript": "^5.x"
},
"dependencies": {
"@redocly/openapi-core": "^1.11.0",
"@redocly/openapi-core": "^1.12.0",
"ansi-colors": "^4.1.3",
"supports-color": "^9.4.0",
"yargs-parser": "^21.1.1"
Expand All @@ -74,7 +74,7 @@
"esbuild": "^0.20.2",
"execa": "^7.2.0",
"typescript": "^5.4.5",
"vite-node": "^1.5.0",
"vitest": "^1.5.0"
"vite-node": "^1.5.2",
"vitest": "^1.5.2"
}
}
11 changes: 8 additions & 3 deletions packages/openapi-typescript/src/transform/schema-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,9 +323,14 @@ function transformSchemaObjectCore(schemaObject: SchemaObject, options: Transfor
}
}

return ts.isTupleTypeNode(itemType) || ts.isArrayTypeNode(itemType)
? itemType
: ts.factory.createArrayTypeNode(itemType); // wrap itemType in array type, but only if not a tuple or array already
const finalType =
ts.isTupleTypeNode(itemType) || ts.isArrayTypeNode(itemType)
? itemType
: ts.factory.createArrayTypeNode(itemType); // wrap itemType in array type, but only if not a tuple or array already

return options.ctx.immutable
? ts.factory.createTypeOperatorNode(ts.SyntaxKind.ReadonlyKeyword, finalType)
: finalType;
}

// polymorphic, or 3.1 nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,28 @@ describe("transformSchemaObject > array", () => {
{
given: {
type: "array",
items: { type: "array", items: { type: "string" } },
items: { type: "string" },
},
want: "string[][]",
want: "readonly string[]",
options: {
...DEFAULT_OPTIONS,
ctx: { ...DEFAULT_OPTIONS.ctx, immutable: true },
},
},
],
[
"options > immutable: true (tuple)",
{
given: {
type: "array",
items: { type: "number" },
prefixItems: [{ type: "number" }, { type: "number" }, { type: "number" }],
},
want: `readonly [
number,
number,
number
]`,
options: {
...DEFAULT_OPTIONS,
ctx: { ...DEFAULT_OPTIONS.ctx, immutable: true },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ describe("transformSchemaObject > object", () => {
},
},
want: `{
readonly array?: {
readonly array?: readonly {
readonly [key: string]: unknown;
}[] | null;
}`,
Expand Down
110 changes: 62 additions & 48 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5b97fb8

Please sign in to comment.