Skip to content

Commit 55a7d17

Browse files
committed
deps: update
1 parent 72b56f6 commit 55a7d17

File tree

6 files changed

+385
-378
lines changed

6 files changed

+385
-378
lines changed

Makefile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1-
.PHONY: test-ts test-ts-matrix clean
1+
.PHONY: test test-ts test-matrix-ts clean
22

33
default:
44
yarn ci
55

6+
test:
7+
yarn test
8+
69
test-ts:
710
@echo "-- $(v) --------------------"
811
$(eval name=apigen_ts$(v))
912
@docker -l warning build -f Dockerfile.test --build-arg TS_VER=$(v) -t $(name) .
1013
@docker run $(name)
1114

12-
test-ts-matrix:
15+
test-matrix-ts:
1316
@make test-ts v=5.0.4
1417
@make test-ts v=5.1.6
1518
@make test-ts v=5.2.2
@@ -18,6 +21,7 @@ test-ts-matrix:
1821
@make test-ts v=5.5.4
1922
@make test-ts v=5.6.3
2023
@make test-ts v=5.7.3
24+
@make test-ts v=5.8.3
2125
@make test-ts v=next
2226

2327
clean:

package.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,23 @@
2222
"ci": "tsc --noEmit && yarn test-cov && yarn build"
2323
},
2424
"dependencies": {
25-
"@redocly/openapi-core": "1.27.2",
25+
"@redocly/openapi-core": "1.34.1",
2626
"@types/lodash-es": "4.17.12",
2727
"@types/swagger2openapi": "7.0.4",
2828
"array-utils-ts": "1.0.2",
29-
"cleye": "1.3.2",
29+
"cleye": "1.3.4",
3030
"lodash-es": "4.17.21",
3131
"swagger2openapi": "7.0.8"
3232
},
3333
"devDependencies": {
34-
"@types/node": "22.10.7",
34+
"@types/node": "22.14.0",
3535
"c8": "10.1.3",
36-
"fetch-mock": "12.2.0",
37-
"pkgroll": "2.6.1",
38-
"prettier": "3.4.2",
36+
"fetch-mock": "12.5.2",
37+
"pkgroll": "2.12.1",
38+
"prettier": "3.5.3",
3939
"prettier-plugin-organize-imports": "4.1.0",
4040
"tsm": "2.3.0",
41-
"typescript": "5.7.3",
41+
"typescript": "5.8.3",
4242
"uvu": "0.5.6"
4343
},
4444
"peerDependencies": {

src/schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export const getReqSchema = (ctx: Context, config: Oas3Operation) => {
6363
return undefined
6464
}
6565

66-
export const getRepSchema = (ctx: Context, config: Oas3Operation): Oas3Schema | undefined => {
66+
export const getRepSchema = (ctx: Context, config: Oas3Operation): OAS3 | undefined => {
6767
const successCodes = Object.keys(config.responses ?? {})
6868
.filter((x) => x.startsWith("2"))
6969
.filter((x) => get(config, ["responses", x, "content"]))

src/type-gen.ts

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ export const normalizeIdentifier = (val: string, asVar = false) => {
3838
const makeInlineEnum = (s: OAS3) => {
3939
if (!s.enum) return undefined
4040

41-
const values = filterEmpty(s.enum)
41+
// https://swagger.io/docs/specification/v3_0/data-models/enums/
42+
const values = filterEmpty(s.enum) as Array<string | number | boolean>
4243
if (!values.length) return undefined
4344

4445
if (!s.type) {
@@ -53,7 +54,7 @@ const makeInlineEnum = (s: OAS3) => {
5354
}
5455

5556
if (s.type === "number") {
56-
const tokens = uniq(values).map((x) => f.createNumericLiteral(x))
57+
const tokens = uniq(values).map((x) => f.createNumericLiteral(x as number))
5758
return f.createUnionTypeNode(tokens.map((x) => f.createLiteralTypeNode(x)))
5859
}
5960

@@ -81,6 +82,13 @@ const makeObject = (ctx: Context, s: OAS3): ts.TypeNode => {
8182
return f.createKeywordTypeNode(ts.SyntaxKind.ObjectKeyword)
8283
}
8384

85+
const makeLiteralUnion = (ctx: Context, types: string[]): ts.TypeNode => {
86+
const tokens = types.map((x) => makeType(ctx, { type: x }))
87+
const hasUnknown = tokens.some((x) => x.kind === ts.SyntaxKind.UnknownKeyword)
88+
if (hasUnknown) return f.createKeywordTypeNode(ts.SyntaxKind.UnknownKeyword)
89+
return f.createUnionTypeNode(tokens)
90+
}
91+
8492
export const makeType = (ctx: Context, s?: Referenced<OAS3>): ts.TypeNode => {
8593
const mk = makeType.bind(null, ctx)
8694

@@ -108,6 +116,7 @@ export const makeType = (ctx: Context, s?: Referenced<OAS3>): ts.TypeNode => {
108116

109117
if ("enum" in s && s.enum && !Array.isArray(s.type)) {
110118
const isArray = s.type === "array"
119+
// @ts-expect-error todo: fix type
111120
const t = makeInlineEnum(isArray ? { ...s, type: s.items?.type } : s)
112121
if (t) return isArray ? f.createArrayTypeNode(t) : t
113122
}
@@ -123,26 +132,15 @@ export const makeType = (ctx: Context, s?: Referenced<OAS3>): ts.TypeNode => {
123132
}
124133

125134
if ("type" in s) {
126-
// openapi v3.1 can have type as array
127-
if (Array.isArray(s.type)) {
128-
const types: OAS3[] = []
129-
for (const type of s.type) {
130-
if (type === "null") types.push({ type: "null" })
131-
else types.push({ ...s, type })
132-
}
133-
134-
return mk({ oneOf: types })
135-
}
136-
137135
let t: ts.TypeNode
138136
// if (s.type === "object") t = f.createKeywordTypeNode(ts.SyntaxKind.UnknownKeyword)
139137
if (s.type === "object") t = makeObject(ctx, s)
140138
else if (s.type === "boolean") t = f.createKeywordTypeNode(ts.SyntaxKind.BooleanKeyword)
141139
else if (s.type === "number") t = f.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword)
142140
else if (s.type === "string") t = f.createKeywordTypeNode(ts.SyntaxKind.StringKeyword)
143-
else if (s.type === "array") t = f.createArrayTypeNode(mk(s.items))
144141
else if (s.type === "null") t = f.createLiteralTypeNode(f.createNull())
145-
else if (isArray(s.type)) t = f.createUnionTypeNode(s.type.map((x) => mk({ type: x })))
142+
else if (isArray(s.type)) t = makeLiteralUnion(ctx, s.type)
143+
else if (s.type === "array" && !isBoolean(s.items)) t = f.createArrayTypeNode(mk(s.items))
146144
else {
147145
console.warn(`makeType: unknown type "${s.type}"`)
148146
return f.createKeywordTypeNode(ts.SyntaxKind.UnknownKeyword)
@@ -153,7 +151,12 @@ export const makeType = (ctx: Context, s?: Referenced<OAS3>): ts.TypeNode => {
153151
if (s.format === "date-time" && ctx.parseDates) t = f.createTypeReferenceNode("Date")
154152
}
155153

156-
return s.nullable ? f.createUnionTypeNode([t, f.createLiteralTypeNode(f.createNull())]) : t
154+
// openapi 3.0 has nullable as boolean property
155+
if ("nullable" in s && s.nullable) {
156+
return f.createUnionTypeNode([t, f.createLiteralTypeNode(f.createNull())])
157+
}
158+
159+
return t
157160
}
158161

159162
// throw new Error(`makeType: unknown schema ${JSON.stringify(s)}`)

test/type-gen.test.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { Oas3Schema, Oas3_1Schema } from "@redocly/openapi-core"
1+
import { Oas3Schema } from "@redocly/openapi-core"
22
import ts from "typescript"
33
import { test } from "uvu"
44
import { equal } from "uvu/assert"
55
import { initCtx } from "../src/config"
66
import { printCode } from "../src/printer"
7+
import { OAS3 } from "../src/schema"
78
import { makeType, makeTypeAlias } from "../src/type-gen"
89

910
type Cfg = {
@@ -12,7 +13,7 @@ type Cfg = {
1213
}
1314

1415
test("type inline", async () => {
15-
const t = (l: Oas3_1Schema, r: string, cfg?: Cfg) => {
16+
const t = (l: OAS3, r: string, cfg?: Cfg) => {
1617
const ctx = initCtx({ ...cfg })
1718
const res = makeType(ctx, l)
1819
const txt = printCode([res as unknown as ts.Statement])
@@ -35,12 +36,17 @@ test("type inline", async () => {
3536
t({ type: "object" }, "object") // should be unknown?
3637
t({ type: "array" }, "void[]") // should be unknown?
3738

38-
// nullable
39-
t({ type: "string", nullable: true }, "string | null")
39+
// nullable (3.0)
4040
t({ type: "string", nullable: false }, "string")
41+
t({ type: "string", nullable: true }, "string | null")
4142
t({ type: "number", nullable: true }, "number | null")
4243
t({ type: "date", nullable: true }, "unknown") // just unknown
4344

45+
// nullable (3.1)
46+
t({ type: ["string", "null"] }, "string | null")
47+
t({ type: ["number", "null"] }, "number | null")
48+
t({ type: ["date", "null"] }, "unknown") // just unknown
49+
4450
// combinations
4551
t({ oneOf: [{ type: "string" }, { type: "number" }] }, "string | number")
4652
t({ anyOf: [{ type: "string" }, { type: "number" }] }, "string | number")

0 commit comments

Comments
 (0)