Skip to content
Merged
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ const { schema, components } = createSchema(job, {
schemaType: 'input'; // This controls whether this should be rendered as a request (`input`) or response (`output`). Defaults to `output`
openapi: '3.0.0'; // OpenAPI version to use, defaults to `'3.1.0'`
components: { jobId: z.string() } // Additional components to use and create while rendering the schema
componentRefPath: '#/definitions/' // Defaults to #/components/schemas/
})
```

Expand Down
6 changes: 4 additions & 2 deletions src/create/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,10 @@ const getCallbacks = (
});
};

export const createComponentSchemaRef = (schemaRef: string) =>
`#/components/schemas/${schemaRef}`;
export const createComponentSchemaRef = (
schemaRef: string,
componentPath?: string,
) => `${componentPath ?? '#/components/schemas/'}${schemaRef}`;

export const createComponentResponseRef = (responseRef: string) =>
`#/components/responses/${responseRef}`;
Expand Down
25 changes: 20 additions & 5 deletions src/create/schema/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import {
type SchemaComponent,
createComponentSchemaRef,
} from '../components';
import type { CreateDocumentOptions } from '../document';

import { enhanceWithMetadata } from './metadata';
import { createSchemaSwitch } from './parsers';
import { verifyEffects } from './parsers/transform';
import type { CreateSchemaOptions } from './single';

export type LazyMap = Map<ZodType, true>;

Expand All @@ -21,7 +21,7 @@ export interface SchemaState {
type: CreationType;
path: string[];
visited: Set<ZodType>;
documentOptions?: CreateDocumentOptions;
documentOptions?: CreateSchemaOptions;
}

const isDescriptionEqual = (schema: Schema, zodSchema: ZodType): boolean =>
Expand Down Expand Up @@ -95,7 +95,12 @@ export const createNewRef = <

return {
type: 'ref',
schema: { $ref: createComponentSchemaRef(ref) },
schema: {
$ref: createComponentSchemaRef(
ref,
state.documentOptions?.componentRefPath,
),
},
effects: newSchema.effects
? [
{
Expand All @@ -121,7 +126,12 @@ export const createExistingRef = <
if (component && component.type === 'complete') {
return {
type: 'ref',
schema: { $ref: createComponentSchemaRef(component.ref) },
schema: {
$ref: createComponentSchemaRef(
component.ref,
state.documentOptions?.componentRefPath,
),
},
effects: component.effects
? [
{
Expand All @@ -138,7 +148,12 @@ export const createExistingRef = <
if (component && component.type === 'in-progress') {
return {
type: 'ref',
schema: { $ref: createComponentSchemaRef(component.ref) },
schema: {
$ref: createComponentSchemaRef(
component.ref,
state.documentOptions?.componentRefPath,
),
},
effects: [
{
type: 'component',
Expand Down
9 changes: 8 additions & 1 deletion src/create/schema/parsers/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,14 @@ export const createExtendedSchema = <
return {
type: 'schema',
schema: {
allOf: [{ $ref: createComponentSchemaRef(completeComponent.ref) }],
allOf: [
{
$ref: createComponentSchemaRef(
completeComponent.ref,
state.documentOptions?.componentRefPath,
),
},
],
...extendedSchema.schema,
},
effects: flattenEffects([
Expand Down
21 changes: 21 additions & 0 deletions src/create/schema/single.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,25 @@ describe('createSchema', () => {
},
});
});

it('should support componentRefPath', () => {
const schema = createSchema(
z.string().openapi({ description: 'foo', ref: 'String' }),
{
componentRefPath: '#/definitions/',
},
);

expect(schema).toEqual<SchemaResult>({
schema: {
$ref: '#/definitions/String',
},
components: {
String: {
type: 'string',
description: 'foo',
},
},
});
});
});
4 changes: 4 additions & 0 deletions src/create/schema/single.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ export interface CreateSchemaOptions extends CreateDocumentOptions {
* Additional components to use and create while rendering the schema
*/
components?: Record<string, ZodType>;
/**
* The $ref path to use for the component. Defaults to `#/components/schemas/`
*/
componentRefPath?: string;
}

export const createSchema = (
Expand Down
Loading