Skip to content

Commit 7940d1f

Browse files
Allow swagger docs that dont have defined schema refs. Right now whole app crashes if a schema is missing a $ref
1 parent 177f9fb commit 7940d1f

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

packages/handlers/openapi/src/openapi-to-graphql/oas_3_tools.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -629,9 +629,21 @@ export function getResponseSchemaAndNames<TSource, TContext, TArgs>(
629629
);
630630
let responseSchema = responseObject.content[availableSimilarContentType || contentTypes[0]].schema;
631631
let fromRef: string;
632-
if ('$ref' in responseSchema) {
633-
fromRef = responseSchema.$ref.split('/').pop();
634-
responseSchema = resolveRef(responseSchema.$ref, oas);
632+
633+
if (responseSchema) {
634+
if ('$ref' in responseSchema) {
635+
fromRef = responseSchema.$ref.split('/').pop();
636+
responseSchema = resolveRef(responseSchema.$ref, oas);
637+
}
638+
} else if (options.allowUndefinedSchemaRefTags) {
639+
options.logger.info(`${path}:${method.toUpperCase()}:${statusCode}`);
640+
fromRef = 'Unknown';
641+
responseSchema = {
642+
description: `Placeholder for missing ${path}:${method.toUpperCase()}:${statusCode} schema ref`,
643+
type: options.defaultUndefinedSchemaType || 'object',
644+
};
645+
} else {
646+
throw new Error(`${path}:${method.toUpperCase()}:${statusCode} has an undefined schema ref`);
635647
}
636648

637649
const responseSchemaNames = {

packages/handlers/openapi/src/openapi-to-graphql/types/options.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,4 +300,14 @@ export type InternalOptions<TSource, TContext, TArgs> = {
300300
includeHttpDetails?: boolean;
301301
pubsub: MeshPubSub;
302302
logger: Logger;
303+
304+
/**
305+
* Allow processing to continue if the swagger schema is missing a schema $ref.
306+
*/
307+
allowUndefinedSchemaRefTags?: boolean;
308+
309+
/**
310+
* Object type to use for missing swagger schemas refs default is object.
311+
*/
312+
defaultUndefinedSchemaType?: 'string' | 'number' | 'object' | 'array' | 'boolean' | 'integer';
303313
};

0 commit comments

Comments
 (0)