Azure / typespec-azure

About TypeSpec Azure Libraries
https://azure.github.io/typespec-azure/
MIT License
9 stars 30 forks source link

Problem to decorate resource key property with `@path` #103

Open tadelesh opened 6 months ago

tadelesh commented 6 months ago

Currently, we will use @path to decorate the resource key property to make the generated swagger right. e.g.,

@doc("A ContosoProviderHub resource")
model Employee is TrackedResource<EmployeeProperties> {
  @doc("Name of employee")
  @pattern("^[a-zA-Z0-9-]{3,24}$")
  @key("employeeName")
  @path
  @segment("employees")
  name: string;
}
    "Employee": {
      "type": "object",
      "description": "A ContosoProviderHub resource",
      "properties": {
        "properties": {
          "$ref": "#/definitions/EmployeeProperties",
          "description": "The resource-specific properties for this resource.",
          "x-ms-client-flatten": true,
          "x-ms-mutability": [
            "read",
            "create"
          ]
        }
      },
      "allOf": [
        {
          "$ref": "../../common-types/resource-management/v5/types.json#/definitions/TrackedResource"
        }
      ]
    }

But for TSP ARM template, TrackedResource does not contain name property, while emitter will filter any property with @path, thus result to the final generated model losing the name property. This will not happen for .NET as they will have core resource model, but for some languages such as Go will generate resource model directly from the TSP definition.

/**
 * Base model that defines common properties for all ARM resources.
 */
@doc("Common properties for all ARM resources.")
model ArmResource extends ArmResourceBase {
  @doc("Fully qualified resource ID for the resource. Ex - /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{resourceProviderNamespace}/{resourceType}/{resourceName}")
  @visibility("read")
  id: string;

  // The name property must be included by the resource type author!

  @doc("The type of the resource. E.g. \"Microsoft.Compute/virtualMachines\" or \"Microsoft.Storage/storageAccounts\"")
  @visibility("read")
  type: string;

  @doc("Azure Resource Manager metadata containing createdBy and modifiedBy information.")
  @visibility("read")
  systemData?: SystemData;
}

/** Base class used for type definitions */
model ArmResourceBase {}

/**
 * The base tracked resource.
 */
@armCommonDefinition("TrackedResource", Azure.ResourceManager.CommonTypes.Versions.v3)
@armCommonDefinition("TrackedResource", Azure.ResourceManager.CommonTypes.Versions.v4)
@armCommonDefinition("TrackedResource", Azure.ResourceManager.CommonTypes.Versions.v5)
@doc("The resource model definition for an Azure Resource Manager tracked top level resource")
model TrackedResourceBase extends ArmResource {
  @doc("The geo-location where the resource lives")
  @visibility("read", "create")
  location: string;

  ...ArmTagsProperty;
}
markcowl commented 6 months ago

We may be able to address this with the resolution to this issue: https://github.com/Azure/typespec-azure-pr/issues/3873