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
  • Loading branch information
ascheffe-kenna committed Jan 27, 2022
1 parent 177f9fb commit 7940d1f
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 @@ -629,9 +629,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';
};

0 comments on commit 7940d1f

Please sign in to comment.