Skip to content

Commit 0086a05

Browse files
iscai-msftiscai-msft
and
iscai-msft
authored
[tcgc] allow models-only packages (#2179)
fixes #1755 --------- Co-authored-by: iscai-msft <[email protected]>
1 parent 9cb47ea commit 0086a05

File tree

4 files changed

+87
-2
lines changed

4 files changed

+87
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
changeKind: feature
3+
packages:
4+
- "@azure-tools/typespec-client-generator-core"
5+
---
6+
7+
add support for models-only packages

cspell.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ words:
3939
- Donezo
4040
- dynatrace
4141
- espt
42+
- fhir
4243
- foos
4344
- français
4445
- horiz

packages/typespec-client-generator-core/src/types.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
getEncode,
2626
getKnownValues,
2727
getLifecycleVisibilityEnum,
28+
getLocationContext,
2829
getSummary,
2930
getVisibilityForClass,
3031
ignoreDiagnostics,
@@ -1867,8 +1868,11 @@ export function handleAllTypes(context: TCGCContext): [void, readonly Diagnostic
18671868
}
18681869
}
18691870
// update for orphan models/enums/unions
1870-
for (const client of listClients(context)) {
1871-
const namespaces = [client.service];
1871+
const allNamespaces = [...context.program.getGlobalNamespaceType().namespaces.values()].filter(
1872+
(x) => getLocationContext(context.program, x).type === "project",
1873+
);
1874+
for (const currNamespace of allNamespaces) {
1875+
const namespaces = [currNamespace];
18721876
while (namespaces.length) {
18731877
const namespace = namespaces.pop()!;
18741878
// orphan models

packages/typespec-client-generator-core/test/package.test.ts

+73
Original file line numberDiff line numberDiff line change
@@ -1260,6 +1260,79 @@ describe("typespec-client-generator-core: package", () => {
12601260
strictEqual(sdkPackage.enums.length, 2);
12611261
});
12621262
});
1263+
it("models only package", async () => {
1264+
await runner.compile(`
1265+
@usage(Usage.input | Usage.output)
1266+
namespace EventGridClient {
1267+
model CloudEvent {
1268+
id: string;
1269+
}
1270+
}
1271+
`);
1272+
const sdkPackage = runner.context.sdkPackage;
1273+
strictEqual(sdkPackage.models.length, 1);
1274+
strictEqual(sdkPackage.models[0].name, "CloudEvent");
1275+
strictEqual(sdkPackage.clients.length, 0);
1276+
});
1277+
1278+
it("models only package with azure and versioning decorators", async () => {
1279+
const runnerWithCore = await createSdkTestRunner({
1280+
librariesToAdd: [AzureCoreTestLibrary],
1281+
autoUsings: ["Azure.Core"],
1282+
emitterName: "@azure-tools/typespec-java",
1283+
});
1284+
await runnerWithCore.compile(`
1285+
@usage(Usage.input | Usage.output)
1286+
@versioned(ServiceApiVersions)
1287+
namespace EventGridClient {
1288+
enum ServiceApiVersions {
1289+
@useDependency(Versions.v1_0_Preview_2)
1290+
v2018_01_01: "2018-01-01",
1291+
@useDependency(Versions.v1_0_Preview_2)
1292+
v2024_01_01: "2024-01-01",
1293+
}
1294+
/**
1295+
* CloudEvent comments
1296+
*/
1297+
model CloudEvent {
1298+
/**
1299+
* id comments
1300+
*/
1301+
id: string;
1302+
}
1303+
1304+
/**
1305+
* Healthcare comments
1306+
*/
1307+
model HealthcareFhirResourceCreatedEventData {
1308+
/**
1309+
* resourceVersionId comments
1310+
*/
1311+
@added(ServiceApiVersions.v2024_01_01)
1312+
resourceVersionId: int64;
1313+
}
1314+
}
1315+
`);
1316+
const sdkPackage = runnerWithCore.context.sdkPackage;
1317+
strictEqual(sdkPackage.models.length, 2);
1318+
const cloudEventModel = sdkPackage.models.find((x) => x.name === "CloudEvent");
1319+
ok(cloudEventModel);
1320+
strictEqual(cloudEventModel.properties.length, 1);
1321+
const idProperty = cloudEventModel.properties[0];
1322+
strictEqual(idProperty.name, "id");
1323+
strictEqual(idProperty.kind, "property");
1324+
deepStrictEqual(idProperty.apiVersions, ["2018-01-01", "2024-01-01"]);
1325+
1326+
const healthcareEventDataModel = sdkPackage.models.find(
1327+
(x) => x.name === "HealthcareFhirResourceCreatedEventData",
1328+
);
1329+
ok(healthcareEventDataModel);
1330+
strictEqual(healthcareEventDataModel.properties.length, 1);
1331+
const resourceVersionIdProperty = healthcareEventDataModel.properties[0];
1332+
strictEqual(resourceVersionIdProperty.name, "resourceVersionId");
1333+
strictEqual(resourceVersionIdProperty.kind, "property");
1334+
deepStrictEqual(resourceVersionIdProperty.apiVersions, ["2024-01-01"]);
1335+
});
12631336
});
12641337

12651338
function getServiceMethodOfClient(

0 commit comments

Comments
 (0)