Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .chronus/changes/fix-required-etag-header-2026-09-14.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
changeKind: fix
packages:
- "@typespec/http-client-python"
---

Generate required `If-Match` headers as direct `if_match` parameters instead of
the optional `etag` and `match_condition` convenience API.
6 changes: 5 additions & 1 deletion packages/http-client-python/emitter/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,11 @@ function isEtagType(type: SdkType): boolean {
);
}

function getEtagRole(parameter: SdkHeaderParameter): string | undefined {
export function getEtagRole(parameter: SdkHeaderParameter): string | undefined {
// The etag/match_condition convenience API can omit or redirect the wire
// header, so it is only valid when the TypeSpec header itself is optional.
if (!parameter.optional) return undefined;

const name = parameter.name.toLowerCase();
const wire = parameter.serializedName.toLowerCase();
// Standard If-Match / If-None-Match headers work with any type
Expand Down
19 changes: 19 additions & 0 deletions packages/http-client-python/emitter/test/http.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { SdkHeaderParameter } from "@azure-tools/typespec-client-generator-core";
import { strictEqual } from "assert";
import { describe, it } from "vitest";
import { getEtagRole } from "../src/http.js";

function createHeaderParameter(optional: boolean): SdkHeaderParameter {
return {
name: "ifMatch",
serializedName: "If-Match",
optional,
} as SdkHeaderParameter;
}

describe("typespec-python: HTTP parameters", () => {
it("only promotes optional If-Match headers to the conditional convenience API", () => {
strictEqual(getEtagRole(createHeaderParameter(true)), "ifMatch");
strictEqual(getEtagRole(createHeaderParameter(false)), undefined);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ async def test_post_if_match(client: ConditionalRequestClient):
await client.post_if_match(etag="valid", match_condition=MatchConditions.IfNotModified)


@pytest.mark.asyncio
async def test_post_required_if_match(client: ConditionalRequestClient):
await client.post_required_if_match(if_match='"required"')


@pytest.mark.asyncio
async def test_post_if_none_match(client: ConditionalRequestClient):
await client.post_if_none_match(etag="invalid", match_condition=MatchConditions.IfModified)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ def test_post_if_match(client: ConditionalRequestClient):
client.post_if_match(etag="valid", match_condition=MatchConditions.IfNotModified)


def test_post_required_if_match(client: ConditionalRequestClient):
client.post_required_if_match(if_match='"required"')


def test_post_if_none_match(client: ConditionalRequestClient):
client.post_if_none_match(etag="invalid", match_condition=MatchConditions.IfModified)

Expand Down
Loading