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).
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):
The problem that I think that needs to be fixed is that even though
Geometryis only used inside theFeature, and nowhere else, thecontext.sdkPackage.modelsstill has theGeometrymodel, it is notexternaland has usage flags - so there is probably no way for the emitter to quickly figure out that if we use an external type forFeature, we don't need to generateGeometry.Similarly, even though the
Featureisexternal, thecontext.sdkPackage.unionsstill has theFeatureIdunion (the one that isid?: string | numeric). But since the wholeFeatureisexternal, there is no need to build and generate that type.We can work around the generation of
Geometryby adding@@alternateType(_Specs_.Azure.ClientGenerator.Core.AlternateType.ExternalType.Geometry, but I don't think we can do the same for theFeature'sidfield (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 theFeatureas external alternate type, and emitter won't even see theFeatureIdorGeometryamong types and unions as if they didn't exist).