Skip to content

Commit

Permalink
Fixes on JSON Machete and OpenAPI handler for #3942
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan committed Aug 3, 2022
1 parent 78552ab commit a607449
Show file tree
Hide file tree
Showing 11 changed files with 2,005 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/hot-kids-share.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"json-machete": patch
---

Respect "required" field in anyOf and allOf schemas and fix the bug that puts an Any schema if this kind of schema is present
5 changes: 5 additions & 0 deletions .changeset/serious-bags-visit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@omnigraph/openapi": patch
---

Respect "\$ref" in parameters
5 changes: 5 additions & 0 deletions .changeset/silly-baboons-explain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"json-machete": patch
---

Use single "Any" schema for unknown types defined in "required" in order to prevent duplication
13 changes: 13 additions & 0 deletions .changeset/warm-pumas-warn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
"@omnigraph/openapi": patch
---

Respect global parameters object on top of method objects like;
```yml
parameters: # Take this as well
- name: foo
...
get:
parameters:
- name: bar
```
23 changes: 13 additions & 10 deletions packages/json-machete/src/healJSONSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ const JSONSchemaStringFormats = [
'uri',
];

const AnySchema = {
title: 'Any',
oneOf: [
{ type: 'string' },
{ type: 'integer' },
{ type: 'boolean' },
{ type: 'number' },
{ type: 'object', additionalProperties: true },
],
};

const titleResolvedRefReservedMap = new WeakMap<
JSONSchemaObject,
{
Expand Down Expand Up @@ -216,19 +227,11 @@ export async function healJSONSchema(
// Try to find the type
if (!subSchema.type) {
// If required exists without properties
if (subSchema.required && !subSchema.properties) {
if (subSchema.required && !subSchema.properties && !subSchema.anyOf && !subSchema.allOf) {
// Add properties
subSchema.properties = {};
for (const missingPropertyName of subSchema.required) {
subSchema.properties[missingPropertyName] = {
oneOf: [
{ type: 'string' },
{ type: 'integer' },
{ type: 'boolean' },
{ type: 'number' },
{ type: 'object', additionalProperties: true },
],
};
subSchema.properties[missingPropertyName] = AnySchema;
}
}
// Properties only exist in objects
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ export async function getJSONSchemaOptionsFromOpenAPIOptions({

for (const relativePath in oasOrSwagger.paths) {
const pathObj = oasOrSwagger.paths[relativePath];
const pathParameters = pathObj.parameters;
for (const method in pathObj) {
if (method === 'parameters') {
continue;
}
const methodObj = pathObj[method] as OpenAPIV2.OperationObject | OpenAPIV3.OperationObject;
const operationConfig = {
method: method.toUpperCase() as HTTPMethod,
Expand All @@ -77,8 +81,20 @@ export async function getJSONSchemaOptionsFromOpenAPIOptions({
responseByStatusCode: Record<string, JSONSchemaOperationResponseConfig>;
};
operations.push(operationConfig);
for (const paramObjIndex in methodObj.parameters) {
const paramObj = methodObj.parameters[paramObjIndex] as OpenAPIV2.ParameterObject | OpenAPIV3.ParameterObject;
let allParams;
if (methodObj.parameters && Array.isArray(methodObj.parameters)) {
allParams = [...(pathParameters || []), ...methodObj.parameters];
} else {
allParams = {
...(pathParameters || {}),
...((methodObj.parameters || {}) as any),
};
}
for (const paramObjIndex in allParams) {
let paramObj = allParams[paramObjIndex] as OpenAPIV2.ParameterObject | OpenAPIV3.ParameterObject;
if ('$ref' in paramObj) {
paramObj = resolvePath(paramObj.$ref.split('#')[1], oasOrSwagger);
}
const argName = sanitizeNameForGraphQL(paramObj.name);
switch (paramObj.in) {
case 'query':
Expand All @@ -102,6 +118,12 @@ export async function getJSONSchemaOptionsFromOpenAPIOptions({
requestSchema.required = requestSchema.required || [];
requestSchema.required.push(paramObj.name);
}
// Fix the reference
if (requestSchema.properties[paramObj.name].$ref?.startsWith('#')) {
requestSchema.properties[paramObj.name].$ref = `${oasFilePath}${
requestSchema.properties[paramObj.name].$ref
}`;
}
} else {
if (!operationConfig.path.includes('?')) {
operationConfig.path += '?';
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ type project_with_id {
}
input get_Status_request_Input {
globalquery: String!
limit: Int!
}
Expand All @@ -316,8 +317,7 @@ type Mutation {
\\"Used to test link parameters with variables\\"
postScanner(input: String, path: String!, query: String): getCopier_200_response
\\"Endpoint to test placeholder objects to wrap response objects.\\"
post_status(input: paths_status_post_requestBody_content_application_json_schema_Input): String
undefined: Any
post_status(input: paths_status_post_requestBody_content_application_json_schema_Input, globalquery: String!): String
\\"Add new contents to the trashcan of a specific owner\\"
postOfficeTrashCan(input: paths_trashcans__LEFT_CURLY_BRACE_username_RIGHT_CURLY_BRACE__post_requestBody_content_application_json_schema_Input, username: String!): trashcan
}
Expand Down Expand Up @@ -385,7 +385,5 @@ input paths_status_post_requestBody_content_application_json_schema_Input {
hello: String
}
scalar Any
scalar paths_trashcans__LEFT_CURLY_BRACE_username_RIGHT_CURLY_BRACE__post_requestBody_content_application_json_schema_Input"
`;
Loading

0 comments on commit a607449

Please sign in to comment.