Skip to content

TCGC should not create types that are only used in external alternate type #3965

@antkmsft

Description

@antkmsft

Take the following spec (it is from https://github.com/Azure/typespec-azure/tree/3dde8002308e1532440f15991ce759c924f98ff3/packages/azure-http-specs/specs/azure/client-generator-core/alternate-type):

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

namespace _Specs_.Azure.ClientGenerator.Core.AlternateType;

@global.Azure.ClientGenerator.Core.operationGroup
namespace ExternalType {
  model Geometry {
    type: string;
    coordinates: numeric[];
  }

  model Feature {
    type: "Feature";
    geometry: Geometry | null;
    properties: Record<unknown>;
    id?: string | numeric;
  }
}

@@alternateType(_Specs_.Azure.ClientGenerator.Core.AlternateType.ExternalType.Feature,
  {
    identity: "geojson::Feature",
    package: "geojson",
    minVersion: "0.24.2",
  },
  "rust"
);

The problem that I think that needs to be fixed is that even though Geometry is only used inside the Feature, and nowhere else, the context.sdkPackage.models still has the Geometry model, it is not external and has usage flags - so there is probably no way for the emitter to quickly figure out that if we use an external type for Feature, we don't need to generate Geometry.

Similarly, even though the Feature is external, the context.sdkPackage.unions still has the FeatureId union (the one that is id?: string | numeric). But since the whole Feature is external, there is no need to build and generate that type.

We can work around the generation of Geometry by adding @@alternateType(_Specs_.Azure.ClientGenerator.Core.AlternateType.ExternalType.Geometry, but I don't think we can do the same for the Feature's id field (maybe there is something else, but still, the TSP will start looking not that pretty after all these - all that should've been sufficient is marking the Feature as external alternate type, and emitter won't even see the FeatureId or Geometry among types and unions as if they didn't exist).

Metadata

Metadata

Labels

lib:tcgcIssues for @azure-tools/typespec-client-generator-core library

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions