Skip to content

Fix immutable arrays #1643

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

Merged
merged 1 commit into from
Apr 29, 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
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.

Loading