Skip to content

refactor(bedrock): parse request body once in the handler dispatch#315

Merged
vieiralucas merged 1 commit intomainfrom
worktree-cleanup+bedrock-dispatch
Apr 12, 2026
Merged

refactor(bedrock): parse request body once in the handler dispatch#315
vieiralucas merged 1 commit intomainfrom
worktree-cleanup+bedrock-dispatch

Conversation

@vieiralucas
Copy link
Copy Markdown
Member

@vieiralucas vieiralucas commented Apr 12, 2026

Summary

BedrockService::handle is a ~600-line match on action. Thirty-four of its arms started with exactly the same line:

let body: Value = serde_json::from_slice(&req.body).unwrap_or_default();

...before handing the body off to the operation-specific function.

This PR parses the body once at the top of the match and passes the shared body reference to each arm instead. Zero behavior change, the dispatcher shrinks by ~54 lines, and the mixed "some arms inline parsing, some arms call a method" pattern the audit flagged becomes uniform.

Test plan

  • cargo build --workspace
  • cargo fmt --check
  • cargo clippy --workspace --all-targets -- -D warnings
  • cargo test --workspace --exclude fakecloud-e2e --exclude fakecloud-conformance — 1102 passed, 0 failed
  • CI green

Summary by cubic

Parse the request body once in the Bedrock dispatcher and pass it to all handler arms. Removes duplicated parsing across 34 actions and trims ~54 lines, with no behavior change.

  • Refactors
    • Parse body once at the top via req.json_body() and pass &body to handlers.
    • Remove repeated serde_json::from_slice(&req.body).unwrap_or_default() in match arms.
    • Make dispatch logic consistent and easier to audit.

Written for commit ceaa90e. Summary will update on new commits.

``BedrockService::handle`` is a ~600-line match on ``action``. Thirty-four
of its arms started with exactly the same line:

    let body: Value = serde_json::from_slice(&req.body).unwrap_or_default();

…before handing the body off to the operation-specific function. Parse
it once at the top of the match and pass the shared ``body`` reference
to each arm instead. Zero behavior change, the dispatcher shrinks by
~54 lines, and the mixed "some arms inline, some arms call a method"
pattern the audit flagged becomes uniform.
Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

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

1 issue found across 1 file

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="crates/fakecloud-bedrock/src/service.rs">

<violation number="1" location="crates/fakecloud-bedrock/src/service.rs:754">
P2: JSON is parsed eagerly for every action, which adds unnecessary work and causes double parsing on runtime endpoints that already parse `req.body` downstream.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

action: format!("{} {}", req.method, req.raw_path),
})?;

let body = req.json_body();
Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai Bot Apr 12, 2026

Choose a reason for hiding this comment

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

P2: JSON is parsed eagerly for every action, which adds unnecessary work and causes double parsing on runtime endpoints that already parse req.body downstream.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At crates/fakecloud-bedrock/src/service.rs, line 754:

<comment>JSON is parsed eagerly for every action, which adds unnecessary work and causes double parsing on runtime endpoints that already parse `req.body` downstream.</comment>

<file context>
@@ -751,13 +751,14 @@ impl AwsService for BedrockService {
                 action: format!("{} {}", req.method, req.raw_path),
             })?;
 
+        let body = req.json_body();
+
         match action {
</file context>
Fix with Cubic

@vieiralucas vieiralucas merged commit b1c302f into main Apr 12, 2026
22 checks passed
@vieiralucas vieiralucas deleted the worktree-cleanup+bedrock-dispatch branch April 12, 2026 23:38
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