Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions src/create/callbacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ import {
type ComponentsObject,
createComponentCallbackRef,
} from './components';
import type { ZodOpenApiCallbackObject } from './document';
import type {
CreateDocumentOptions,
ZodOpenApiCallbackObject,
} from './document';
import { createPathItem } from './paths';
import { isISpecificationExtension } from './specificationExtension';

export const createCallback = (
callbackObject: ZodOpenApiCallbackObject,
components: ComponentsObject,
subpath: string[],
documentOptions?: CreateDocumentOptions,
): oas31.CallbackObject => {
const { ref, ...callbacks } = callbackObject;

Expand All @@ -24,11 +28,13 @@ export const createCallback = (
return acc;
}

// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
acc[callbackName] = createPathItem(pathItemObject, components, [
...subpath,
callbackName,
]);
acc[callbackName] = createPathItem(
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
pathItemObject,
components,
[...subpath, callbackName],
documentOptions,
);
return acc;
}, {});

Expand All @@ -50,6 +56,7 @@ export const createCallbacks = (
callbacksObject: oas31.CallbackObject | undefined,
components: ComponentsObject,
subpath: string[],
documentOptions?: CreateDocumentOptions,
): oas31.CallbackObject | undefined => {
if (!callbacksObject) {
return undefined;
Expand All @@ -61,11 +68,14 @@ export const createCallbacks = (
acc[callbackName] = callbackObject;
return acc;
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
acc[callbackName] = createCallback(callbackObject, components, [
...subpath,
callbackName,
]);

acc[callbackName] = createCallback(
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
callbackObject,
components,
[...subpath, callbackName],
documentOptions,
);
return acc;
},
{},
Expand Down
63 changes: 52 additions & 11 deletions src/create/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { isAnyZodType } from '../zodType';

import { createCallback } from './callbacks';
import type {
CreateDocumentOptions,
ZodOpenApiCallbackObject,
ZodOpenApiComponentsObject,
ZodOpenApiRequestBodyObject,
Expand Down Expand Up @@ -399,16 +400,35 @@ export const createComponentCallbackRef = (callbackRef: string) =>
export const createComponents = (
componentsObject: ZodOpenApiComponentsObject,
components: ComponentsObject,
documentOptions?: CreateDocumentOptions,
): oas31.ComponentsObject | undefined => {
const combinedSchemas = createSchemaComponents(componentsObject, components);
const combinedSchemas = createSchemaComponents(
componentsObject,
components,
documentOptions,
);
const combinedParameters = createParamComponents(
componentsObject,
components,
documentOptions,
);
const combinedHeaders = createHeaderComponents(
componentsObject,
components,
documentOptions,
);
const combinedResponses = createResponseComponents(
components,
documentOptions,
);
const combinedRequestBodies = createRequestBodiesComponents(
components,
documentOptions,
);
const combinedCallbacks = createCallbackComponents(
components,
documentOptions,
);
const combinedHeaders = createHeaderComponents(componentsObject, components);
const combinedResponses = createResponseComponents(components);
const combinedRequestBodies = createRequestBodiesComponents(components);
const combinedCallbacks = createCallbackComponents(components);

const { schemas, parameters, headers, responses, requestBodies, ...rest } =
componentsObject;
Expand All @@ -428,6 +448,7 @@ export const createComponents = (
const createSchemaComponents = (
componentsObject: ZodOpenApiComponentsObject,
components: ComponentsObject,
documentOptions?: CreateDocumentOptions,
): oas31.ComponentsObject['schemas'] => {
Array.from(components.schemas).forEach(([schema, { type }], index) => {
if (type === 'manual') {
Expand All @@ -436,6 +457,7 @@ const createSchemaComponents = (
type: schema._def.openapi?.refType ?? 'output',
path: [],
visited: new Set(),
documentOptions,
};

createSchema(schema, state, [`component schema index ${index}`]);
Expand Down Expand Up @@ -479,13 +501,15 @@ const createSchemaComponents = (
const createParamComponents = (
componentsObject: ZodOpenApiComponentsObject,
components: ComponentsObject,
documentOptions?: CreateDocumentOptions,
): oas31.ComponentsObject['parameters'] => {
Array.from(components.parameters).forEach(([schema, component], index) => {
if (component.type === 'manual') {
createParamOrRef(
schema,
components,
[`component parameter index ${index}`],
documentOptions,
component.in,
component.ref,
);
Expand Down Expand Up @@ -527,10 +551,11 @@ const createParamComponents = (
const createHeaderComponents = (
componentsObject: ZodOpenApiComponentsObject,
components: ComponentsObject,
documentOptions?: CreateDocumentOptions,
): oas31.ComponentsObject['headers'] => {
Array.from(components.headers).forEach(([schema, component]) => {
if (component.type === 'manual') {
createHeaderOrRef(schema, components);
createHeaderOrRef(schema, components, documentOptions);
}
});

Expand Down Expand Up @@ -566,10 +591,16 @@ const createHeaderComponents = (

const createResponseComponents = (
components: ComponentsObject,
documentOptions?: CreateDocumentOptions,
): oas31.ComponentsObject['responses'] => {
Array.from(components.responses).forEach(([schema, component], index) => {
if (component.type === 'manual') {
createResponse(schema, components, [`component response index ${index}`]);
createResponse(
schema,
components,
[`component response index ${index}`],
documentOptions,
);
}
});

Expand All @@ -591,12 +622,16 @@ const createResponseComponents = (

const createRequestBodiesComponents = (
components: ComponentsObject,
documentOptions?: CreateDocumentOptions,
): oas31.ComponentsObject['requestBodies'] => {
Array.from(components.requestBodies).forEach(([schema, component], index) => {
if (component.type === 'manual') {
createRequestBody(schema, components, [
`component request body ${index}`,
]);
createRequestBody(
schema,
components,
[`component request body ${index}`],
documentOptions,
);
}
});

Expand All @@ -619,10 +654,16 @@ const createRequestBodiesComponents = (

const createCallbackComponents = (
components: ComponentsObject,
documentOptions?: CreateDocumentOptions,
): oas31.ComponentsObject['callbacks'] => {
Array.from(components.callbacks).forEach(([schema, component], index) => {
if (component.type === 'manual') {
createCallback(schema, components, [`component callback ${index}`]);
createCallback(
schema,
components,
[`component callback ${index}`],
documentOptions,
);
}
});

Expand Down
17 changes: 13 additions & 4 deletions src/create/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { isAnyZodType } from '../zodType';

import type { ComponentsObject, CreationType } from './components';
import type {
CreateDocumentOptions,
ZodOpenApiContentObject,
ZodOpenApiMediaTypeObject,
} from './document';
Expand All @@ -19,6 +20,7 @@ export const createMediaTypeSchema = (
components: ComponentsObject,
type: CreationType,
subpath: string[],
documentOptions?: CreateDocumentOptions,
): oas31.SchemaObject | oas31.ReferenceObject | undefined => {
if (!schemaObject) {
return undefined;
Expand All @@ -35,6 +37,7 @@ export const createMediaTypeSchema = (
type,
path: [],
visited: new Set(),
documentOptions,
},
subpath,
);
Expand All @@ -45,17 +48,21 @@ const createMediaTypeObject = (
components: ComponentsObject,
type: CreationType,
subpath: string[],
documentOptions?: CreateDocumentOptions,
): oas31.MediaTypeObject | undefined => {
if (!mediaTypeObject) {
return undefined;
}

return {
...mediaTypeObject,
schema: createMediaTypeSchema(mediaTypeObject.schema, components, type, [
...subpath,
'schema',
]),
schema: createMediaTypeSchema(
mediaTypeObject.schema,
components,
type,
[...subpath, 'schema'],
documentOptions,
),
};
};

Expand All @@ -64,6 +71,7 @@ export const createContent = (
components: ComponentsObject,
type: CreationType,
subpath: string[],
documentOptions?: CreateDocumentOptions,
): oas31.ContentObject =>
Object.entries(contentObject).reduce<oas31.ContentObject>(
(acc, [mediaType, zodOpenApiMediaTypeObject]): oas31.ContentObject => {
Expand All @@ -72,6 +80,7 @@ export const createContent = (
components,
type,
[...subpath, mediaType],
documentOptions,
);

if (mediaTypeObject) {
Expand Down
Loading
Loading