Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/dark-toes-live.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"swagger-typescript-api": patch
---

Add support for generating tuple types from prefixItems array schemas
12 changes: 9 additions & 3 deletions src/schema-parser/base-schema-parsers/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@ import { MonoSchemaParser } from "../mono-schema-parser.js";
export class ArraySchemaParser extends MonoSchemaParser {
override parse() {
let contentType;
const { type, description, items } = this.schema || {};
const { type, description, items, prefixItems } = this.schema || {};

if (Array.isArray(items) && type === SCHEMA_TYPES.ARRAY) {
const tupleItems = Array.isArray(prefixItems)
? prefixItems
: Array.isArray(items)
? items
: null;

if (tupleItems && type === SCHEMA_TYPES.ARRAY) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2: When prefixItems is combined with items (or omitted, implying an open tuple), the generated type is always a fixed-length closed tuple [A, B, C], ignoring the items schema that should describe trailing/rest elements (e.g. [A, B, ...string[]]) or the fact that extra items of any type are allowed by default. Consider appending a rest element derived from items when it's present alongside prefixItems.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At src/schema-parser/base-schema-parsers/array.ts, line 15:

<comment>When `prefixItems` is combined with `items` (or omitted, implying an open tuple), the generated type is always a fixed-length closed tuple `[A, B, C]`, ignoring the `items` schema that should describe trailing/rest elements (e.g. `[A, B, ...string[]]`) or the fact that extra items of any type are allowed by default. Consider appending a rest element derived from `items` when it's present alongside `prefixItems`.</comment>

<file context>
@@ -4,11 +4,17 @@ import { MonoSchemaParser } from "../mono-schema-parser.js";
+        ? items
+        : null;
+
+    if (tupleItems && type === SCHEMA_TYPES.ARRAY) {
       const tupleContent = [];
-      for (const item of items) {
</file context>

const tupleContent = [];
for (const item of items) {
for (const item of tupleItems) {
tupleContent.push(
this.schemaParserFabric
.createSchemaParser({ schema: item, schemaPath: this.schemaPath })
Expand Down
22 changes: 22 additions & 0 deletions tests/spec/prefixItems-array/__snapshots__/basic.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`basic > prefixItems-array 1`] = `
"/* eslint-disable */
/* tslint:disable */
// @ts-nocheck
/*
* ---------------------------------------------------------------
* ## THIS FILE WAS GENERATED VIA SWAGGER-TYPESCRIPT-API ##
* ## ##
* ## AUTHOR: acacode ##
* ## SOURCE: https://github.com/acacode/swagger-typescript-api ##
* ---------------------------------------------------------------
*/

/**
* @maxItems 3
* @minItems 3
*/
export type PrefixItemsArrayType = [number, boolean, string];
"
`;
34 changes: 34 additions & 0 deletions tests/spec/prefixItems-array/basic.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import * as fs from "node:fs/promises";
import * as os from "node:os";
import * as path from "node:path";
import { afterAll, beforeAll, describe, expect, test } from "vitest";
import { generateApi } from "../../../src/index.js";

describe("basic", async () => {
let tmpdir = "";

beforeAll(async () => {
tmpdir = await fs.mkdtemp(path.join(os.tmpdir(), "swagger-typescript-api"));
});

afterAll(async () => {
await fs.rm(tmpdir, { recursive: true });
});

test("prefixItems-array", async () => {
await generateApi({
fileName: "schema",
input: path.resolve(import.meta.dirname, "schema.yaml"),
output: tmpdir,
silent: true,
anotherArrayType: true,
generateClient: false,
});

const content = await fs.readFile(path.join(tmpdir, "schema.ts"), {
encoding: "utf8",
});

expect(content).toMatchSnapshot();
});
});
14 changes: 14 additions & 0 deletions tests/spec/prefixItems-array/schema.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
openapi: 3.1.0
info:
title: test
version: 0.0.0
components:
schemas:
PrefixItemsArrayType:
type: array
prefixItems:
- type: number
- type: boolean
- type: string
maxItems: 3
minItems: 3