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

fix(openapi-typescript): correct generated type when using prefixItems #1893

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 1 addition & 2 deletions docs/advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,7 @@ minItems: 2

```yaml
type: array
items:
type: number
items: false
prefixItems:
- number
- number
Expand Down
3 changes: 1 addition & 2 deletions docs/ja/advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,7 @@ minItems: 2

```yaml
type: array
items:
type: number
items: false
prefixItems:
- number
- number
Expand Down
3 changes: 1 addition & 2 deletions docs/zh/advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,7 @@ minItems: 2

```yaml
type: array
items:
type: number
items: false
prefixItems:
- number
- number
Expand Down
45 changes: 42 additions & 3 deletions packages/openapi-typescript/src/transform/schema-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,11 +302,50 @@ function transformSchemaObjectCore(schemaObject: SchemaObject, options: Transfor
if (schemaObject.type === "array") {
// default to `unknown[]`
let itemType: ts.TypeNode = UNKNOWN;
// tuple type
if (schemaObject.prefixItems || Array.isArray(schemaObject.items)) {
const prefixItems = schemaObject.prefixItems ?? (schemaObject.items as (SchemaObject | ReferenceObject)[]);
// tuple type using deprecated syntax
if (Array.isArray(schemaObject.items)) {
const prefixItems = schemaObject.items;
itemType = ts.factory.createTupleTypeNode(prefixItems.map((item) => transformSchemaObject(item, options)));
}
// standard tuple type
else if (schemaObject.prefixItems) {
const prefixItems = schemaObject.prefixItems;
itemType = ts.factory.createTupleTypeNode(prefixItems.map((item) => transformSchemaObject(item, options)));
if (schemaObject.items !== undefined) {
if (schemaObject.items !== false) {
let additionalItemType: ts.TypeNode = UNKNOWN;
if ("type" in schemaObject.items) {
additionalItemType = ts.factory.createArrayTypeNode(transformSchemaObject(schemaObject.items, options));
} else {
additionalItemType = transformSchemaObject(schemaObject.items, options);
}
itemType = ts.factory.createTupleTypeNode([
...(itemType as ts.TupleTypeNode).elements,
ts.factory.createRestTypeNode(additionalItemType),
]);
}
} else if (schemaObject.unevaluatedItems !== undefined) {
if (schemaObject.unevaluatedItems !== false) {
let additionalItemType: ts.TypeNode = UNKNOWN;
if ("type" in schemaObject.unevaluatedItems) {
additionalItemType = ts.factory.createArrayTypeNode(
transformSchemaObject(schemaObject.unevaluatedItems, options),
);
} else {
additionalItemType = transformSchemaObject(schemaObject.unevaluatedItems, options);
}
itemType = ts.factory.createTupleTypeNode([
...(itemType as ts.TupleTypeNode).elements,
ts.factory.createRestTypeNode(additionalItemType),
]);
}
} else {
itemType = ts.factory.createTupleTypeNode([
...(itemType as ts.TupleTypeNode).elements,
ts.factory.createRestTypeNode(ts.factory.createArrayTypeNode(UNKNOWN)),
]);
}
}
// standard array type
else if (schemaObject.items) {
if ("type" in schemaObject.items && schemaObject.items.type === "array") {
Expand Down
3 changes: 2 additions & 1 deletion packages/openapi-typescript/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,8 @@ export interface IntegerSubtype {
export interface ArraySubtype {
type: "array" | ["array", "null"];
prefixItems?: (SchemaObject | ReferenceObject)[];
items?: SchemaObject | ReferenceObject | (SchemaObject | ReferenceObject)[];
items?: false | SchemaObject | ReferenceObject | (SchemaObject | ReferenceObject)[];
unevaluatedItems?: false | SchemaObject | ReferenceObject;
minItems?: number;
maxItems?: number;
enum?: (SchemaObject | ReferenceObject)[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe("transformSchemaObject > array", () => {
},
],
[
"tuple > prefixItems",
"tuple > prefixItems (open tuple using items)",
{
given: {
type: "array",
Expand All @@ -45,6 +45,72 @@ describe("transformSchemaObject > array", () => {
want: `[
number,
number,
number,
...number[]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This other modification to an existing test may also technically be a breaking change, but I think because it’s purely additive, could be considered a bugfix. I would be OK with this going in as-is.

]`,
// options: DEFAULT_OPTIONS,
},
],
[
"tuple > prefixItems (closed tuple using unevaluatedItems)",
{
given: {
type: "array",
unevaluatedItems: { type: "number" },
prefixItems: [{ type: "number" }, { type: "number" }, { type: "number" }],
},
want: `[
number,
number,
number,
...number[]
]`,
// options: DEFAULT_OPTIONS,
},
],
[
"tuple > prefixItems (open tuple not specified)",
{
given: {
type: "array",
prefixItems: [{ type: "number" }, { type: "number" }, { type: "number" }],
},
want: `[
number,
number,
number,
...unknown[]
]`,
// options: DEFAULT_OPTIONS,
},
],
[
"tuple > prefixItems (closed tuple using items)",
{
given: {
type: "array",
items: false,
prefixItems: [{ type: "number" }, { type: "number" }, { type: "number" }],
},
want: `[
number,
number,
number
]`,
// options: DEFAULT_OPTIONS,
},
],
[
"tuple > prefixItems (closed tuple using unevaluatedItems)",
{
given: {
type: "array",
unevaluatedItems: false,
prefixItems: [{ type: "number" }, { type: "number" }, { type: "number" }],
},
want: `[
number,
number,
number
]`,
// options: DEFAULT_OPTIONS,
Expand Down Expand Up @@ -179,7 +245,7 @@ describe("transformSchemaObject > array", () => {
{
given: {
type: "array",
items: { type: "number" },
items: false,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This specifically is the breaking change I’m referring to—we’ll need to ensure that the original test case of items: { type: "number" } stays tested, and it generates the same output. While we’re on version 7.x this can’t change (and we can revisit it when it’s time to release 8.x).

In general, modifying an original test is a good hint that it may be a breaking change.

prefixItems: [{ type: "number" }, { type: "number" }, { type: "number" }],
},
want: `readonly [
Expand Down