Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: force one of flag #180

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 12 additions & 0 deletions src/openapi-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,14 @@ export class OpenAPIGenerator {
return this.generateSchemaWithRef(optionToGenerate);
});

const internalOptions = this.getOptions(zodSchema);
if (internalOptions.forceOneOf) {
return {
oneOf: this.mapNullableOfArray(schemas, isNullable),
default: defaultValue,
};
}

return {
anyOf: this.mapNullableOfArray(schemas, isNullable),
default: defaultValue,
Expand Down Expand Up @@ -1294,6 +1302,10 @@ export class OpenAPIGenerator {
return this.getInternalMetadata(zodSchema)?.refId;
}

private getOptions<T extends any>(zodSchema: ZodType<T>) {
return this.getInternalMetadata(zodSchema)?.options;
}

private applySchemaMetadata(
initialData: SchemaObject | ParameterObject | ReferenceObject,
metadata: Partial<ZodOpenAPIMetadata>
Expand Down
20 changes: 18 additions & 2 deletions src/zod-extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,18 @@ export type ZodOpenAPIMetadata<T = any, E = ExampleValue<T>> = Omit<
export interface ZodOpenAPIInternalMetadata {
refId?: string;
extendedFrom?: { refId: string; schema: ZodObject<ZodRawShape> };
options?: ZodToOpenAPIOptions;
}

export interface ZodOpenApiFullMetadata<T = any> {
_internal?: ZodOpenAPIInternalMetadata;
metadata?: ZodOpenAPIMetadata<T>;
}

export interface ZodToOpenAPIOptions {
forceOneOf?: boolean;
}

declare module 'zod' {
interface ZodTypeDef {
openapi?: ZodOpenApiFullMetadata;
Expand All @@ -54,6 +59,13 @@ declare module 'zod' {
refId: string,
metadata?: Partial<ZodOpenAPIMetadata<z.infer<T>>>
): T;

openapi<T extends ZodTypeAny>(
this: T,
refId: string,
metadata?: Partial<ZodOpenAPIMetadata<z.infer<T>>>,
options?: ZodToOpenAPIOptions,
): T;
}
}

Expand Down Expand Up @@ -83,9 +95,12 @@ export function extendZodWithOpenApi(zod: typeof z) {

zod.ZodType.prototype.openapi = function (
refOrOpenapi: string | Partial<ZodOpenAPIMetadata<any>>,
metadata?: Partial<ZodOpenAPIMetadata<any>>
metadataOrOptions?: Partial<ZodOpenAPIMetadata<any>> | ZodToOpenAPIOptions,
options?: ZodToOpenAPIOptions,
) {
const openapi = typeof refOrOpenapi === 'string' ? metadata : refOrOpenapi;
const usingRef = typeof refOrOpenapi === 'string';
const openapi = (usingRef ? metadataOrOptions : refOrOpenapi) as Partial<ZodOpenAPIMetadata<any>> | undefined;
const resolvedOptions = (usingRef ? options : metadataOrOptions) as ZodToOpenAPIOptions | undefined;

const { param, ...restOfOpenApi } = openapi ?? {};

Expand All @@ -94,6 +109,7 @@ export function extendZodWithOpenApi(zod: typeof z) {
...(typeof refOrOpenapi === 'string'
? { refId: refOrOpenapi }
: undefined),
...({ options: resolvedOptions } ?? {}),
};

const resultMetadata = {
Expand Down
Loading