How to use openapi-request-coercer - 3 common examples

To help you get started, we’ve selected a few openapi-request-coercer examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github kogosoftwarellc / open-api / packages / openapi-framework / index.ts View on Github external
`${this.loggingPrefix}request validator on for`,
                  methodName,
                  openapiPath
                );
              }

              if (
                allowsCoercionFeature(
                  this,
                  this.apiDoc,
                  pathModule,
                  pathDoc,
                  operationDoc
                )
              ) {
                const coercer = new OpenAPIRequestCoercer({
                  extensionBase: `x-${this.name}-coercion`,
                  loggingKey: `${this.name}-coercion`,
                  parameters: methodParameters,
                  enableObjectCoercion: this.enableObjectCoercion
                });

                operationContext.features.coercer = coercer;
              }

              // no point in default feature if we don't have any parameters with defaults.
              if (
                methodParameters.filter(byDefault).length &&
                allowsDefaultsFeature(
                  this,
                  this.apiDoc,
                  pathModule,
github aimed / slushy / server / src / middleware / RequestCoercionMiddlewareFactory.ts View on Github external
if (!path) {
            throw new Error('RequestCoercionMiddlewareFactory requires path')
        }

        if (!operation) {
            throw new Error('RequestCoercionMiddlewareFactory requires operation')
        }

        let operationObject: OpenAPIV3.OperationObject
        try {
            operationObject = getOperationObject(props.openApi, path, operation)
        } catch (error) {
            return []
        }

        const coercer = new OpenApiRequestCoercer({
            enableObjectCoercion: true,
            parameters: operationObject.parameters || [],
        })

        return [
            (req, _res, next) => {
                coercer.coerce(req)
                next()
            },
        ]
    }
}
github inaturalist / iNaturalistAPI / openapi_app.js View on Github external
"multipart/form-data": ( req, res, next ) => {
      const knownUploadFields = [];
      const { properties } = req.operationDoc.requestBody.content["multipart/form-data"].schema;
      _.each( properties, ( schema, name ) => {
        if ( schema.type === "string" && schema.format === "binary" ) {
          knownUploadFields.push( name );
        }
      } );
      const props = req.operationDoc.requestBody.content["multipart/form-data"].schema.properties;
      const parameters = _.map( _.keys( props ), k => ( {
        in: "formData",
        name: k,
        schema: req.operationDoc.requestBody.content["multipart/form-data"].schema.properties[k]
      } ) );
      const coercer = new openapiCoercer.default( {
        extensionBase: "x-express-openapi-coercion",
        loggingKey: "express-openapi-coercion",
        parameters,
        enableObjectCoercion: true
      } );
      coercer.coerce( req );

      upload.fields( _.map( knownUploadFields, f => ( { name: f } ) ) )( req, res, err => {
        const originalBody = _.cloneDeep( req.body );
        const newBody = { };
        _.each( originalBody, ( v, k ) => {
          if ( _.isObject( v ) ) {
            newBody[k] = { };
            _.each( v, ( vv, kk ) => {
              if ( vv !== "" ) {
                newBody[k][kk] = vv;

openapi-request-coercer

Coerce request properties according to an openapi parameter list.

MIT
Latest version published 1 year ago

Package Health Score

62 / 100
Full package analysis

Popular openapi-request-coercer functions