Skip to content

Commit

Permalink
handle orphan types in nested namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
tadelesh committed Sep 18, 2024
1 parent d8c73dd commit a38c9a3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 13 deletions.
31 changes: 18 additions & 13 deletions packages/typespec-client-generator-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1303,8 +1303,8 @@ function updateMultiPartInfo(
: undefined,
contentType: httpOperationPart.body.contentTypeProperty
? diagnostics.pipe(
getSdkModelPropertyType(context, httpOperationPart.body.contentTypeProperty, operation)
)
getSdkModelPropertyType(context, httpOperationPart.body.contentTypeProperty, operation)
)
: undefined,
defaultContentTypes: httpOperationPart.body.contentTypes,
};
Expand Down Expand Up @@ -1843,17 +1843,22 @@ export function getAllModelsWithDiagnostics(
}
// update for orphan models/enums/unions
for (const client of listClients(context)) {
// orphan models
for (const model of client.service.models.values()) {
diagnostics.pipe(handleServiceOrphanType(context, model));
}
// orphan enums
for (const enumType of client.service.enums.values()) {
diagnostics.pipe(handleServiceOrphanType(context, enumType));
}
// orphan unions
for (const unionType of client.service.unions.values()) {
diagnostics.pipe(handleServiceOrphanType(context, unionType));
const namespaces = [client.service];
while (namespaces.length) {
const namespace = namespaces.pop()!;
// orphan models
for (const model of namespace.models.values()) {
diagnostics.pipe(handleServiceOrphanType(context, model));
}
// orphan enums
for (const enumType of namespace.enums.values()) {
diagnostics.pipe(handleServiceOrphanType(context, enumType));
}
// orphan unions
for (const unionType of namespace.unions.values()) {
diagnostics.pipe(handleServiceOrphanType(context, unionType));
}
namespaces.push(...namespace.namespaces.values());
}
}
// update access
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,4 +435,29 @@ describe("typespec-client-generator-core: @usage", () => {
code: "@azure-tools/typespec-client-generator-core/conflict-usage-override",
});
});

it("orphan model in group", async () => {
await runner.compileWithBuiltInService(
`
@access(Access.public)
@usage(Usage.output)
namespace Models {
model Model1 {
ref: Model2;
}
model Model2 {
name: string;
}
}
`
);
const models = runner.context.sdkPackage.models;
strictEqual(models.length, 2);
strictEqual(models[0].usage, UsageFlags.Output);
strictEqual(models[0].access, "public");
strictEqual(models[0].usage, UsageFlags.Output);
strictEqual(models[0].access, "public");
});

});

0 comments on commit a38c9a3

Please sign in to comment.