Repository navigation
fix(abstractions): keep single value headers to one value when added as a string - #819
Merged
Vincent Biret (baywet) merged 2 commits intoOct 20, 2026
Conversation
UNKNOWN H4CK3R (unknownhackerworld)
requested a review
from a team
as a code owner
October 20, 2026 11:32
Contributor
Author
|
@microsoft-github-policy-service agree |
…as a string HeadersCollection.add() only applied SINGLE_VALUE_HEADERS in the list branch. Adding a string value for content-type, content-encoding or content-length appended it to the existing set, so the header ended up with several values. add_all() goes through the same path. Wrap a string value in a one-item list so both inputs share the list path. Fixes microsoft#818
UNKNOWN H4CK3R (unknownhackerworld)
force-pushed
the
fix/single-value-header-string
branch
from
October 20, 2026 11:41
cf02220 to
2b04343
Compare
Vincent Biret (baywet)
approved these changes
Oct 20, 2026
Vincent Biret (baywet)
left a comment
Member
There was a problem hiding this comment.
Thanks for the contribution!
Vincent Biret (baywet)
enabled auto-merge
October 20, 2026 13:08
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Overview
HeadersCollection.add()now replaces the value of a single value header (content-type,content-encoding,content-length) when the value is passed as a string, the same way it already does when the value is passed as a list.Before this change:
After:
{'text/plain'}.Related Issue
Fixes #818
Notes
Root cause: in
packages/abstractions/kiota_abstractions/headers_collection.py,add()has two branches. The list branch checksself.SINGLE_VALUE_HEADERSand replaces the set. The string branch skips that check and goes straight tovalues.add(header_values)on the existing set.add_all()callsadd(key, value)with one string at a time, so merging collections hits the same branch.Fix: wrap a string value in a one-item list at the top of
add(), so both kinds of input go through one path that appliesSINGLE_VALUE_HEADERS. The last value wins for single value headers. This matches kiota-dotnet'sRequestHeaders.Add, which replaces single value headers no matter how many values are passed. It also removes the duplicated string branch, soadd()is shorter than before.Tests:
test_add_single_value_header_to_existing_headerused a subset check ({"value2"} <= headers.get("content-type")), so it passed while both values were kept. I changed it to an equality check, and addedtest_add_all_replaces_single_value_headerfor theadd_all()path. Both fail onmainand pass with this change.Tradeoffs and alternatives:
try_add()is left as is. It already refuses to overwrite an existing header, which is its documented behavior.SINGLE_VALUE_HEADERScheck to the string branch as a one-line fix. That worked but pushedadd()over SonarCloud's cognitive complexity limit (16 vs 15) and kept two copies of the same logic, so I normalized the input instead.add()keeping twoContent-Typevalues would now see only the last one. The class name and existing test say single value headers should hold one value, so this looks like the intended behavior.AI disclosure: this change was found and written with help from an AI coding assistant.
Testing Instructions
cd packages/abstractions && pip install -e . pytest pytest-asynciopytest tests/test_request_header.py: 13 passed (2 fail onmain)pytest tests: 158 passedyapf --diffandmypyonkiota_abstractions/headers_collection.pyare clean