Skip to content

Commit 7f0b6a1

Browse files
Merge pull request #128 from aqsous/main
refactor: update openapi3-ts
2 parents fa4371d + 969cf34 commit 7f0b6a1

File tree

7 files changed

+33
-46
lines changed

7 files changed

+33
-46
lines changed

package-lock.json

+13-13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
"ngx-deploy-npm": "^6.0.0",
6969
"nx": "16.2.2",
7070
"nx-cloud": "16.0.5",
71-
"openapi3-ts": "^3.1.2",
71+
"openapi3-ts": "^4.1.2",
7272
"prettier": "^2.8.8",
7373
"reflect-metadata": "^0.1.13",
7474
"supertest": "^6.3.3",

packages/zod-nestjs/src/lib/create-zod-dto.ts

+4-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { SchemaObject } from 'openapi3-ts';
1+
import type { SchemaObject } from 'openapi3-ts/oas31';
22
import { generateSchema, OpenApiZodAny } from '@anatine/zod-openapi';
33
import * as z from 'zod';
44

@@ -79,14 +79,8 @@ export const createZodDto = <T extends OpenApiZodAny>(
7979
const schemaObjectWithRequiredField = {
8080
...schemaObject,
8181
};
82-
if (
83-
(generatedSchema.required !== undefined,
84-
generatedSchema.required?.includes(key))
85-
) {
86-
schemaObjectWithRequiredField.required = true;
87-
} else {
88-
schemaObjectWithRequiredField.required = false;
89-
}
82+
schemaObjectWithRequiredField.required = !!(generatedSchema.required !== undefined,
83+
generatedSchema.required?.includes(key));
9084
properties[key] = schemaObjectWithRequiredField as any; // TODO: Fix this
9185
}
9286
return properties as Record<string, SchemaObject>;
@@ -95,7 +89,7 @@ export const createZodDto = <T extends OpenApiZodAny>(
9589
public static create(input: unknown): CompatibleZodInfer<T> {
9690
return this.zodSchema.parse(input);
9791
}
98-
};
92+
}
9993

10094
return <MergeZodSchemaOutput<T>>SchemaHolderClass;
10195
};

packages/zod-nestjs/src/lib/patch-nest-swagger.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
* This file was copied from:
66
* https://github.com/kbkk/abitia/blob/master/packages/zod-dto/src/OpenApi/patchNestjsSwagger.ts
77
*/
8-
import { generateSchema } from '@anatine/zod-openapi';
9-
import type { SchemaObject } from 'openapi3-ts';
8+
import {generateSchema} from '@anatine/zod-openapi';
9+
import type {SchemaObject} from 'openapi3-ts/oas31';
1010

1111
interface Type<T = any> extends Function {
1212
new (...args: any[]): T;
@@ -38,9 +38,7 @@ export const patchNestjsSwagger = (
3838
return orgExploreModelSchema.call(this, type, schemas, schemaRefsStack);
3939
}
4040

41-
const openApiDef = generateSchema(type.zodSchema);
42-
43-
schemas[type.name] = openApiDef;
41+
schemas[type.name] = generateSchema(type.zodSchema);
4442

4543
return type.name;
4644
};

packages/zod-openapi/src/lib/zod-extensions.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This code is heavily inspired by https://github.com/asteasolutions/zod-to-openap
44

55
import { extendApi } from './zod-openapi';
66
import {z} from "zod";
7-
import { SchemaObject} from "openapi3-ts";
7+
import { SchemaObject } from "openapi3-ts/oas31";
88
import {ZodTypeDef} from "zod/lib/types";
99

1010

packages/zod-openapi/src/lib/zod-openapi.spec.ts

+5-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { SchemaObject } from 'openapi3-ts';
1+
import { SchemaObject } from 'openapi3-ts/oas31';
22
import validator from 'validator';
33
import { z } from 'zod';
44
import { generateSchema, extendApi } from './zod-openapi';
@@ -84,10 +84,6 @@ describe('zodOpenapi', () => {
8484
);
8585
const apiSchema = generateSchema(zodSchema);
8686
expect(apiSchema.properties).toBeDefined();
87-
expect((apiSchema.properties?.aAny as SchemaObject).nullable).toEqual(true);
88-
expect((apiSchema.properties?.aUnknown as SchemaObject).nullable).toEqual(
89-
true
90-
);
9187
});
9288

9389
it('should support never type', () => {
@@ -201,12 +197,12 @@ describe('zodOpenapi', () => {
201197
aNumberMin: { type: 'number', minimum: 3 },
202198
aNumberMax: { type: 'number', maximum: 8 },
203199
aNumberInt: { type: 'integer' },
204-
aNumberPositive: { type: 'number', minimum: 0, exclusiveMinimum: true },
200+
aNumberPositive: { type: 'number', minimum: 0, exclusiveMinimum: 0 },
205201
aNumberNonnegative: { type: 'number', minimum: 0 },
206-
aNumberNegative: { type: 'number', maximum: 0, exclusiveMaximum: true },
202+
aNumberNegative: { type: 'number', maximum: 0, exclusiveMaximum: 0 },
207203
aNumberNonpositive: { type: 'number', maximum: 0 },
208-
aNumberGt: { type: 'number', minimum: 5, exclusiveMinimum: true },
209-
aNumberLt: { type: 'number', maximum: 5, exclusiveMaximum: true },
204+
aNumberGt: { type: 'number', minimum: 5, exclusiveMinimum: 5 },
205+
aNumberLt: { type: 'number', maximum: 5, exclusiveMaximum: 5 },
210206
aNumberMultipleOf: { type: 'number', multipleOf: 2 },
211207
},
212208
description: 'Look mah, the horse can count higher than me!',

packages/zod-openapi/src/lib/zod-openapi.ts

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import type { SchemaObject, SchemaObjectType } from 'openapi3-ts';
1+
import type {SchemaObject, SchemaObjectType} from 'openapi3-ts/oas31';
22
import merge from 'ts-deepmerge';
3-
import { AnyZodObject, z, ZodTypeAny } from 'zod';
3+
import {AnyZodObject, z, ZodTypeAny} from 'zod';
44

55
export interface OpenApiZodAny extends ZodTypeAny {
66
metaOpenApi?: SchemaObject | SchemaObject[];
@@ -145,11 +145,11 @@ function parseNumber({
145145
case 'max':
146146
baseSchema.maximum = item.value;
147147
// TODO: option to make this always explicit? (false instead of non-existent)
148-
if (!item.inclusive) baseSchema.exclusiveMaximum = true;
148+
if (!item.inclusive) baseSchema.exclusiveMaximum = item.value;
149149
break;
150150
case 'min':
151151
baseSchema.minimum = item.value;
152-
if (!item.inclusive) baseSchema.exclusiveMinimum = true;
152+
if (!item.inclusive) baseSchema.exclusiveMinimum = item.value;
153153
break;
154154
case 'int':
155155
baseSchema.type = 'integer';
@@ -206,7 +206,7 @@ function parseObject({
206206
const required =
207207
requiredProperties.length > 0 ? { required: requiredProperties } : {};
208208

209-
const result = merge(
209+
return merge(
210210
{
211211
type: 'object' as SchemaObjectType,
212212
properties: iterateZodObject({
@@ -217,10 +217,9 @@ function parseObject({
217217
...required,
218218
...additionalProperties,
219219
},
220-
zodRef.description ? { description: zodRef.description } : {},
220+
zodRef.description ? {description: zodRef.description} : {},
221221
...schemas
222222
);
223-
return result;
224223
}
225224

226225
function parseRecord({

0 commit comments

Comments
 (0)