diff --git a/.chronus/changes/python-fixCasingEnum-2026-3-9-16-6-39.md b/.chronus/changes/python-fixCasingEnum-2026-3-9-16-6-39.md new file mode 100644 index 00000000000..c0979d3298d --- /dev/null +++ b/.chronus/changes/python-fixCasingEnum-2026-3-9-16-6-39.md @@ -0,0 +1,7 @@ +--- +changeKind: fix +packages: + - "@typespec/http-client-python" +--- + +Preserve underscores before numeric enum segments and acronyms such as `A2A` without changing numeric identifiers such as `int32`. diff --git a/packages/http-client-python/emitter/src/utils.ts b/packages/http-client-python/emitter/src/utils.ts index 3258d6982ce..526f8eafb84 100644 --- a/packages/http-client-python/emitter/src/utils.ts +++ b/packages/http-client-python/emitter/src/utils.ts @@ -44,15 +44,21 @@ function deconstruct( return [...identifier.flatMap((each) => deconstruct(each, maxUppercasePreserve))]; } - return `${identifier}` - .replace(/([a-z]+)([A-Z])/g, "$1 $2") // Add a space in between camelCase words(e.g. fooBar => foo Bar) - .replace(/(\d+)/g, " $1 ") // Adds a space after numbers(e.g. foo123 => foo123 bar) - .replace(/\b([A-Z]+)([A-Z])s([^a-z])(.*)/g, "$1$2« $3$4") // Add a space after a plural upper cased word(e.g. MBsFoo => MBs Foo) - .replace(/\b([A-Z]+)([A-Z])([a-z]+)/g, "$1 $2$3") // Add a space between an upper case word(2 char+) and the last capital case.(e.g. SQLConnection -> SQL Connection) - .replace(/«/g, "s") - .trim() - .split(/[\W|_]+/) - .map((each) => (IsFullyUpperCase(each, maxUppercasePreserve) ? each : each.toLowerCase())); + return ( + `${identifier}` + // Keep digits attached to the preceding word and preserve short acronyms like A2A. + .replace( + /(? (digits ? `${digits} ` : match), + ) + .replace(/([a-z]+)([A-Z])/g, "$1 $2") // Add a space in between camelCase words(e.g. fooBar => foo Bar) + .replace(/\b([A-Z]+)([A-Z])s(\d*)([^a-z])(.*)/g, "$1$2«$3 $4$5") // Add a space after a plural upper cased word(e.g. MBsFoo => MBs Foo) + .replace(/\b([A-Z]+)([A-Z])([a-z]+)/g, "$1 $2$3") // Add a space between an upper case word(2 char+) and the last capital case.(e.g. SQLConnection -> SQL Connection) + .replace(/«/g, "s") + .trim() + .split(/[\W|_]+/) + .map((each) => (IsFullyUpperCase(each, maxUppercasePreserve) ? each : each.toLowerCase())) + ); } function isEqual(s1: string, s2: string): boolean { @@ -103,9 +109,7 @@ function filterEmptyStrings(arr: string[]): string[] { export function camelToSnakeCase(name: string): string { if (!name) return name; const words = filterEmptyStrings(normalize(name, false, 6)); - const result = words.join("_").toLowerCase(); - const result_final = result.replace(/([^\d])_(\d+)/g, "$1$2"); - return result_final; + return words.join("_").toLowerCase(); } export function getClientName(named: { name: string; isExactName: boolean }): string { diff --git a/packages/http-client-python/emitter/test/emitter.test.ts b/packages/http-client-python/emitter/test/emitter.test.ts index 857dc01bc02..772ed45f2a7 100644 --- a/packages/http-client-python/emitter/test/emitter.test.ts +++ b/packages/http-client-python/emitter/test/emitter.test.ts @@ -2,6 +2,55 @@ import { expectDiagnostics, t } from "@typespec/compiler/testing"; import { expect, it } from "vitest"; import { EmitterTester, emitCodeModel } from "./test-host.js"; +it("preserves numeric property names while fixing numeric enum members", async () => { + const { codeModel, diagnostics } = await emitCodeModel(` + import "@typespec/http"; + using TypeSpec.Http; + + @service namespace Test; + enum NumericNames { + WEB_SEARCH_PREVIEW_2025_03_11: "web", + DEFAULT_2024_11_15: "default", + A2A: "a2a", + int32: "int32", + enumMemberV2Preview: "preview", + } + model NumericProperties { + int32Property: int32; + float32Property: float32; + subscriptionResource1Name: string; + names: NumericNames; + } + @get op read(): NumericProperties; + `); + + expectDiagnostics(diagnostics, []); + expect(codeModel.types).toContainEqual( + expect.objectContaining({ + type: "enum", + name: "NumericNames", + values: expect.arrayContaining([ + expect.objectContaining({ name: "WEB_SEARCH_PREVIEW_2025_03_11" }), + expect.objectContaining({ name: "DEFAULT_2024_11_15" }), + expect.objectContaining({ name: "A2A" }), + expect.objectContaining({ name: "INT32" }), + expect.objectContaining({ name: "ENUM_MEMBER_V2_PREVIEW" }), + ]), + }), + ); + expect(codeModel.types).toContainEqual( + expect.objectContaining({ + type: "model", + name: "NumericProperties", + properties: expect.arrayContaining([ + expect.objectContaining({ clientName: "int32_property" }), + expect.objectContaining({ clientName: "float32_property" }), + expect.objectContaining({ clientName: "subscription_resource1_name" }), + ]), + }), + ); +}); + it("preserves an XML response body with the legacy bytes encoding", async () => { const { codeModel, diagnostics } = await emitCodeModel(` import "@typespec/http"; diff --git a/packages/http-client-python/emitter/test/utils.test.ts b/packages/http-client-python/emitter/test/utils.test.ts index 332bf597e76..e3eb3f3e1c8 100644 --- a/packages/http-client-python/emitter/test/utils.test.ts +++ b/packages/http-client-python/emitter/test/utils.test.ts @@ -9,20 +9,58 @@ describe("typespec-python: utils", () => { StandardSSDLRS: "standard_ssdlrs", QRCode: "qr_code", MicroQRCode: "micro_qr_code", - detection_01: "detection01", - "v1.1-preview.1": "v1_1_preview1", + detection_01: "detection_01", + "v1.1-preview.1": "v1_1_preview_1", pointInTimeUTC: "point_in_time_utc", diskSizeGB: "disk_size_gb", lastModifiedTS: "last_modified_ts", "FOO-BAR": "foo_bar", "FOO-BAR-BAZ": "foo_bar_baz", "A-B": "a_b", + // Fix for GitHub issue #10312: preserve underscores before numbers (dates) + WebSearchPreview_2025_03_11: "web_search_preview_2025_03_11", + Default_2024_11_15: "default_2024_11_15", + // Fix for GitHub issue #10312: don't insert underscores around single digits in acronyms + A2a: "a2a", }; for (const [input, expected] of Object.entries(cases)) { strictEqual(camelToSnakeCase(input), expected); } }); + it.each([ + ["int32", "int32"], + ["uint8", "uint8"], + ["uint32", "uint32"], + ["float32", "float32"], + ["float64", "float64"], + ["int32Property", "int32_property"], + ["float32Property", "float32_property"], + ["int32MillisecondsArray", "int32_milliseconds_array"], + ["uint32AsStringOptional", "uint32_as_string_optional"], + ["base64", "base64"], + ["base64url", "base64_url"], + ["base64urlArray", "base64_url_array"], + ["iso8601", "iso8601"], + ["rfc3339", "rfc3339"], + ["rfc7231", "rfc7231"], + ["two6k", "two6_k"], + ["subscriptionResource1Name", "subscription_resource1_name"], + ["subscriptionResource2Name", "subscription_resource2_name"], + ["InterfaceV2", "interface_v2"], + ["v2InInterface", "v2_in_interface"], + ["enumMemberV2Preview", "enum_member_v2_preview"], + ["errorResponseStatusCode404", "error_response_status_code404"], + ["A2A", "a2a"], + ["B2B", "b2b"], + ["Protocol_A2A", "protocol_a2a"], + ["A2A_Protocol", "a2a_protocol"], + ["WEB_SEARCH_PREVIEW_2025_03_11", "web_search_preview_2025_03_11"], + ["DEFAULT_2024_11_15", "default_2024_11_15"], + ])("preserves numeric identifier boundaries in %s", (input, expected) => { + strictEqual(camelToSnakeCase(input), expected); + }); + it("md2rst", async () => { const des = "Format: .."; strictEqual(md2Rst(des), "Format: ..");