Something went wrong. Try again.
fork of hey-api/openapi-ts because I need some additional things
Something went wrong. Try again.
5.6 kB · 219 lines
TypeScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220import type { SymbolMeta } from '@hey-api/codegen-core';import { fromRef, ref } from '@hey-api/codegen-core';import type { IR, SchemaWithType } from '@hey-api/shared';import { deduplicateSchema, pathToJsonPointer } from '@hey-api/shared';
// import { $ } from '../../../py-dsl';import type { Ast, IrSchemaToAstOptions } from '../shared/types';import type { PydanticPlugin } from '../types';import { createProcessor } from './processor';import { irSchemaWithTypeToAst } from './toAst';
export function irSchemaToAst({ optional, plugin, schema, schemaExtractor, state,}: IrSchemaToAstOptions & { optional?: boolean; schema: IR.SchemaObject;}): Ast { if (schemaExtractor && !schema.$ref) { const extracted = schemaExtractor({ meta: { resource: 'definition', resourceId: pathToJsonPointer(fromRef(state.path)), }, naming: plugin.config.definitions, path: fromRef(state.path), plugin, schema, }); if (extracted !== schema) schema = extracted; }
if (schema.$ref) { const query: SymbolMeta = { category: 'schema', resource: 'definition', resourceId: schema.$ref, tool: 'pydantic', }; const refSymbol = plugin.referenceSymbol(query); const refName = typeof refSymbol === 'string' ? refSymbol : refSymbol.name;
return { // expression: $.expr(refName), fieldConstraints: optional ? { default: null } : undefined, hasLazyExpression: !plugin.isSymbolRegistered(query), models: [], // pipes: [], typeAnnotation: refName, }; }
if (schema.type) { const typeAst = irSchemaWithTypeToAst({ plugin, schema: schema as SchemaWithType, state, });
const constraints: Record<string, unknown> = {}; if (optional) { constraints.default = null; } if (schema.default !== undefined) { constraints.default = schema.default; } if (schema.description) { constraints.description = schema.description; }
return { ...typeAst, fieldConstraints: { ...typeAst.fieldConstraints, ...constraints }, // pipes: [], }; }
if (schema.items) { schema = deduplicateSchema({ schema });
if (schema.items) { const itemsAnnotations: string[] = []; const itemsConstraints: Record<string, unknown>[] = [];
for (const item of schema.items) { const itemAst = irSchemaToAst({ plugin, schema: item, state: { ...state, path: ref([...fromRef(state.path), 'items']), }, }); itemsAnnotations.push(itemAst.typeAnnotation); if (itemAst.fieldConstraints) { itemsConstraints.push(itemAst.fieldConstraints); } }
const unionType = itemsAnnotations.join(' | '); return { // expression: $.expr(`list[${unionType}]`), fieldConstraints: itemsConstraints.length > 0 ? itemsConstraints[0] : undefined, hasLazyExpression: false, models: [], // pipes: [], typeAnnotation: `list[${unionType}]`, }; } }
return { // expression: $.expr('Any'), hasLazyExpression: false, models: [], // pipes: [], typeAnnotation: 'Any', };}
export const handlerV2: PydanticPlugin['Handler'] = ({ plugin }) => { plugin.symbol('Any', { external: 'typing', importKind: 'named', meta: { category: 'external', resource: 'typing.Any', }, }); plugin.symbol('BaseModel', { external: 'pydantic', importKind: 'named', meta: { category: 'external', resource: 'pydantic.BaseModel', }, }); plugin.symbol('ConfigDict', { external: 'pydantic', importKind: 'named', meta: { category: 'external', resource: 'pydantic.ConfigDict', }, }); plugin.symbol('Field', { external: 'pydantic', importKind: 'named', meta: { category: 'external', resource: 'pydantic.Field', }, }); plugin.symbol('Literal', { external: 'typing', importKind: 'named', meta: { category: 'external', resource: 'typing.Literal', }, }); plugin.symbol('Optional', { external: 'typing', importKind: 'named', meta: { category: 'external', resource: 'typing.Optional', }, });
const processor = createProcessor(plugin);
plugin.forEach('operation', 'parameter', 'requestBody', 'schema', 'webhook', (event) => { switch (event.type) { case 'parameter': processor.process({ meta: { resource: 'definition', resourceId: pathToJsonPointer(event._path), }, naming: plugin.config.definitions, path: event._path, plugin, schema: event.parameter.schema, tags: event.tags, }); break; case 'requestBody': processor.process({ meta: { resource: 'definition', resourceId: pathToJsonPointer(event._path), }, naming: plugin.config.definitions, path: event._path, plugin, schema: event.requestBody.schema, tags: event.tags, }); break; case 'schema': processor.process({ meta: { resource: 'definition', resourceId: pathToJsonPointer(event._path), }, naming: plugin.config.definitions, path: event._path, plugin, schema: event.schema, tags: event.tags, }); break; } });};