Skip to content

Commit

Permalink
Autorest: Propagate @clientName to x-ms-enum.name for enums and unions (
Browse files Browse the repository at this point in the history
#2137)

Fixes #1641.
  • Loading branch information
tjprescott authored Feb 4, 2025
1 parent f044c2e commit 1afdf42
Show file tree
Hide file tree
Showing 4 changed files with 965 additions and 998 deletions.
7 changes: 7 additions & 0 deletions .chronus/changes/clientNameXMsEnum-2025-0-27-14-20-31.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@azure-tools/typespec-autorest"
---

Ensure that `@clientName` value propagates to `x-ms-enum.name` for enums and unions.
7 changes: 5 additions & 2 deletions packages/typespec-autorest/src/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1770,11 +1770,13 @@ export async function getOpenAPIForService(
foundCustom = true;
}
}

const clientName = getClientName(context, union as any);
const schema: OpenAPI2Schema = {
type: e.kind,
enum: [...e.flattenedMembers.values()].map((x) => x.value),
"x-ms-enum": {
name: union.name,
name: clientName ?? union.name,
modelAsString: e.open,
},
};
Expand Down Expand Up @@ -2310,8 +2312,9 @@ export async function getOpenAPIForService(
modelAsString: false,
};
} else if (type.kind === "Enum") {
const clientName = getClientName(context, type);
schema["x-ms-enum"] = {
name: type.name,
name: clientName ?? type.name,
modelAsString: false,
};

Expand Down
24 changes: 24 additions & 0 deletions packages/typespec-autorest/test/openapi-output.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,24 @@ describe("typespec-autorest: definitions", () => {
required: ["name"],
});
});

it("overrides x-ms-enum.name with @clientName", async () => {
const res = await oapiForModel(
"FooResponse",
`
@clientName("RenamedFoo")
union Foo {
foo: "foo",
bar: "bar"
}
model FooResponse {
foo: Foo;
}`,
);
const schema = res.defs.RenamedFoo;
deepStrictEqual(schema["x-ms-enum"].name, "RenamedFoo");
});
});

it("recovers logical type name", async () => {
Expand Down Expand Up @@ -754,6 +772,12 @@ describe("typespec-autorest: enums", () => {
"x-ms-enum": { name: "PetType", modelAsString: true },
});
});

it("overrides x-ms-enum.name with @clientName", async () => {
const res = await oapiForModel("Foo", `@clientName("RenamedFoo") enum Foo {foo, bar}`);
const schema = res.defs.RenamedFoo;
deepStrictEqual(schema["x-ms-enum"].name, "RenamedFoo");
});
});

describe("typespec-autorest: extension decorator", () => {
Expand Down
Loading

0 comments on commit 1afdf42

Please sign in to comment.