diff --git a/.changeset/dark-toes-live.md b/.changeset/dark-toes-live.md new file mode 100644 index 000000000..7432aa750 --- /dev/null +++ b/.changeset/dark-toes-live.md @@ -0,0 +1,5 @@ +--- +"swagger-typescript-api": patch +--- + +Add support for generating tuple types from prefixItems array schemas diff --git a/src/schema-parser/base-schema-parsers/array.ts b/src/schema-parser/base-schema-parsers/array.ts index ae84eef7c..aaa281bfb 100644 --- a/src/schema-parser/base-schema-parsers/array.ts +++ b/src/schema-parser/base-schema-parsers/array.ts @@ -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) { const tupleContent = []; - for (const item of items) { + for (const item of tupleItems) { tupleContent.push( this.schemaParserFabric .createSchemaParser({ schema: item, schemaPath: this.schemaPath }) diff --git a/tests/spec/prefixItems-array/__snapshots__/basic.test.ts.snap b/tests/spec/prefixItems-array/__snapshots__/basic.test.ts.snap new file mode 100644 index 000000000..8a4e98d9b --- /dev/null +++ b/tests/spec/prefixItems-array/__snapshots__/basic.test.ts.snap @@ -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]; +" +`; diff --git a/tests/spec/prefixItems-array/basic.test.ts b/tests/spec/prefixItems-array/basic.test.ts new file mode 100644 index 000000000..1301e0195 --- /dev/null +++ b/tests/spec/prefixItems-array/basic.test.ts @@ -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(); + }); +}); diff --git a/tests/spec/prefixItems-array/schema.yaml b/tests/spec/prefixItems-array/schema.yaml new file mode 100644 index 000000000..371de559a --- /dev/null +++ b/tests/spec/prefixItems-array/schema.yaml @@ -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