function methodSchema()

in src/jsii2schema.ts [379:430]


function methodSchema(method: jsiiReflect.Callable, ctx: SchemaContext) {
  ctx = ctx.child('method', method.name);

  const fqn = `${method.parentType.fqn}.${method.name}`;

  const methodctx = ctx;

  return ctx.define(fqn, () => {
    const properties: any = { };
    const required = new Array<string>();

    const addProperty = (prop: jsiiReflect.Property | jsiiReflect.Parameter): void => {
      const param = schemaForTypeReference(prop.type, ctx);

      // bail out - can't serialize a required parameter, so we can't serialize the method
      if (!param && !prop.optional) {
        ctx.error('cannot schematize method because parameter cannot be schematized');
        return undefined;
      }

      properties[prop.name] = param;

      if (!prop.optional) {
        required.push(prop.name);
      }
    };

    for (let i = 0; i < method.parameters.length; ++i) {
      const p = method.parameters[i];
      methodctx.child('param', p.name);

      // if this is the last parameter and it's a data type, treat as keyword arguments
      if (i === method.parameters.length - 1 && isDataType(p.type.type)) {
        const kwargs = schemaForInterface(p.type.type, ctx);
        if (kwargs) {
          for (const prop of p.type.type.allProperties) {
            addProperty(prop);
          }
        }
      } else {
        addProperty(p);
      }
    }

    return {
      type: 'object',
      properties,
      additionalProperties: false,
      required: required.length > 0 ? required : undefined,
    };
  });
}