Something went wrong. Try again.
fork of hey-api/openapi-ts because I need some additional things
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677import { ref } from '@hey-api/codegen-core';import type { Hooks, IR } from '@hey-api/shared';import { createSchemaProcessor, createSchemaWalker, pathToJsonPointer } from '@hey-api/shared';
import { exportAst } from '../shared/export';import type { ProcessorContext, ProcessorResult } from '../shared/processor';import type { PydanticFinal } from '../shared/types';import type { PydanticPlugin } from '../types';import { createVisitor } from './walker';
export function createProcessor(plugin: PydanticPlugin['Instance']): ProcessorResult { const processor = createSchemaProcessor();
const extractorHooks: ReadonlyArray<NonNullable<Hooks['schemas']>['shouldExtract']> = [ (ctx) => ctx.schema.type === 'object' && ctx.schema.properties !== undefined && Boolean(Object.keys(ctx.schema.properties).length), (ctx) => ctx.schema.type === 'enum' && ctx.schema.items !== undefined && Boolean(ctx.schema.items.length), plugin.config['~hooks']?.schemas?.shouldExtract, plugin.context.config.parser.hooks.schemas?.shouldExtract, ];
function extractor(ctx: ProcessorContext): IR.SchemaObject { if (processor.hasEmitted(ctx.path)) { return ctx.schema; }
for (const hook of extractorHooks) { const result = typeof hook === 'boolean' ? hook : (hook?.(ctx) ?? false); if (result) { process({ namingAnchor: processor.context.anchor, tags: processor.context.tags, ...ctx, }); return { $ref: pathToJsonPointer(ctx.path) }; } }
return ctx.schema; }
function process(ctx: ProcessorContext): PydanticFinal | void { if (!processor.markEmitted(ctx.path)) return;
const shouldExport = ctx.export !== false;
return processor.withContext({ anchor: ctx.namingAnchor, tags: ctx.tags }, () => { const visitor = createVisitor({ schemaExtractor: extractor }); const walk = createSchemaWalker(visitor);
const result = walk(ctx.schema, { path: ref(ctx.path), plugin, });
const final = visitor.applyModifiers(result, { path: ref(ctx.path), plugin, }) as PydanticFinal;
if (shouldExport) { exportAst({ ...ctx, final, plugin }); return; }
return final; }); }
return { process };}