Skip to content

Commit

Permalink
fix(schemas): handling correctly non openapi file entry
Browse files Browse the repository at this point in the history
  • Loading branch information
anymaniax committed Apr 21, 2023
1 parent 94e2c8e commit be77eb2
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 20 deletions.
2 changes: 1 addition & 1 deletion packages/orval/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const getApiBuilder = async ({
context: ContextSpecs;
}): Promise<GeneratorApiBuilder> => {
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);

Expand Down
53 changes: 36 additions & 17 deletions packages/orval/src/import-specs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,49 @@ 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';

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 (
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions packages/orval/src/write-specs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -53,7 +53,7 @@ export const writeSpecs = async (
return acc;
}, {} as Record<keyof typeof schemas, string>);

const header = getHeader(output.override.header, info);
const header = getHeader(output.override.header, info as InfoObject);

if (output.schemas) {
const rootSchemaPath = output.schemas;
Expand Down

0 comments on commit be77eb2

Please sign in to comment.