export function getPropertyDecorators()

in packages/extensions/openapi-to-typespec/src/utils/decorators.ts [72:193]


export function getPropertyDecorators(element: Property | Parameter): TypespecDecorator[] {
  const { isFullCompatible } = getOptions();
  const decorators: TypespecDecorator[] = [];

  const paging = element.language.default.paging ?? {};

  if (!isParameter(element)) {
    const visibility = getPropertyVisibility(element);
    if (visibility.length) {
      decorators.push({
        name: "visibility",
        arguments: visibility.map((v) => ({ value: v, options: { unwrap: true } })),
      });
    }
  }

  if (paging.isNextLink) {
    decorators.push({ name: "nextLink" });
  }

  if (paging.isValue) {
    decorators.push({ name: "items" });
  }

  if (element.schema.type === SchemaType.Credential) {
    decorators.push({ name: "secret" });
  }

  getNumberSchemaDecorators(element.schema, decorators);
  getStringSchemaDecorators(element.schema, decorators);
  getUnixTimeSchemaDecorators(element.schema, decorators);
  getUuidSchemaDecorators(element.schema, decorators);

  if (element.language.default.isResourceKey) {
    decorators.push({
      name: "key",
      fixMe: [
        "// FIXME: (resource-key-guessing) - Verify that this property is the resource key, if not please update the model with the right one",
      ],
    });
  }

  if (isParameter(element) && element?.protocol?.http?.in) {
    const location = element.protocol.http.in === "body" ? "bodyRoot" : element.protocol.http.in;
    const locationDecorator: TypespecDecorator = { name: location };

    if (location === "query") {
      locationDecorator.arguments = [element.language.default.serializedName];
      if (element.schema.type === SchemaType.Array) {
        let format = "multi";
        switch (element.protocol.http?.style) {
          case SerializationStyle.Form:
            if (!element.protocol.http?.explode) {
              format = "csv";
            }
            break;
          case SerializationStyle.PipeDelimited:
            format = "pipes";
            break;
          case SerializationStyle.Simple:
            format = "csv";
            break;
          case SerializationStyle.SpaceDelimited:
            format = "ssv";
            break;
          case SerializationStyle.TabDelimited:
            format = "tsv";
            break;
        }
        locationDecorator.arguments =
          format === "multi"
            ? [
                {
                  value: `#{ name: "${element.language.default.serializedName}", explode: true }`,
                  options: { unwrap: true },
                },
              ]
            : [
                {
                  value:
                    format === "csv"
                      ? `"${element.language.default.serializedName}"`
                      : `{name: "${element.language.default.serializedName}", format: "${format}"}`,
                  options: { unwrap: true },
                },
              ];

        if (isFullCompatible && locationDecorator.name === "query" && format === "multi") {
          locationDecorator.suppressionCode = "@azure-tools/typespec-azure-core/no-query-explode";
          locationDecorator.suppressionMessage = "For backward compatibility";
        }
      }
    }

    decorators.push(locationDecorator);
  }

  if (!isParameter(element) && element.extensions?.["x-ms-identifiers"]?.length >= 0) {
    decorators.push({
      name: "OpenAPI.extension",
      arguments: [
        { value: "x-ms-identifiers", options: { unwrap: false } },
        {
          value: `#[${element.extensions!["x-ms-identifiers"].map((v: any) => `"${v}"`).join(", ")}]`,
          options: { unwrap: true },
        },
      ],
      //namespace: "TypeSpec.OpenAPI",
      //module: "@typespec/openapi",
    });
  }

  if (element.extensions?.["x-ms-client-flatten"] && isFullCompatible) {
    decorators.push({
      name: "Azure.ResourceManager.Private.conditionalClientFlatten",
      suppressionCode: "@azure-tools/typespec-azure-core/no-private-usage",
      suppressionMessage: "For backward compatibility",
    });
  }

  return decorators;
}