diff --git a/packages/orval/src/api.ts b/packages/orval/src/api.ts index 705a3cfbc..b33e4c3a9 100644 --- a/packages/orval/src/api.ts +++ b/packages/orval/src/api.ts @@ -31,7 +31,7 @@ export const getApiBuilder = async ({ context: ContextSpecs; }): Promise => { const api = await asyncReduce( - Object.entries(context.specs[context.specKey].paths), + Object.entries(context.specs[context.specKey].paths ?? {}), async (acc, [pathRoute, verbs]: [string, PathItemObject]) => { const route = getRoute(pathRoute); diff --git a/packages/orval/src/import-specs.ts b/packages/orval/src/import-specs.ts index 17cb284a1..0458aa50e 100644 --- a/packages/orval/src/import-specs.ts +++ b/packages/orval/src/import-specs.ts @@ -9,6 +9,8 @@ import { WriteSpecsBuilder, } from '@orval/core'; import chalk from 'chalk'; +import yaml from 'js-yaml'; +import fs from 'fs-extra'; import { importOpenApi } from './import-open-api'; @@ -16,28 +18,40 @@ const resolveSpecs = async ( path: string, { validate, ...options }: SwaggerParserOptions, isUrl: boolean, + isOnlySchema: boolean, ) => { - if (validate) { - try { - await SwaggerParser.validate(path, options); - } catch (e: any) { - if (e?.name === 'ParserError') { - throw e; + try { + if (validate) { + try { + await SwaggerParser.validate(path, options); + } catch (e: any) { + if (e?.name === 'ParserError') { + throw e; + } + + if (!isOnlySchema) { + log(`⚠️ ${chalk.yellow(e)}`); + } } - log(`⚠️ ${chalk.yellow(e)}`); } - } - const data = (await SwaggerParser.resolve(path, options)).values(); + const data = (await SwaggerParser.resolve(path, options)).values(); - if (isUrl) { - return data; - } + if (isUrl) { + return data; + } - // normalizing slashes after SwaggerParser - return Object.fromEntries( - Object.entries(data).map(([key, value]) => [upath.resolve(key), value]), - ); + // normalizing slashes after SwaggerParser + return Object.fromEntries( + Object.entries(data).map(([key, value]) => [upath.resolve(key), value]), + ); + } catch { + const file = await fs.readFile(path, 'utf8'); + + return { + [path]: yaml.load(file), + }; + } }; export const importSpecs = async ( @@ -58,7 +72,12 @@ export const importSpecs = async ( const isPathUrl = isUrl(input.target); - const data = await resolveSpecs(input.target, input.parserOptions, isPathUrl); + const data = await resolveSpecs( + input.target, + input.parserOptions, + isPathUrl, + !output.target, + ); return importOpenApi({ data, diff --git a/packages/orval/src/write-specs.ts b/packages/orval/src/write-specs.ts index 0eadf4a6c..b8195ccd7 100644 --- a/packages/orval/src/write-specs.ts +++ b/packages/orval/src/write-specs.ts @@ -40,7 +40,7 @@ export const writeSpecs = async ( options: NormalizedOptions, projectName?: string, ) => { - const { info, schemas, target } = builder; + const { info = { title: '', version: 0 }, schemas, target } = builder; const { output } = options; const projectTitle = projectName || info.title; @@ -53,7 +53,7 @@ export const writeSpecs = async ( return acc; }, {} as Record); - const header = getHeader(output.override.header, info); + const header = getHeader(output.override.header, info as InfoObject); if (output.schemas) { const rootSchemaPath = output.schemas;