How to use the @loopback/core.MetadataInspector.getDesignTypeForMethod function in @loopback/core

To help you get started, we’ve selected a few @loopback/core 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 strongloop / loopback-next / packages / openapi-v3 / src / controller-spec.ts View on Github external
}

    if (!spec.paths[path]) {
      spec.paths[path] = {};
    }

    if (spec.paths[path][verb]) {
      // Operations from subclasses override those from the base
      debug(`  Overriding ${endpointName} - endpoint was already defined`);
    }

    debug(`  adding ${endpointName}`, operationSpec);
    spec.paths[path][verb] = operationSpec;

    debug(`  inferring schema object for method %s`, op);
    const opMetadata = MetadataInspector.getDesignTypeForMethod(
      constructor.prototype,
      op,
    );
    const paramTypes = opMetadata.parameterTypes;

    const isComplexType = (ctor: Function) =>
      !includes([String, Number, Boolean, Array, Object], ctor);

    for (const p of paramTypes) {
      if (isComplexType(p)) {
        generateOpenAPISchema(spec, p);
      }
    }
  }
  return spec;
}
github strongloop / loopback-next / packages / openapi-v3 / src / decorators / parameter.decorator.ts View on Github external
return function(target: object, member: string, index: number) {
    paramSpec = paramSpec || {};
    // Get the design time method parameter metadata
    const methodSig = MetadataInspector.getDesignTypeForMethod(target, member);
    const paramTypes = methodSig?.parameterTypes || [];

    // Map design-time parameter type to the OpenAPI param type

    const paramType = paramTypes[index];

    if (paramType) {
      if (
        // generate schema if `paramSpec` doesn't have it
        !paramSpec.schema ||
        // generate schema if `paramSpec` has `schema` but without `type`
        (isSchemaObject(paramSpec.schema) && !paramSpec.schema.type)
      ) {
        // please note `resolveSchema` only adds `type` and `format` for `schema`
        paramSpec.schema = resolveSchema(paramType, paramSpec.schema);
      }
github strongloop / loopback-next / packages / openapi-v3 / src / decorators / request-body.decorator.ts View on Github external
return function(target: object, member: string, index: number) {
    debug('@requestBody() on %s.%s', target.constructor.name, member);
    debug('  parameter index: %s', index);
    /* istanbul ignore if */
    if (debug.enabled)
      debug('  options: %s', inspect(requestBodySpec, {depth: null}));

    // Use 'application/json' as default content if `requestBody` is undefined
    requestBodySpec = requestBodySpec ?? {content: {}};

    if (_.isEmpty(requestBodySpec.content))
      requestBodySpec.content = {'application/json': {}};

    // Get the design time method parameter metadata
    const methodSig = MetadataInspector.getDesignTypeForMethod(target, member);
    const paramTypes = methodSig?.parameterTypes || [];

    const paramType = paramTypes[index];
    const schema = resolveSchema(paramType);
    /* istanbul ignore if */
    if (debug.enabled)
      debug('  inferred schema: %s', inspect(schema, {depth: null}));
    requestBodySpec.content = _.mapValues(requestBodySpec.content, c => {
      if (!c.schema) {
        c.schema = schema;
      }
      return c;
    });

    // The default position for request body argument is 0
    // if not, add extension 'x-parameter-index' to specify the position