function getSchemaForArrayModel()

in packages/typespec-powershell/src/utils/modelUtils.ts [1193:1291]


function getSchemaForArrayModel(
  dpgContext: SdkContext,
  type: Model,
  options?: GetSchemaOptions
) {
  const { program } = dpgContext;
  const { indexer } = type;
  const {
    usage,
    isParentRequestBody,
    mediaTypes: contentTypes
  } = options ?? {};
  let schema: any = {};
  if (!indexer) {
    return schema;
  }
  if (isArrayModelType(program, type)) {
    // schema = {
    //   type: "array",
    //   elementType: getSchemaForType(dpgContext, indexer.value!, {
    //     usage,
    //     isRequestBody: false,
    //     mediaTypes: contentTypes,
    //     // special handling for array in formdata
    //     // isParentRequestBody: hasMediaType(
    //     //   KnownMediaType.MultipartFormData,
    //     //   contentTypes
    //     // )
    //     //   ? isParentRequestBody
    //     //   : false,
    //     needRef: !isAnonymousModelType(indexer.value!)
    //   }),
    //   description: getDoc(program, type)
    // };
    schema = new ArraySchema("", "", getSchemaForType(dpgContext, indexer.value!));
    // circle reference, resolve it later
    if (!getSchemaForType(dpgContext, indexer.value!)) {
      schema.delayType = indexer.value!;
    }
    if (
      !program.checker.isStdType(indexer.value) &&
      !isUnknownType(indexer.value!) &&
      indexer.value?.kind &&
      schema.items?.name &&
      !schema.items.enum
    ) {
      schema.typeName = `Array<${schema.items.name}>`;
      if (usage && usage.includes(SchemaContext.Output)) {
        schema.outputTypeName = `Array<${schema.items.name}Output>`;
      }
    } else {
      if (schema.items?.typeName) {
        if (schema.items.type === "dictionary") {
          schema.typeName = `${schema.items.typeName}[]`;
        } else if (schema.items.type === "union") {
          schema.typeName = `(${schema.items.typeName})[]`;
        } else if (
          schema.items.typeName.includes(BINARY_TYPE_UNION) &&
          schema.items.type === "string"
        ) {
          schema.typeName = `(${schema.items.typeName})[]`;
          if (usage && usage.includes(SchemaContext.Output)) {
            schema.outputTypeName = `(${schema.items.outputTypeName})[]`;
          }
        } else if (isAnonymousObjectSchema(schema.items)) {
          schema.typeName = `${schema.items.typeName}[]`;
          if (usage && usage.includes(SchemaContext.Output)) {
            schema.outputTypeName = `${schema.items.outputTypeName}[]`;
          }
        } else {
          schema.typeName = schema.items.typeName
            .split("|")
            .map((typeName: string) => {
              return `${typeName}[]`;
            })
            .join(" | ");
          if (
            schema.items.outputTypeName &&
            usage &&
            usage.includes(SchemaContext.Output)
          ) {
            schema.outputTypeName = schema.items.outputTypeName
              .split("|")
              .map((typeName: string) => {
                return `${typeName}[]`;
              })
              .join(" | ");
          }
        }
      } else if ((schema.items?.type || []).includes("|")) {
        schema.typeName = `(${schema.items.type})[]`;
      } else {
        schema.typeName = `${schema.items?.type}[]`;
      }
    }
    schema.usage = usage;
    return schema;
  }
}