Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: move server responseHeaders to server headers custom #1402

Conversation

ologbonowiwi
Copy link
Contributor

@ologbonowiwi ologbonowiwi commented Mar 12, 2024

  • feat: use headers.custom to hold response headers
  • test: update responseHeaders cases to use headers.custom instead
  • docs: update docs via ./lint.sh --mode=fix

Issue Reference(s):
Fixes #1400
/claim #1400

Build & Testing:

  • I ran cargo test successfully.
  • I have run ./lint.sh --mode=fix to fix all linting issues raised by ./lint.sh --mode=check.

Checklist:

  • I have added relevant unit & integration tests.
  • I have updated the documentation accordingly.
  • I have performed a self-review of my code.
  • PR follows the naming convention of <type>(<optional scope>): <title>

Summary by CodeRabbit

  • New Features
    • Introduced a more flexible way to define custom headers in server responses, enhancing control over Access-Control-Allow-Origin and other headers for downstream services.
  • Refactor
    • Refined handling of response headers across the system for better flexibility and traceability.
    • Implemented new methods for merging key-value pairs in server responses, improving the way custom headers are managed.
  • Tests
    • Updated and added tests to validate the new custom headers functionality and ensure backward compatibility with existing configurations.

Copy link
Contributor

coderabbitai bot commented Mar 12, 2024

Walkthrough

The recent updates involve transitioning the responseHeaders directive to a more flexible custom directive within the Headers input type. This shift enhances the ability to define custom headers for server responses, aligning with modern development practices and offering improved functionality for server-side configurations.

Changes

File Path Change Summary
generated/.../.tailcallrc.graphql Replaced responseHeaders directive with custom in Headers input type.
generated/.../.tailcallrc.schema.json Moved responseHeaders and introduced custom field for custom key-value pairs in server responses.
src/blueprint/server.rs Modified handle_response_headers to include additional tracing for "custom" and "headers".
src/config/headers.rs Added custom field to Headers struct for custom key-value pairs.
src/config/key_values.rs Added function for merging vectors of KeyValue structs.
src/config/mod.rs Exported headers module publicly.
src/config/server.rs Updated Server struct and methods for handling headers with custom field.
tests/execution/... Updated tests to reflect headers.custom configuration in JSON and GraphQL schema.

Assessment against linked issues

Objective Addressed Explanation
move server.responseHeaders to server.headers.custom (#1400)

Poem

In the land where code does weave,
A rabbit hopped, a trick up its sleeve.
From responseHeaders it took a leap,
To custom fields, a change not cheap.
🎩✨🐇
"Behold," it said, "a flexible feat,
For headers custom, isn't that neat?"

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share

Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit-tests for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit tests for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

@github-actions github-actions bot added the type: chore Routine tasks like conversions, reorganization, and maintenance work. label Mar 12, 2024
Copy link

codecov bot commented Mar 12, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 88.50%. Comparing base (de4213e) to head (2478fe9).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1402      +/-   ##
==========================================
+ Coverage   88.44%   88.50%   +0.06%     
==========================================
  Files         127      127              
  Lines       13470    13532      +62     
==========================================
+ Hits        11914    11977      +63     
+ Misses       1556     1555       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 3665038 and 012f104.
Files ignored due to path filters (4)
  • tests/snapshots/execution_spec__custom-headers.md_merged.snap is excluded by: !**/*.snap
  • tests/snapshots/execution_spec__test-response-header-value.md_errors.snap is excluded by: !**/*.snap
  • tests/snapshots/execution_spec__test-response-headers-multi.md_errors.snap is excluded by: !**/*.snap
  • tests/snapshots/execution_spec__test-response-headers-name.md_errors.snap is excluded by: !**/*.snap
Files selected for processing (10)
  • generated/.tailcallrc.graphql (2 hunks)
  • generated/.tailcallrc.schema.json (2 hunks)
  • src/blueprint/server.rs (1 hunks)
  • src/config/headers.rs (2 hunks)
  • src/config/mod.rs (1 hunks)
  • src/config/server.rs (4 hunks)
  • tests/execution/custom-headers.md (1 hunks)
  • tests/execution/test-response-header-value.md (1 hunks)
  • tests/execution/test-response-headers-multi.md (1 hunks)
  • tests/execution/test-response-headers-name.md (1 hunks)
Additional comments: 13
tests/execution/test-response-headers-name.md (1)
  • 6-6: The change from responseHeaders to headers: {custom: [{key: "🤣", value: "a"}]} aligns with the PR objectives of structuring response headers more clearly. This update in the test documentation ensures that the new method is correctly represented.
tests/execution/test-response-header-value.md (1)
  • 6-6: The update to use headers: {custom: [{key: "a", value: "a \n b"}]} correctly demonstrates handling header values with special characters, such as newline characters, in the new headers.custom structure. This ensures that the test cases cover scenarios with complex header values.
tests/execution/test-response-headers-multi.md (1)
  • 6-6: The transition to headers: {custom: [{key: "a b", value: "a \n b"}, {key: "a c", value: "a \n b"}]} for specifying multiple custom headers is correctly implemented in this test case. It showcases the flexibility of the new structure in handling multiple headers, including those with spaces in keys and special characters in values.
src/config/mod.rs (1)
  • 4-4: Exporting the headers module publicly in config/mod.rs is a necessary step to support the new headers.custom structure for response headers. This change ensures that the Headers struct and related functionality are accessible where needed.
tests/execution/custom-headers.md (1)
  • 6-17: The JSON configuration example correctly demonstrates the new way of specifying custom headers using headers.custom within the server object. This change aligns with the PR objectives and ensures that the documentation and test cases reflect the new structure for response headers.
src/config/headers.rs (1)
  • 15-19: Adding the custom field of type Vec<KeyValue> to the Headers struct is a key part of the refactor, enabling the structured specification of custom headers in server responses. This change is well-implemented and aligns with the PR objectives.
src/blueprint/server.rs (1)
  • 176-177: The addition of tracing for "custom" and "headers" in the handle_response_headers function enhances the visibility and debugging capabilities for handling custom headers. This update is a thoughtful improvement that aligns with the overall refactor's goals.
src/config/server.rs (4)
  • 163-171: The refactoring of the get_response_headers method to utilize the new headers.custom structure is correctly implemented. This change ensures that the server struct aligns with the new way of handling response headers, enhancing the structure and maintainability of the code.
  • 182-196: The introduction of the merge_key_value_iterators method is a significant improvement for merging key-value pairs, especially in the context of handling custom headers. This method provides a more robust and flexible way to merge headers, aligning with the refactor's objectives.
  • 200-210: The updates to the merge_right method, particularly the handling of the headers field, demonstrate a thoughtful approach to merging server configurations. The use of merge_key_value_iterators for merging custom headers ensures that the new structure is correctly maintained during merges.
  • 223-223: The update to handle the merging of vars using the merge_key_value_iterators method is a good practice, ensuring consistency in how key-value pairs are merged across different fields of the Server struct.
generated/.tailcallrc.graphql (1)
  • 574-579: The introduction of the custom field within the Headers input type is a positive change, enhancing the flexibility and structure in specifying custom headers for server responses. However, it would be beneficial to include detailed documentation for the custom field to ensure users understand its purpose and how to use it effectively.
generated/.tailcallrc.schema.json (1)
  • 1218-1224: The addition of the custom field under headers is well-defined, with a clear description and appropriate type specification (array of key-value pairs). This aligns with the PR objectives to transition to a more structured approach for specifying response headers. The schema reference ($ref) to KeyValue for items ensures that each header is represented as a key-value pair, which is a standard and flexible way to handle headers. This change should make the handling of custom headers more intuitive and flexible for developers.

Copy link
Contributor

@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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 012f104 and 0a9ebe8.
Files ignored due to path filters (1)
  • tests/snapshots/execution_spec__test-response-header-merge.md_merged.snap is excluded by: !**/*.snap
Files selected for processing (1)
  • tests/execution/test-response-header-merge.md (1 hunks)
Additional comments: 2
tests/execution/test-response-header-merge.md (2)
  • 3-16: The transition to using headers.custom for specifying response headers is correctly implemented in this schema definition. This aligns well with the PR objectives of refactoring the codebase for more structured header management.
  • 18-31: Similarly, this schema definition correctly utilizes the headers.custom directive for response headers. It's good to see consistency in applying the new method across different test cases.

@ologbonowiwi ologbonowiwi force-pushed the refactor/#1400/move-server-responseHeaders-to-server-headers-custom branch from 0a9ebe8 to a784132 Compare March 12, 2024 22:39
Copy link
Contributor

@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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 55ae414 and a784132.
Files ignored due to path filters (5)
  • tests/snapshots/execution_spec__custom-headers.md_merged.snap is excluded by: !**/*.snap
  • tests/snapshots/execution_spec__test-response-header-merge.md_merged.snap is excluded by: !**/*.snap
  • tests/snapshots/execution_spec__test-response-header-value.md_errors.snap is excluded by: !**/*.snap
  • tests/snapshots/execution_spec__test-response-headers-multi.md_errors.snap is excluded by: !**/*.snap
  • tests/snapshots/execution_spec__test-response-headers-name.md_errors.snap is excluded by: !**/*.snap
Files selected for processing (11)
  • generated/.tailcallrc.graphql (2 hunks)
  • generated/.tailcallrc.schema.json (2 hunks)
  • src/blueprint/server.rs (1 hunks)
  • src/config/headers.rs (2 hunks)
  • src/config/mod.rs (1 hunks)
  • src/config/server.rs (4 hunks)
  • tests/execution/custom-headers.md (1 hunks)
  • tests/execution/test-response-header-merge.md (1 hunks)
  • tests/execution/test-response-header-value.md (1 hunks)
  • tests/execution/test-response-headers-multi.md (1 hunks)
  • tests/execution/test-response-headers-name.md (1 hunks)
Check Runs (1)
Check Examples completed (6)
Files skipped from review as they are similar to previous changes (5)
  • generated/.tailcallrc.graphql
  • generated/.tailcallrc.schema.json
  • src/blueprint/server.rs
  • src/config/headers.rs
  • src/config/mod.rs
Additional comments: 8
tests/execution/test-response-headers-name.md (1)
  • 6-6: The update from responseHeaders to headers.custom aligns well with the PR objectives, ensuring that the test case reflects the new method of specifying response headers. This change is correctly implemented in the test case.
tests/execution/test-response-header-value.md (1)
  • 6-6: The update to use headers.custom with values containing newline characters is correctly implemented, reflecting the PR's objective to handle response headers more flexibly. This test case accurately captures the intended functionality.
tests/execution/test-response-headers-multi.md (1)
  • 6-6: The implementation of multiple custom headers using headers.custom is correctly demonstrated in this test case, aligning with the PR's objectives to enhance header management. This change is accurately reflected.
tests/execution/test-response-header-merge.md (1)
  • 4-4: The test cases for merging response headers using headers.custom are correctly implemented, showcasing the intended functionality as per the PR objectives. This demonstrates the flexibility and robustness of the new header management approach.
tests/execution/custom-headers.md (1)
  • 6-6: The renaming and new structure for specifying custom headers within the JSON configuration is correctly implemented in this test case. This aligns with the PR's objectives to enhance header management through a more structured approach.
src/config/server.rs (3)
  • 163-171: The refactoring of the get_response_headers method to handle the new headers.custom structure is correctly implemented, aligning with the PR's objectives for enhanced header management. This change improves the method's clarity and maintainability.
  • 182-196: The introduction of the merge_key_value_iterators method for merging key-value pairs is a good practice, enhancing the code's maintainability and modularity. This method is correctly implemented and aligns with the PR's objectives for robust header management.
  • 179-213: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [197-223]

The updates to the merge_right method, including the use of merge_key_value_iterators for merging headers and vars, are correctly implemented. These changes improve the method's robustness and maintainability, aligning with the PR's objectives for enhanced header management.

src/config/server.rs Outdated Show resolved Hide resolved
src/config/server.rs Outdated Show resolved Hide resolved
@ologbonowiwi ologbonowiwi requested a review from tusharmath March 13, 2024 13:31
Copy link
Contributor

@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.

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between a784132 and 108ddf2.
Files ignored due to path filters (1)
  • tests/snapshots/execution_spec__test-response-header-merge.md_merged.snap is excluded by: !**/*.snap
Files selected for processing (4)
  • src/blueprint/server.rs (2 hunks)
  • src/config/headers.rs (2 hunks)
  • src/config/key_values.rs (1 hunks)
  • src/config/server.rs (5 hunks)
Check Runs (3)
codecov/patch completed (1)
  • src/config/key_values.rs: 37-38: Added lines #L37 - L38 were not covered by tests
Check Examples completed (6)
Run Formatter and Lint Check completed (3)
  • src/config/key_values.rs: 27-27: Diff in /home/runner/work/tailcall/tailcall/src/config/key_values.rs
  • src/config/server.rs: 195-195: Diff in /home/runner/work/tailcall/tailcall/src/config/server.rs
Files skipped from review as they are similar to previous changes (2)
  • src/blueprint/server.rs
  • src/config/headers.rs
Additional comments: 2
src/config/server.rs (2)
  • 163-172: The refactoring of the get_response_headers method to utilize the new headers.custom field aligns well with the updated approach to managing response headers. The implementation is concise and correctly handles optional fields, ensuring backward compatibility and ease of use.
  • 195-213: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [185-210]

The updates to the merge_right method, including the use of merge_headers and merge_key_value_vecs, enhance the robustness and consistency of header and variable merging. However, there's a redundant operation in merging vars twice. Consider removing the fold operation before merge_key_value_vecs to streamline the process.

- self.vars = other
-     .vars
-     .iter()
-     .fold(self.vars.to_vec(), |mut acc, kv| {
-         let position = acc.iter().position(|x| x.key == kv.key);
-         if let Some(pos) = position {
-             acc[pos] = kv.clone();
-         } else {
-             acc.push(kv.clone());
-         };
-         acc
-     });
self.vars = merge_key_value_vecs(&self.vars, &other.vars);

Overall, the method's updates align well with the PR's objectives and improve the codebase's maintainability and readability.

src/config/key_values.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@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.

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 108ddf2 and bb1acd5.
Files selected for processing (2)
  • src/config/key_values.rs (1 hunks)
  • src/config/server.rs (6 hunks)
Check Runs (1)
Check Examples completed (6)
Files skipped from review as they are similar to previous changes (1)
  • src/config/key_values.rs
Additional comments: 3
src/config/server.rs (3)
  • 163-172: The refactoring of get_response_headers to utilize headers.custom for response headers is aligned with the PR objectives. This change simplifies the access to custom headers and makes the code more readable. However, ensure that all instances where get_response_headers is called are updated to handle the new return type if there was any change.
  • 185-185: The use of merge_headers in the merge_right method to handle merging of headers fields is a good practice. It encapsulates the logic for merging headers, making the merge_right method cleaner and more maintainable. Ensure that merge_headers correctly handles all edge cases, such as duplicate headers and case sensitivity.
  • 198-198: The initial approach to merging vars using an iterative method is replaced with merge_key_value_vecs. This change likely improves the readability and maintainability of the code by abstracting the merging logic into a separate function. However, it's important to ensure that merge_key_value_vecs correctly handles duplicates and prioritizes the values from other.vars over self.vars when conflicts arise.

src/config/server.rs Show resolved Hide resolved
@tusharmath tusharmath enabled auto-merge (squash) March 13, 2024 14:29
auto-merge was automatically disabled March 13, 2024 15:14

Head branch was pushed to by a user without write access

Copy link
Contributor

@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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between bb1acd5 and 2ece27b.
Files selected for processing (1)
  • src/config/key_values.rs (2 hunks)
Check Runs (2)
Check Examples completed (6)
Run Formatter and Lint Check completed (3)
  • src/config/key_values.rs: 162-162: Diff in /home/runner/work/tailcall/tailcall/src/config/key_values.rs
  • src/config/key_values.rs: 39-39: iterating on a map's values
Additional comments: 3
src/config/key_values.rs (3)
  • 29-40: The merge_key_value_vecs function is implemented correctly and fulfills its purpose of merging two vectors of KeyValue structs. However, consider the performance implications of cloning each KeyValue struct, especially for large vectors. If performance becomes a concern, you might explore more efficient ways to handle the merging, such as using references or optimizing the cloning process.
  • 114-167: The tests for merge_key_value_vecs are comprehensive and well-structured, covering all the essential scenarios including empty vectors, unique keys, and overlapping keys. Good job ensuring that the function behaves as expected across different cases.
  • 39-39: Iterating over the values of a map to collect them into a vector, as done in the final step of merge_key_value_vecs, is a standard and efficient pattern for this use case. The implementation is appropriate and does not require changes.

Copy link
Contributor

@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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 2ece27b and 2478fe9.
Files selected for processing (1)
  • src/config/key_values.rs (2 hunks)
Check Runs (1)
Check Examples completed (6)
Files skipped from review as they are similar to previous changes (1)
  • src/config/key_values.rs

@tusharmath tusharmath merged commit f4b9e21 into tailcallhq:main Mar 13, 2024
20 checks passed
@ologbonowiwi ologbonowiwi deleted the refactor/#1400/move-server-responseHeaders-to-server-headers-custom branch March 13, 2024 16:40
ssddOnTop pushed a commit that referenced this pull request Mar 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🙋 Bounty claim type: chore Routine tasks like conversions, reorganization, and maintenance work.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

move server.responseHeaders to server.headers.custom
2 participants