extract basic OpenAPI links (#2624)

This commit is contained in:
François Mockers 2024-08-23 21:11:45 +02:00 committed by GitHub
parent 25e57d2578
commit fd6b3630a5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -59,12 +59,15 @@ const transformOpenapiRequestItem = (request) => {
operationName = `${request.method} ${request.path}`;
}
// replace OpenAPI links in path by Bruno variables
let path = request.path.replace(/{([a-zA-Z]+)}/g, `{{${_operationObject.operationId}_$1}}`);
const brunoRequestItem = {
uid: uuid(),
name: operationName,
type: 'http-request',
request: {
url: ensureUrl(request.global.server + '/' + request.path),
url: ensureUrl(request.global.server + '/' + path),
method: request.method.toUpperCase(),
auth: {
mode: 'none',
@ -81,6 +84,9 @@ const transformOpenapiRequestItem = (request) => {
xml: null,
formUrlEncoded: [],
multipartForm: []
},
script: {
res: null
}
}
};
@ -195,6 +201,26 @@ const transformOpenapiRequestItem = (request) => {
}
}
// build the extraction scripts from responses that have links
// https://swagger.io/docs/specification/links/
let script = [];
each(_operationObject.responses || [], (response, responseStatus) => {
if (Object.hasOwn(response, 'links')) {
// only extract if the status code matches the response
script.push(`if (res.status === ${responseStatus}) {`);
each(response.links, (link) => {
each(link.parameters || [], (expression, parameter) => {
let value = openAPIRuntimeExpressionToScript(expression);
script.push(` bru.setVar('${link.operationId}_${parameter}', ${value});`);
});
});
script.push(`}`);
}
});
if (script.length > 0) {
brunoRequestItem.request.script.res = script.join('\n');
}
return brunoRequestItem;
};
@ -305,6 +331,18 @@ const getSecurity = (apiSpec) => {
};
};
const openAPIRuntimeExpressionToScript = (expression) => {
// see https://swagger.io/docs/specification/links/#runtime-expressions
if (expression === '$response.body') {
return 'res.body';
} else if (expression.startsWith('$response.body#')) {
let pointer = expression.substring(15);
// could use https://www.npmjs.com/package/json-pointer for better support
return `res.body${pointer.replace('/', '.')}`;
}
return expression;
};
const parseOpenApiCollection = (data) => {
const brunoCollection = {
name: '',