Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function buildOperationArguments(
operationSpec: OperationObject,
request: Request,
pathParams: PathParameterValues,
body: RequestBody,
globalSchemas: SchemasObject,
options: RequestBodyValidationOptions = {},
): OperationArgs {
let requestBodyIndex = -1;
if (operationSpec.requestBody) {
// the type of `operationSpec.requestBody` could be `RequestBodyObject`
// or `ReferenceObject`, resolving a `$ref` value is not supported yet.
if (isReferenceObject(operationSpec.requestBody)) {
throw new Error('$ref requestBody is not supported yet.');
}
const i = operationSpec.requestBody[REQUEST_BODY_INDEX];
requestBodyIndex = i ? i : 0;
}
const paramArgs: OperationArgs = [];
for (const paramSpec of operationSpec.parameters ?? []) {
if (isReferenceObject(paramSpec)) {
// TODO(bajtos) implement $ref parameters
// See https://github.com/strongloop/loopback-next/issues/435
throw new Error('$ref parameters are not supported yet.');
}
const spec = paramSpec as ParameterObject;
const rawValue = getParamFromRequest(spec, request, pathParams);
private async _matchRequestBodySpec(
operationSpec: OperationObject,
request: Request,
) {
const requestBody: RequestBody = {
value: undefined,
};
if (!operationSpec.requestBody) return {requestBody};
const contentType = getContentType(request) ?? 'application/json';
debug('Loading request body with content type %j', contentType);
// the type of `operationSpec.requestBody` could be `RequestBodyObject`
// or `ReferenceObject`, resolving a `$ref` value is not supported yet.
if (isReferenceObject(operationSpec.requestBody)) {
throw new Error('$ref requestBody is not supported yet.');
}
let content = operationSpec.requestBody.content || {};
if (!Object.keys(content).length) {
content = {
// default to allow json and urlencoded
'application/json': {schema: {type: 'object'}},
'application/x-www-form-urlencoded': {schema: {type: 'object'}},
};
}
// Check of the request content type matches one of the expected media
// types in the request body spec
let matchedMediaType: string | false = false;
let customParser = undefined;
new HttpErrors.BadRequest('Request body is required'),
{
code: 'MISSING_REQUIRED_PARAMETER',
parameterName: 'request body',
},
);
throw err;
}
const schema = body.schema;
/* istanbul ignore if */
if (debug.enabled) {
debug('Request body schema:', util.inspect(schema, {depth: null}));
if (
schema &&
isReferenceObject(schema) &&
schema.$ref.startsWith('#/components/schemas/')
) {
const ref = schema.$ref.slice('#/components/schemas/'.length);
debug(' referencing:', util.inspect(globalSchemas[ref], {depth: null}));
}
}
if (!schema) return;
options = Object.assign({coerceTypes: !!body.coercionRequired}, options);
validateValueAgainstSchema(body.value, schema, globalSchemas, options);
}
): OperationArgs {
let requestBodyIndex = -1;
if (operationSpec.requestBody) {
// the type of `operationSpec.requestBody` could be `RequestBodyObject`
// or `ReferenceObject`, resolving a `$ref` value is not supported yet.
if (isReferenceObject(operationSpec.requestBody)) {
throw new Error('$ref requestBody is not supported yet.');
}
const i = operationSpec.requestBody[REQUEST_BODY_INDEX];
requestBodyIndex = i ? i : 0;
}
const paramArgs: OperationArgs = [];
for (const paramSpec of operationSpec.parameters ?? []) {
if (isReferenceObject(paramSpec)) {
// TODO(bajtos) implement $ref parameters
// See https://github.com/strongloop/loopback-next/issues/435
throw new Error('$ref parameters are not supported yet.');
}
const spec = paramSpec as ParameterObject;
const rawValue = getParamFromRequest(spec, request, pathParams);
const coercedValue = coerceParameter(rawValue, spec);
paramArgs.push(coercedValue);
}
debug('Validating request body - value %j', body);
validateRequestBody(body, operationSpec.requestBody, globalSchemas, options);
if (requestBodyIndex > -1) paramArgs.splice(requestBodyIndex, 0, body.value);
return paramArgs;
}
export function coerceParameter(
data: string | undefined | object,
spec: ParameterObject,
) {
const schema = spec.schema;
if (!schema || isReferenceObject(schema)) {
debug(
'The parameter with schema %s is not coerced since schema' +
'dereference is not supported yet.',
schema,
);
return data;
}
const OAIType = getOAIPrimitiveType(schema.type, schema.format);
const validator = new Validator({parameterSpec: spec});
validator.validateParamBeforeCoercion(data);
if (data === undefined) return data;
switch (OAIType) {
case 'byte':
return coerceBuffer(data, spec);