Skip to content

Commit

Permalink
Bump zod-openapi requirement to 3.0.1 (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
samchungy authored Sep 20, 2024
1 parent 33adc12 commit 8d8cdcf
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 49 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/prerelease.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ jobs:
with:
fetch-depth: 0

- name: Set up Node.js 18.x
- name: Set up Node.js 20.x
uses: actions/setup-node@v3
with:
node-version: 18.x
node-version: 20.x
registry-url: 'https://registry.npmjs.org'

- name: Set up pnpm
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ jobs:
with:
fetch-depth: 0

- name: Set up Node.js 18.x
- name: Set up Node.js 20.x
uses: actions/setup-node@v3
with:
node-version: 18.x
node-version: 20.x
registry-url: 'https://registry.npmjs.org'

- name: Set up pnpm
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ jobs:
- name: Check out repo
uses: actions/checkout@v3

- name: Set up Node.js 18.x
- name: Set up Node.js 20.x
uses: actions/setup-node@v3
with:
node-version: 18.x
node-version: 20.x

- name: Set up pnpm
run: corepack enable pnpm
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@
"fastify": "5.0.0",
"skuba": "8.2.1",
"zod": "3.23.8",
"zod-openapi": "2.19.0"
"zod-openapi": "3.0.1"
},
"peerDependencies": {
"@fastify/swagger": "^9.0.0",
"@fastify/swagger-ui": "^5.0.1",
"fastify": "5",
"zod": "^3.21.4",
"zod-openapi": "^2.6.3"
"zod-openapi": "^3.0.1"
},
"peerDependenciesMeta": {
"@fastify/swagger": {
Expand Down
12 changes: 6 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions src/plugin.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'zod-openapi/extend';
import fastifySwagger from '@fastify/swagger';
import fastifySwaggerUI from '@fastify/swagger-ui';
import fastify from 'fastify';
import { z } from 'zod';
import { extendZodWithOpenApi } from 'zod-openapi';

import type {
FastifyPluginAsyncZodOpenApi,
Expand All @@ -13,8 +13,6 @@ import { serializerCompiler } from './serializerCompiler';
import type { FastifyZodOpenApiSchema } from './transformer';
import { validatorCompiler } from './validatorCompiler';

extendZodWithOpenApi(z);

describe('validatorCompiler', () => {
it('should pass a valid response', async () => {
const app = fastify();
Expand Down
11 changes: 7 additions & 4 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ import type {
} from 'fastify';
import fp from 'fastify-plugin';
import type { ZodType, z } from 'zod';
import { api } from 'zod-openapi';
import type {
ZodOpenApiComponentsObject,
ZodOpenApiVersion,
} from 'zod-openapi';
import {
type ComponentsObject as ApiComponentsObject,
getDefaultComponents,
} from 'zod-openapi/api';

export const FASTIFY_ZOD_OPENAPI_COMPONENTS = Symbol(
'fastify-zod-openapi-components',
Expand All @@ -25,15 +28,15 @@ type FastifyZodOpenApiOpts = {

declare module 'fastify' {
interface FastifySchema {
[FASTIFY_ZOD_OPENAPI_COMPONENTS]?: api.ComponentsObject;
[FASTIFY_ZOD_OPENAPI_COMPONENTS]?: ApiComponentsObject;
}
}

declare module 'openapi-types' {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace OpenAPIV3 {
interface Document {
[FASTIFY_ZOD_OPENAPI_COMPONENTS]?: api.ComponentsObject;
[FASTIFY_ZOD_OPENAPI_COMPONENTS]?: ApiComponentsObject;
}
}
}
Expand All @@ -49,7 +52,7 @@ export type FastifyZodOpenApi = FastifyPluginAsync<FastifyZodOpenApiOpts>;

// eslint-disable-next-line @typescript-eslint/require-await
const fastifyZodOpenApi: FastifyZodOpenApi = async (fastify, opts) => {
const components = api.getDefaultComponents(opts.components, opts.openapi);
const components = getDefaultComponents(opts.components, opts.openapi);

fastify.addHook('onRoute', (routeOptions) => {
if (routeOptions.schema) {
Expand Down
8 changes: 2 additions & 6 deletions src/serializerCompiler.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import 'zod-openapi/extend';
import fastify from 'fastify';
import { z } from 'zod';
import {
type ZodOpenApiResponsesObject,
extendZodWithOpenApi,
} from 'zod-openapi';
import type { ZodOpenApiResponsesObject } from 'zod-openapi';

import type { FastifyZodOpenApiTypeProvider } from './plugin';
import { serializerCompiler } from './serializerCompiler';

extendZodWithOpenApi(z);

describe('validatorCompiler', () => {
it('should pass a valid response', async () => {
const app = fastify();
Expand Down
4 changes: 1 addition & 3 deletions src/transformer.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'zod-openapi/extend';
import fastifySwagger from '@fastify/swagger';
import fastifySwaggerUI from '@fastify/swagger-ui';
import fastify from 'fastify';
import { z } from 'zod';
import { extendZodWithOpenApi } from 'zod-openapi';

import {
type FastifyZodOpenApiTypeProvider,
Expand All @@ -16,8 +16,6 @@ import {
fastifyZodOpenApiTransformObject,
} from '../src/transformer';

extendZodWithOpenApi(z);

describe('fastifyZodOpenApiTransform', () => {
it('should support creating an openapi response', async () => {
const app = fastify();
Expand Down
33 changes: 18 additions & 15 deletions src/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ import type { FastifyDynamicSwaggerOptions } from '@fastify/swagger';
import type { FastifySchema } from 'fastify';
import type { OpenAPIV3 } from 'openapi-types';
import type { AnyZodObject, ZodObject, ZodRawShape, ZodType } from 'zod';
import { api } from 'zod-openapi';
import type {
ZodOpenApiComponentsObject,
ZodOpenApiParameters,
ZodOpenApiResponsesObject,
oas31,
} from 'zod-openapi';
import {
type ComponentsObject,
createComponents,
createMediaTypeSchema,
createParamOrRef,
} from 'zod-openapi/api';

import { FASTIFY_ZOD_OPENAPI_COMPONENTS } from './plugin';

Expand Down Expand Up @@ -51,12 +56,12 @@ export const createParams = (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
querystring: ZodObject<any, any, any, any, any>,
type: keyof ZodOpenApiParameters,
components: api.ComponentsObject,
components: ComponentsObject,
path: string[],
): Record<string, FastifySwaggerSchemaObject | oas31.ReferenceObject> =>
Object.entries(querystring.shape as ZodRawShape).reduce(
(acc, [key, value]: [string, ZodType]) => {
const parameter = api.createParamOrRef(
const parameter = createParamOrRef(
value,
components,
[...path, key],
Expand All @@ -80,11 +85,11 @@ export const createParams = (

export const createResponseSchema = (
schema: FastifyResponseSchema,
components: api.ComponentsObject,
components: ComponentsObject,
path: string[],
): unknown => {
if (isZodType(schema)) {
return api.createMediaTypeSchema(schema, components, 'output', [
return createMediaTypeSchema(schema, components, 'output', [
...path,
'schema',
]);
Expand All @@ -94,7 +99,7 @@ export const createResponseSchema = (

export const createContent = (
content: unknown,
components: api.ComponentsObject,
components: ComponentsObject,
path: string[],
): unknown => {
if (typeof content !== 'object' || content == null) {
Expand Down Expand Up @@ -124,7 +129,7 @@ export const createContent = (

export const createResponse = (
response: unknown,
components: api.ComponentsObject,
components: ComponentsObject,
path: string[],
): unknown => {
if (typeof response !== 'object' || response == null) {
Expand All @@ -134,7 +139,7 @@ export const createResponse = (
return Object.entries(response).reduce(
(acc, [key, value]: [string, unknown]) => {
if (isZodType(value)) {
acc[key] = api.createMediaTypeSchema(value, components, 'output', [
acc[key] = createMediaTypeSchema(value, components, 'output', [
...path,
key,
]);
Expand Down Expand Up @@ -193,12 +198,10 @@ export const fastifyZodOpenApiTransform: Transform = ({
};

if (isZodType(body)) {
transformedSchema.body = api.createMediaTypeSchema(
body,
components,
'input',
[url, 'body'],
);
transformedSchema.body = createMediaTypeSchema(body, components, 'input', [
url,
'body',
]);
}

if (maybeResponse) {
Expand Down Expand Up @@ -247,7 +250,7 @@ export const fastifyZodOpenApiTransformObject: TransformObject = (opts) => {

return {
...opts.openapiObject,
components: api.createComponents(
components: createComponents(
(opts.openapiObject.components ?? {}) as ZodOpenApiComponentsObject,
components,
) as OpenAPIV3.ComponentsObject,
Expand Down
4 changes: 1 addition & 3 deletions src/validatorCompiler.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import 'zod-openapi/extend';
import fastify from 'fastify';
import { z } from 'zod';
import { extendZodWithOpenApi } from 'zod-openapi';

import type { FastifyZodOpenApiTypeProvider } from './plugin';
import { validatorCompiler } from './validatorCompiler';

extendZodWithOpenApi(z);

describe('validatorCompiler', () => {
describe('querystring', () => {
it('should pass a valid input', async () => {
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
"removeComments": false,
"target": "ES2022"
},
"exclude": ["lib*/**/*"],
"exclude": ["lib*/**/*", "dist*/**/*"],
"extends": "skuba/config/tsconfig.json"
}

0 comments on commit 8d8cdcf

Please sign in to comment.