Skip to content

Commit 43cfe2b

Browse files
authored
fix: use schema types rather than ir (#390)
as written, these tests are using `Schema` types not normalized `IRModel` types for the input. this needs correcting before `Schema` and `IRModel` begin to diverge, split from #386
1 parent 3014e00 commit 43cfe2b

File tree

4 files changed

+140
-136
lines changed

4 files changed

+140
-136
lines changed

packages/openapi-code-generator/src/typescript/common/schema-builders/joi-schema-builder.spec.ts

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
import vm from "node:vm"
22
import {describe, expect, it} from "@jest/globals"
33
import type {
4-
IRModelArray,
5-
IRModelBoolean,
6-
IRModelNumeric,
7-
IRModelObject,
8-
IRModelString,
9-
} from "../../../core/openapi-types-normalized"
4+
SchemaArray,
5+
SchemaBoolean,
6+
SchemaNumber,
7+
SchemaObject,
8+
SchemaString,
9+
} from "../../../core/openapi-types"
1010
import {testVersions} from "../../../test/input.test-utils"
1111
import type {SchemaBuilderConfig} from "./abstract-schema-builder"
1212
import {
13-
irModelNumber,
14-
irModelObject,
15-
irModelString,
1613
schemaBuilderTestHarness,
14+
schemaNumber,
15+
schemaObject,
16+
schemaString,
1717
} from "./schema-builder.test-utils"
1818

1919
describe.each(testVersions)(
@@ -408,7 +408,7 @@ describe.each(testVersions)(
408408
})
409409

410410
describe("numbers", () => {
411-
const base: IRModelNumeric = {
411+
const base: SchemaNumber = {
412412
nullable: false,
413413
readOnly: false,
414414
type: "number",
@@ -642,7 +642,7 @@ describe.each(testVersions)(
642642
})
643643

644644
describe("strings", () => {
645-
const base: IRModelString = {
645+
const base: SchemaString = {
646646
nullable: false,
647647
readOnly: false,
648648
type: "string",
@@ -876,7 +876,7 @@ describe.each(testVersions)(
876876
})
877877

878878
describe("booleans", () => {
879-
const base: IRModelBoolean = {
879+
const base: SchemaBoolean = {
880880
nullable: false,
881881
readOnly: false,
882882
type: "boolean",
@@ -978,7 +978,7 @@ describe.each(testVersions)(
978978
})
979979

980980
describe("arrays", () => {
981-
const base: IRModelArray = {
981+
const base: SchemaArray = {
982982
nullable: false,
983983
readOnly: false,
984984
type: "array",
@@ -1061,7 +1061,7 @@ describe.each(testVersions)(
10611061
it("supports minItems / maxItems / uniqueItems", async () => {
10621062
const {code, execute} = await getActualFromModel({
10631063
...base,
1064-
items: {type: "number", nullable: false, readOnly: false},
1064+
items: schemaNumber(),
10651065
minItems: 1,
10661066
maxItems: 3,
10671067
uniqueItems: true,
@@ -1111,7 +1111,7 @@ describe.each(testVersions)(
11111111
})
11121112

11131113
describe("objects", () => {
1114-
const base: IRModelObject = {
1114+
const base: SchemaObject = {
11151115
type: "object",
11161116
allOf: [],
11171117
anyOf: [],
@@ -1203,8 +1203,8 @@ describe.each(testVersions)(
12031203
describe("unions", () => {
12041204
it("can union a string and number", async () => {
12051205
const {code, execute} = await getActualFromModel(
1206-
irModelObject({
1207-
anyOf: [irModelString(), irModelNumber()],
1206+
schemaObject({
1207+
anyOf: [schemaString(), schemaNumber()],
12081208
}),
12091209
)
12101210

@@ -1222,17 +1222,17 @@ describe.each(testVersions)(
12221222

12231223
it("can union an intersected object and string", async () => {
12241224
const {code, execute} = await getActualFromModel(
1225-
irModelObject({
1225+
schemaObject({
12261226
anyOf: [
1227-
irModelString(),
1228-
irModelObject({
1227+
schemaString(),
1228+
schemaObject({
12291229
allOf: [
1230-
irModelObject({
1231-
properties: {foo: irModelString()},
1230+
schemaObject({
1231+
properties: {foo: schemaString()},
12321232
required: ["foo"],
12331233
}),
1234-
irModelObject({
1235-
properties: {bar: irModelString()},
1234+
schemaObject({
1235+
properties: {bar: schemaString()},
12361236
required: ["bar"],
12371237
}),
12381238
],
@@ -1275,14 +1275,14 @@ describe.each(testVersions)(
12751275
describe("intersections", () => {
12761276
it("can intersect objects", async () => {
12771277
const {code, execute} = await getActualFromModel(
1278-
irModelObject({
1278+
schemaObject({
12791279
allOf: [
1280-
irModelObject({
1281-
properties: {foo: irModelString()},
1280+
schemaObject({
1281+
properties: {foo: schemaString()},
12821282
required: ["foo"],
12831283
}),
1284-
irModelObject({
1285-
properties: {bar: irModelString()},
1284+
schemaObject({
1285+
properties: {bar: schemaString()},
12861286
required: ["bar"],
12871287
}),
12881288
],
@@ -1315,22 +1315,22 @@ describe.each(testVersions)(
13151315
// TODO: https://github.com/hapijs/joi/issues/3057
13161316
it.skip("can intersect unions", async () => {
13171317
const {code, execute} = await getActualFromModel(
1318-
irModelObject({
1318+
schemaObject({
13191319
allOf: [
1320-
irModelObject({
1320+
schemaObject({
13211321
oneOf: [
1322-
irModelObject({
1323-
properties: {foo: irModelString()},
1322+
schemaObject({
1323+
properties: {foo: schemaString()},
13241324
required: ["foo"],
13251325
}),
1326-
irModelObject({
1327-
properties: {bar: irModelString()},
1326+
schemaObject({
1327+
properties: {bar: schemaString()},
13281328
required: ["bar"],
13291329
}),
13301330
],
13311331
}),
1332-
irModelObject({
1333-
properties: {id: irModelString()},
1332+
schemaObject({
1333+
properties: {id: schemaString()},
13341334
required: ["id"],
13351335
}),
13361336
],
@@ -1377,7 +1377,7 @@ describe.each(testVersions)(
13771377

13781378
describe("unspecified schemas when allowAny: true", () => {
13791379
const config: SchemaBuilderConfig = {allowAny: true}
1380-
const base: IRModelObject = {
1380+
const base: SchemaObject = {
13811381
type: "object",
13821382
allOf: [],
13831383
anyOf: [],
@@ -1482,7 +1482,7 @@ describe.each(testVersions)(
14821482

14831483
describe("unspecified schemas when allowAny: false", () => {
14841484
const config: SchemaBuilderConfig = {allowAny: false}
1485-
const base: IRModelObject = {
1485+
const base: SchemaObject = {
14861486
type: "object",
14871487
allOf: [],
14881488
anyOf: [],

packages/openapi-code-generator/src/typescript/common/schema-builders/schema-builder.test-utils.ts

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import ts from "typescript"
22
import type {Input} from "../../../core/input"
33
import type {
4-
IRModel,
5-
IRModelNumeric,
6-
IRModelObject,
7-
IRModelString,
8-
MaybeIRModel,
9-
} from "../../../core/openapi-types-normalized"
4+
Reference,
5+
Schema,
6+
SchemaNumber,
7+
SchemaObject,
8+
SchemaString,
9+
} from "../../../core/openapi-types"
10+
import {isRef} from "../../../core/openapi-utils"
1011
import {
1112
type OpenApiVersion,
1213
unitTestInput,
@@ -23,11 +24,11 @@ export function schemaBuilderTestHarness(
2324
executeParseSchema: (code: string, input?: unknown) => Promise<unknown>,
2425
) {
2526
async function getActualFromModel(
26-
model: IRModel,
27+
schema: Schema,
2728
config: SchemaBuilderConfig = {allowAny: false},
2829
) {
2930
const {input} = await unitTestInput(version)
30-
return getResult(input, model, true, config)
31+
return getResult(input, schema, true, config)
3132
}
3233

3334
async function getActual(
@@ -40,7 +41,7 @@ export function schemaBuilderTestHarness(
4041

4142
async function getResult(
4243
input: Input,
43-
maybeModel: MaybeIRModel,
44+
maybeSchema: Schema | Reference,
4445
required: boolean,
4546
config: SchemaBuilderConfig,
4647
) {
@@ -66,7 +67,10 @@ export function schemaBuilderTestHarness(
6667

6768
const schema = schemaBuilder
6869
.withImports(imports)
69-
.fromModel(maybeModel, required)
70+
.fromModel(
71+
isRef(maybeSchema) ? maybeSchema : input.schema(maybeSchema),
72+
required,
73+
)
7074

7175
const code = (
7276
await formatter.format(
@@ -117,9 +121,9 @@ export function schemaBuilderTestHarness(
117121
}
118122
}
119123

120-
export function irModelObject(
121-
partial: Partial<IRModelObject> = {},
122-
): IRModelObject {
124+
export function schemaObject(
125+
partial: Partial<SchemaObject> = {},
126+
): SchemaObject {
123127
return {
124128
type: "object",
125129
allOf: [],
@@ -134,9 +138,9 @@ export function irModelObject(
134138
}
135139
}
136140

137-
export function irModelString(
138-
partial: Partial<IRModelString> = {},
139-
): IRModelString {
141+
export function schemaString(
142+
partial: Partial<SchemaString> = {},
143+
): SchemaString {
140144
return {
141145
type: "string",
142146
nullable: false,
@@ -145,9 +149,9 @@ export function irModelString(
145149
}
146150
}
147151

148-
export function irModelNumber(
149-
partial: Partial<IRModelNumeric> = {},
150-
): IRModelNumeric {
152+
export function schemaNumber(
153+
partial: Partial<SchemaNumber> = {},
154+
): SchemaNumber {
151155
return {
152156
type: "number",
153157
nullable: false,

0 commit comments

Comments
 (0)