Skip to content

feat: add to_dict() and get_headers() methods to Headers#1376

Open
sansyrox wants to merge 1 commit intomainfrom
feat/headers-to-dict-v2
Open

feat: add to_dict() and get_headers() methods to Headers#1376
sansyrox wants to merge 1 commit intomainfrom
feat/headers-to-dict-v2

Conversation

@sansyrox
Copy link
Copy Markdown
Member

@sansyrox sansyrox commented Apr 11, 2026

Summary

  • Adds Headers.to_dict() which returns a flat dict[str, str] with comma-joined values for duplicate headers, matching standard HTTP semantics.
  • Adds Python type stubs for both to_dict() and get_headers() in robyn.pyi.
  • Adds unit tests covering single values, duplicates, empty headers, and get_headers() list preservation.

Supersedes #1184

Test plan

  • test_headers_to_dict — basic single-value headers
  • test_headers_to_dict_with_duplicates — comma-joining of multi-value headers
  • test_headers_get_headers — list preservation via get_headers()
  • test_headers_to_dict_empty — empty headers edge case

Made with Cursor

Summary by CodeRabbit

  • New Features

    • Added get_headers() method to retrieve all headers grouped by name with values as lists.
    • Added to_dict() method to retrieve headers as a flattened dictionary, with multiple values concatenated by commas.
  • Tests

    • Added unit tests for both new header retrieval methods, including edge cases for duplicate and missing headers.

Headers.to_dict() returns a flat dict with comma-joined values for
duplicate headers. Headers.get_headers() returns a dict of lists
preserving all values per header name.

Supersedes #1184

Made-with: Cursor
@vercel
Copy link
Copy Markdown

vercel Bot commented Apr 11, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
robyn Ready Ready Preview, Comment Apr 11, 2026 11:45pm

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 11, 2026

📝 Walkthrough

Walkthrough

Two new accessor methods are added to the Headers class: get_headers() returns all header values grouped by key as lists, while to_dict() returns a flattened dictionary with comma-separated values for duplicate headers. Implementation includes type stubs, Rust code, and unit tests.

Changes

Cohort / File(s) Summary
Type Definitions
robyn/robyn.pyi
Added method signatures for get_headers() and to_dict() to the Headers class type stub.
Rust Implementation
src/types/headers.rs
Implemented to_dict() method that creates a Python dictionary mapping header keys to either single string values or comma-joined concatenated values for duplicate headers.
Test Coverage
unit_tests/test_request_object.py
Added comprehensive unit tests validating to_dict() with case-normalized keys, duplicate header concatenation, get_headers() returning value lists, and behavior on empty headers.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 Two methods hopped into Headers with glee,
One flattens to dict, one keeps lists free,
With commas and values all neatly combined,
The rabbit rejoices—what elegant design! ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 27.27% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main change: adding two new methods (to_dict() and get_headers()) to the Headers class.
Description check ✅ Passed The PR description provides a clear summary of changes, includes a comprehensive test plan with all tests marked as completed, and addresses the template requirements.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/headers-to-dict-v2

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/types/headers.rs`:
- Around line 101-105: to_dict() currently unconditionally joins multiple header
values with commas (joined_value), which corrupts non-combinable headers like
"set-cookie"; change the construction so that when the header name is in a small
constant set (e.g., NON_COMBINABLE_HEADERS containing "set-cookie",
"set-cookie2", etc.) you do NOT comma-merge but instead preserve a single value
(e.g., values[0].clone()) or otherwise avoid joining, and leave a note that
callers should use get_headers() for full-fidelity multi-value headers; add the
NON_COMBINABLE_HEADERS constant and use it inside to_dict() where joined_value
is computed.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 4b2e2577-dcac-4021-a3e5-f825944dbc5e

📥 Commits

Reviewing files that changed from the base of the PR and between 3e04c65 and d9ac46a.

📒 Files selected for processing (3)
  • robyn/robyn.pyi
  • src/types/headers.rs
  • unit_tests/test_request_object.py

Comment thread src/types/headers.rs
Comment on lines +101 to +105
let joined_value = if values.len() == 1 {
values[0].clone()
} else {
values.join(",")
};
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot Apr 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

to_dict() should not comma-merge non-combinable headers (e.g., set-cookie).

At Line 104, duplicate values are always joined with commas. That is not valid for all HTTP headers and can corrupt semantics for headers like set-cookie. Prefer special-casing non-combinable names (or documenting to_dict() as lossy and directing callers to get_headers() for full fidelity).

Suggested adjustment
 pub fn to_dict(&self, py: Python) -> Py<PyDict> {
     let dict = PyDict::new(py);
     for iter in self.headers.iter() {
         let (key, values) = iter.pair();
-        let joined_value = if values.len() == 1 {
+        let joined_value = if values.len() == 1 {
             values[0].clone()
+        } else if key.eq_ignore_ascii_case("set-cookie") {
+            // Non-combinable header; keep a deterministic single value in flattened view.
+            // Callers needing all values should use `get_headers()`.
+            values.last().cloned().unwrap_or_default()
         } else {
             values.join(",")
         };
         dict.set_item(key, joined_value).unwrap();
     }
     dict.into()
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
let joined_value = if values.len() == 1 {
values[0].clone()
} else {
values.join(",")
};
pub fn to_dict(&self, py: Python) -> Py<PyDict> {
let dict = PyDict::new(py);
for iter in self.headers.iter() {
let (key, values) = iter.pair();
let joined_value = if values.len() == 1 {
values[0].clone()
} else if key.eq_ignore_ascii_case("set-cookie") {
// Non-combinable header; keep a deterministic single value in flattened view.
// Callers needing all values should use `get_headers()`.
values.last().cloned().unwrap_or_default()
} else {
values.join(",")
};
dict.set_item(key, joined_value).unwrap();
}
dict.into()
}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/types/headers.rs` around lines 101 - 105, to_dict() currently
unconditionally joins multiple header values with commas (joined_value), which
corrupts non-combinable headers like "set-cookie"; change the construction so
that when the header name is in a small constant set (e.g.,
NON_COMBINABLE_HEADERS containing "set-cookie", "set-cookie2", etc.) you do NOT
comma-merge but instead preserve a single value (e.g., values[0].clone()) or
otherwise avoid joining, and leave a note that callers should use get_headers()
for full-fidelity multi-value headers; add the NON_COMBINABLE_HEADERS constant
and use it inside to_dict() where joined_value is computed.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolve — by design. to_dict() is a convenience flattened view. get_headers() preserves full multi-value fidelity for headers like set-cookie.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rate Limit Exceeded

@sansyrox have exceeded the limit for the number of chat messages per hour. Please wait 2 minutes and 1 seconds before sending another message.

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Apr 12, 2026

Merging this PR will not alter performance

✅ 189 untouched benchmarks


Comparing feat/headers-to-dict-v2 (d9ac46a) with main (3e04c65)

Open in CodSpeed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant