Skip to content

Commit

Permalink
Allow swagger docs that dont have defined schema refs. Right now whol…
Browse files Browse the repository at this point in the history
…e app crashes if a schema is missing a $ref (#3551)
  • Loading branch information
ascheffe-kenna authored Aug 3, 2022
1 parent 961e071 commit 62d78fb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
18 changes: 15 additions & 3 deletions packages/handlers/openapi/src/openapi-to-graphql/oas_3_tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -635,9 +635,21 @@ export function getResponseSchemaAndNames<TSource, TContext, TArgs>(
);
let responseSchema = responseObject.content[availableSimilarContentType || contentTypes[0]].schema;
let fromRef: string;
if ('$ref' in responseSchema) {
fromRef = responseSchema.$ref.split('/').pop();
responseSchema = resolveRef(responseSchema.$ref, oas);

if (responseSchema) {
if ('$ref' in responseSchema) {
fromRef = responseSchema.$ref.split('/').pop();
responseSchema = resolveRef(responseSchema.$ref, oas);
}
} else if (options.allowUndefinedSchemaRefTags) {
options.logger.info(`${path}:${method.toUpperCase()}:${statusCode}`);
fromRef = 'Unknown';
responseSchema = {
description: `Placeholder for missing ${path}:${method.toUpperCase()}:${statusCode} schema ref`,
type: options.defaultUndefinedSchemaType || 'object',
};
} else {
throw new Error(`${path}:${method.toUpperCase()}:${statusCode} has an undefined schema ref`);
}

const responseSchemaNames = {
Expand Down
10 changes: 10 additions & 0 deletions packages/handlers/openapi/src/openapi-to-graphql/types/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,4 +300,14 @@ export type InternalOptions<TSource, TContext, TArgs> = {
includeHttpDetails?: boolean;
pubsub: MeshPubSub;
logger: Logger;

/**
* Allow processing to continue if the swagger schema is missing a schema $ref.
*/
allowUndefinedSchemaRefTags?: boolean;

/**
* Object type to use for missing swagger schemas refs default is object.
*/
defaultUndefinedSchemaType?: 'string' | 'number' | 'object' | 'array' | 'boolean' | 'integer';
};

1 comment on commit 62d78fb

@vercel
Copy link

@vercel vercel bot commented on 62d78fb Aug 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.