export function schemaForPolymorphic()

in src/jsii2schema.ts [143:184]


export function schemaForPolymorphic(type: jsiiReflect.Type | undefined, ctx: SchemaContext) {
  if (!type) {
    return undefined;
  }

  ctx = ctx.child('polymorphic', type.fqn);

  const anyOf = new Array<any>();

  const parentctx = ctx;

  for (const x of allImplementationsOfType(type)) {

    ctx = parentctx.child('impl', x.fqn);

    const enumLike = schemaForEnumLikeClass(x, ctx);
    if (enumLike) {
      anyOf.push(enumLike);
    }

    if (x.initializer) {
      const methd = methodSchema(x.initializer, ctx);
      if (methd) {
        anyOf.push({
          type: 'object',
          additionalProperties: false,
          properties: {
            [x.fqn]: methd,
          },
        });
      }
    }
  }

  if (anyOf.length === 0) {
    return undefined;
  }

  return ctx.define(type.fqn, () => {
    return { anyOf };
  });
}