diff --git a/src/schema.ts b/src/schema.ts index cb4c40d..bf944e6 100644 --- a/src/schema.ts +++ b/src/schema.ts @@ -1,4 +1,5 @@ import { + Oas3_1Schema, Oas3Operation, Oas3Parameter, Oas3RequestBody, @@ -8,8 +9,10 @@ import { import { get } from "lodash-es" import { Context } from "./config" +export type OAS3 = Oas3Schema | Oas3_1Schema + // todo: wrong typing -export const unref = ( +export const unref = ( ctx: Context, s?: Referenced, ) => { diff --git a/src/type-gen.ts b/src/type-gen.ts index c57051a..31d8a66 100644 --- a/src/type-gen.ts +++ b/src/type-gen.ts @@ -3,7 +3,7 @@ import { filterEmpty } from "array-utils-ts" import { isArray, uniq, upperFirst } from "lodash-es" import ts from "typescript" import { Context } from "./config" -import { unref } from "./schema" +import { OAS3, unref } from "./schema" const f = ts.factory @@ -35,7 +35,7 @@ export const normalizeIdentifier = (val: string, asVar = false) => { return name } -const makeInlineEnum = (s: Oas3Schema) => { +const makeInlineEnum = (s: OAS3) => { if (!s.enum) return undefined const values = filterEmpty(s.enum) @@ -68,7 +68,7 @@ const makeInlineEnum = (s: Oas3Schema) => { return undefined } -export const makeType = (ctx: Context, s?: Referenced): ts.TypeNode => { +export const makeType = (ctx: Context, s?: Referenced): ts.TypeNode => { const mk = makeType.bind(null, ctx) if (s === undefined) return f.createKeywordTypeNode(ts.SyntaxKind.VoidKeyword) @@ -112,7 +112,7 @@ export const makeType = (ctx: Context, s?: Referenced): ts.TypeNode if ("type" in s) { // openapi v3.1 can have type as array if (Array.isArray(s.type)) { - const types: Oas3Schema[] = [] + const types: OAS3[] = [] for (const type of s.type) { if (type === "null") types.push({ type: "null" }) else types.push({ ...s, type }) diff --git a/test/type-gen.test.ts b/test/type-gen.test.ts index 2bfbe0d..02aa625 100644 --- a/test/type-gen.test.ts +++ b/test/type-gen.test.ts @@ -40,7 +40,6 @@ test("type inline", async () => { t({ oneOf: [{ type: "string" }, { type: "number" }] }, "string | number") t({ anyOf: [{ type: "string" }, { type: "number" }] }, "string | number") t({ allOf: [{ type: "string" }, { type: "number" }] }, "string & number") - // @ts-expect-error https://github.com/Redocly/redocly-cli/pull/1362 t({ type: ["string", "number"] }, "string | number") // arrays of basic types @@ -51,7 +50,6 @@ test("type inline", async () => { t({ type: "array", items: { type: "null" } }, "null[]") t({ type: "array", items: { type: "object" } }, "object[]") // should be unknown? t({ type: "array", items: { type: "file" } }, "unknown[]") - // @ts-expect-error https://github.com/Redocly/redocly-cli/pull/1362 t({ type: "array", items: { type: ["string", "number"] } }, "(string | number)[]") // arrays of arrays