Skip to content

Commit

Permalink
fix: tuples type generation support (#1011) (#1082)
Browse files Browse the repository at this point in the history
  • Loading branch information
liangskyli committed Apr 25, 2023
1 parent aa963bb commit c2c186e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
16 changes: 13 additions & 3 deletions src/transform/schema-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,19 @@ export function defaultSchemaObjectTransform(
// "type": "array"
if (schemaObject.type === "array") {
indentLv++;
let itemType = schemaObject.items
? transformSchemaObject(schemaObject.items, { path, ctx: { ...ctx, indentLv } })
: "unknown";
let itemType = "unknown";
if (schemaObject.items) {
if (Array.isArray(schemaObject.items)) {
// tuple type support
const result: string[] = [];
schemaObject.items.forEach((item) => {
result.push(transformSchemaObject(item, { path, ctx: { ...ctx, indentLv } }));
});
itemType = `[${result.join(",")}]`;
} else {
itemType = transformSchemaObject(schemaObject.items, { path, ctx: { ...ctx, indentLv } });
}
}
const minItems: number =
typeof schemaObject.minItems === "number" && schemaObject.minItems >= 0 ? schemaObject.minItems : 0;
const maxItems: number | undefined =
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ export type SchemaObject = {
| {
type: "array";
prefixItems?: SchemaObject | ReferenceObject;
items?: SchemaObject | ReferenceObject;
items?: SchemaObject | ReferenceObject | (SchemaObject | ReferenceObject)[];
minItems?: number;
maxItems?: number;
}
Expand Down
6 changes: 6 additions & 0 deletions test/schema-object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ describe("Schema Object", () => {
expect(generated).toBe("(string)[]");
});

test("tuple array", () => {
const schema: SchemaObject = { type: "array", items: [{ type: "string" },{"type": "number"}],minItems:2,maxItems:2 };
const generated = transformSchemaObject(schema, options);
expect(generated).toBe("([string,number])[]");
});

test("ref", () => {
const schema: SchemaObject = { type: "array", items: { $ref: 'components["schemas"]["ArrayItem"]' } };
const generated = transformSchemaObject(schema, options);
Expand Down

0 comments on commit c2c186e

Please sign in to comment.