microsoft / typespec

https://typespec.io/
MIT License
4.1k stars 194 forks source link

[Bug]: Client Name Overrides are failing if the property is inherited(alias) from parent #3240

Open sarangan12 opened 4 months ago

sarangan12 commented 4 months ago

Describe the bug

In the Typescript SDK Generation, I am trying to override the names of some objects/models generated. But, the compiler is not able to find the properties of the object when the properties are inherited from the parent.

Reproduction

The following code does not work. Playground Link is here.

import "@typespec/http";
import "@typespec/rest";
import "@typespec/versioning";
import "@azure-tools/typespec-azure-core";
import "@azure-tools/typespec-client-generator-core";

using Azure.ClientGenerator.Core;
using TypeSpec.Http;
using TypeSpec.Rest;
using TypeSpec.Versioning;
using Azure.Core;
using Azure.Core.Traits;

model A {
  ...KeyVaultCertificateBase
}

alias KeyVaultCertificateBase = {
 NBF?: float32
};

@@clientName(A.NBF, "nbf");
@@clientName(A.NBF, "nbf", "javascript");

But the following code works:

import "@typespec/http";
import "@typespec/rest";
import "@typespec/versioning";
import "@azure-tools/typespec-azure-core";
import "@azure-tools/typespec-client-generator-core";

using Azure.ClientGenerator.Core;
using TypeSpec.Http;
using TypeSpec.Rest;
using TypeSpec.Versioning;
using Azure.Core;
using Azure.Core.Traits;

model A {
  NBF?: float32;
}

@@clientName(A.NBF, "nbf");

In the first code, the property is inherited. Only in such cases, the compiler errors out with the following message.

C:\Users\......>npx tsp compile client.tsp
TypeSpec compiler v0.55.0

Diagnostics were reported during compilation:

C:/Users/..../propertyNameOverride.tsp:429:93 - error invalid-ref: Model doesn't have member nBF
> 429 | @@clientName(Microsoft.EventGrid.SystemEvents.KeyVaultCertificateNewVersionCreatedEventData.nBF,
      |                                                                                             ^^^

Found 1 error

Please help me to resolve this issue.

Additional Info Please note the following code works:

import "@typespec/http";
import "@typespec/rest";
import "@typespec/versioning";
import "@azure-tools/typespec-azure-core";
import "@azure-tools/typespec-client-generator-core";

using Azure.ClientGenerator.Core;
using TypeSpec.Http;
using TypeSpec.Rest;
using TypeSpec.Versioning;
using Azure.Core;
using Azure.Core.Traits;

model A {
  ...KeyVaultCertificateBase
}

model KeyVaultCertificateBase {
 NBF?: float32
};

@@clientName(A.NBF, "nbf");
@@clientName(A.NBF, "nbf", "javascript");

The code fails only when alias is used.

@xirzec @lmazuel @joheredi FYI.....

Checklist

qiaozha commented 4 months ago

This is not just an issue for tcgc @clientName, any decorator that is trying to decorate on spread alias properties defined as above is problematic. See this @encodedName example

markcowl commented 4 months ago

@markcowl link to symbol refactor

markcowl commented 4 months ago

This may be fixed by https://github.com/microsoft/typespec/pull/2640