Skip to content

Commit

Permalink
Improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan committed Aug 29, 2022
1 parent 151ad23 commit 1088fda
Show file tree
Hide file tree
Showing 15 changed files with 809 additions and 99 deletions.
8 changes: 8 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const ROOT_DIR = __dirname;
const TSCONFIG = resolve(ROOT_DIR, 'tsconfig.json');
const tsconfig = require(TSCONFIG);

const ESM_PACKAGES = ['get-port'];

process.env.LC_ALL = 'en_US';

module.exports = {
Expand All @@ -24,4 +26,10 @@ module.exports = {
collectCoverage: false,
cacheDirectory: resolve(ROOT_DIR, `${CI ? '' : 'node_modules/'}.cache/jest`),
extensionsToTreatAsEsm: ['.ts'],
transform: {
'^.+\\.mjs?$': 'babel-jest',
'^.+\\.ts?$': 'babel-jest',
'^.+\\.js$': 'babel-jest',
},
transformIgnorePatterns: [`node_modules/(?!(${ESM_PACKAGES.join('|')})/)`],
};
1 change: 1 addition & 0 deletions packages/loaders/openapi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"body-parser": "1.20.0",
"cookie-parser": "1.4.6",
"express": "4.18.1",
"get-port": "6.1.2",
"json-bigint-patch": "0.0.8",
"multer": "1.4.5-lts.1"
},
Expand Down
100 changes: 62 additions & 38 deletions packages/loaders/openapi/src/getJSONSchemaOptionsFromOpenAPIOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,47 +95,25 @@ export async function getJSONSchemaOptionsFromOpenAPIOptions(

oasOrSwagger = (await dereferenceObject(oasOrSwagger)) as any;
const operations: JSONSchemaOperationConfig[] = [];
let baseOperationArgTypeMap: Record<string, JSONSchemaObject>;

function handleCallback(callbackKey: string, callbackObj: OpenAPIV3.CallbackObject) {
for (const callbackUrlRefKey in callbackObj) {
if (callbackUrlRefKey.startsWith('$')) {
continue;
}
const pubsubTopic = callbackUrlRefKey.split('$request.query').join('args').split('$request.body#/').join('args.');
const callbackOperationConfig: JSONSchemaPubSubOperationConfig = {
type: OperationTypeNode.SUBSCRIPTION,
field: '',
pubsubTopic,
};
const callbackUrlObj = callbackObj[callbackUrlRefKey];
for (const method in callbackUrlObj) {
const callbackOperation: OpenAPIV3.OperationObject = callbackUrlObj[method];
callbackOperationConfig.field = callbackOperation.operationId;
callbackOperationConfig.description = callbackOperation.description || callbackOperation.summary;
const requestBodyContents = (callbackOperation.requestBody as OpenAPIV3.RequestBodyObject)?.content;
if (requestBodyContents) {
callbackOperationConfig.responseSchema = requestBodyContents[Object.keys(requestBodyContents)[0]]
.schema as any;
}
const responses = callbackOperation.responses;
if (responses) {
const response = responses[Object.keys(responses)[0]];
if (response) {
const responseContents = (response as OpenAPIV3.ResponseObject).content;
if (responseContents) {
callbackOperationConfig.requestSchema = responseContents[Object.keys(responseContents)[0]].schema as any;
}
if (!baseUrl) {
if ('servers' in oasOrSwagger) {
const serverObj = oasOrSwagger.servers[0];
baseUrl = serverObj.url.split('{').join('{args.');
if (serverObj.variables) {
for (const variableName in serverObj.variables) {
const variable = serverObj.variables[variableName];
if (!(variable as JSONSchemaObject).type) {
(variable as JSONSchemaObject).type = 'string';
}
baseOperationArgTypeMap = baseOperationArgTypeMap || {};
baseOperationArgTypeMap[variableName] = variable as JSONSchemaObject;
if (variable.default) {
baseUrl = baseUrl.replace(`{args.${variableName}}`, `{args.${variableName}:${variable.default}}`);
}
}
}
callbackOperationConfig.field = callbackOperationConfig.field || sanitizeNameForGraphQL(callbackKey);
operations.push(callbackOperationConfig);
}
}

if (!baseUrl) {
if ('servers' in oasOrSwagger) {
baseUrl = oasOrSwagger.servers[0].url;
}

if ('schemes' in oasOrSwagger && oasOrSwagger.schemes.length > 0 && oasOrSwagger.host) {
Expand Down Expand Up @@ -169,6 +147,13 @@ export async function getJSONSchemaOptionsFromOpenAPIOptions(
schemaHeaders,
operationHeaders,
responseByStatusCode: {},
...(baseOperationArgTypeMap
? {
argTypeMap: {
...baseOperationArgTypeMap,
},
}
: {}),
} as OperationConfig;
operations.push(operationConfig);
methodObjFieldMap.set(methodObj, operationConfig);
Expand Down Expand Up @@ -472,7 +457,46 @@ export async function getJSONSchemaOptionsFromOpenAPIOptions(

if ('callbacks' in methodObj) {
for (const callbackKey in methodObj.callbacks) {
handleCallback(callbackKey, methodObj.callbacks[callbackKey] as OpenAPIV3.CallbackObject);
const callbackObj = methodObj.callbacks[callbackKey] as OpenAPIV3.CallbackObject;
for (const callbackUrlRefKey in callbackObj) {
if (callbackUrlRefKey.startsWith('$')) {
continue;
}
const pubsubTopic = callbackUrlRefKey
.split('$request.query')
.join('args')
.split('$request.body#/')
.join('args.');
const callbackOperationConfig: JSONSchemaPubSubOperationConfig = {
type: OperationTypeNode.SUBSCRIPTION,
field: '',
pubsubTopic,
};
const callbackUrlObj = callbackObj[callbackUrlRefKey];
for (const method in callbackUrlObj) {
const callbackOperation: OpenAPIV3.OperationObject = callbackUrlObj[method];
callbackOperationConfig.field = callbackOperation.operationId;
callbackOperationConfig.description = callbackOperation.description || callbackOperation.summary;
const requestBodyContents = (callbackOperation.requestBody as OpenAPIV3.RequestBodyObject)?.content;
if (requestBodyContents) {
callbackOperationConfig.responseSchema = requestBodyContents[Object.keys(requestBodyContents)[0]]
.schema as any;
}
const responses = callbackOperation.responses;
if (responses) {
const response = responses[Object.keys(responses)[0]];
if (response) {
const responseContents = (response as OpenAPIV3.ResponseObject).content;
if (responseContents) {
callbackOperationConfig.requestSchema = responseContents[Object.keys(responseContents)[0]]
.schema as any;
}
}
}
}
callbackOperationConfig.field = callbackOperationConfig.field || sanitizeNameForGraphQL(callbackKey);
operations.push(callbackOperationConfig);
}
}
}

Expand Down
Loading

0 comments on commit 1088fda

Please sign in to comment.