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 and ardatan committed Aug 3, 2022
1 parent 961e071 commit 945f498
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 4 deletions.
18 changes: 18 additions & 0 deletions .changeset/spotty-avocados-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
"@graphql-mesh/openapi": minor
---

Allow swagger docs that don't have defined schema refs. Right now whole app crashes if a schema is missing a $ref;


```ts
/**
* 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';
```
2 changes: 2 additions & 0 deletions packages/handlers/openapi/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ export default class OpenAPIHandler implements MeshHandler {

return originalFactory(() => resolverParams, this.logger)(root, args, context, info);
},
allowUndefinedSchemaRefTags: this.config.allowUndefinedSchemaRefTags,
defaultUndefinedSchemaType: this.config.defaultUndefinedSchemaType,
});

const { args, contextVariables } = parseInterpolationStrings(Object.values(operationHeaders || {}));
Expand Down
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';
};
17 changes: 17 additions & 0 deletions packages/handlers/openapi/yaml-config.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ type OpenapiHandler @md {
This option forces OpenAPI handler to only create field names based on the operationId.
"""
operationIdFieldNames: Boolean
"""
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: UndefinedSchemaType
}

enum SourceFormat {
Expand Down Expand Up @@ -87,3 +95,12 @@ enum QueryOrMutation {
Query
Mutation
}

enum UndefinedSchemaType {
string
number
object
array
boolean
integer
}
9 changes: 9 additions & 0 deletions packages/types/src/config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1995,6 +1995,15 @@
"operationIdFieldNames": {
"type": "boolean",
"description": "Field names can only be sanitized operationIds\nBy default, query field names are based on the return type type name and mutation field names are based on the operationId, which may be generated if it does not exist.\nThis option forces OpenAPI handler to only create field names based on the operationId."
},
"allowUndefinedSchemaRefTags": {
"type": "boolean",
"description": "Allow processing to continue if the swagger schema is missing a schema $ref."
},
"defaultUndefinedSchemaType": {
"type": "string",
"enum": ["string", "number", "object", "array", "boolean", "integer"],
"description": "Object type to use for missing swagger schemas refs default is object. (Allowed values: string, number, object, array, boolean, integer)"
}
},
"required": ["source"]
Expand Down
8 changes: 8 additions & 0 deletions packages/types/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,14 @@ export interface OpenapiHandler {
* This option forces OpenAPI handler to only create field names based on the operationId.
*/
operationIdFieldNames?: boolean;
/**
* 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. (Allowed values: string, number, object, array, boolean, integer)
*/
defaultUndefinedSchemaType?: 'string' | 'number' | 'object' | 'array' | 'boolean' | 'integer';
}
export interface SelectQueryOrMutationFieldConfig {
/**
Expand Down
4 changes: 3 additions & 1 deletion website/src/generated-markdown/OpenapiHandler.generated.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ Overrides the server object in the OAS.
* `provideErrorExtensions` (type: `Boolean`) - Overwrite automatic wrapping of errors into GraphqlErrors
* `operationIdFieldNames` (type: `Boolean`) - Field names can only be sanitized operationIds
By default, query field names are based on the return type type name and mutation field names are based on the operationId, which may be generated if it does not exist.
This option forces OpenAPI handler to only create field names based on the operationId.
This option forces OpenAPI handler to only create field names based on the operationId.
* `allowUndefinedSchemaRefTags` (type: `Boolean`) - Allow processing to continue if the swagger schema is missing a schema $ref.
* `defaultUndefinedSchemaType` (type: `String (string | number | object | array | boolean | integer)`) - Object type to use for missing swagger schemas refs default is object.

1 comment on commit 945f498

@vercel
Copy link

@vercel vercel bot commented on 945f498 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.