Skip to content

fix(router): preserve fang order#655

Merged
kanarus merged 4 commits into
ohkami-rs:mainfrom
iWhatty:fix/nested-router-merge
Apr 12, 2026
Merged

fix(router): preserve fang order#655
kanarus merged 4 commits into
ohkami-rs:mainfrom
iWhatty:fix/nested-router-merge

Conversation

@iWhatty
Copy link
Copy Markdown
Contributor

@iWhatty iWhatty commented Apr 11, 2026

Summary

This fixes fang ordering during final router compression for single-child static chains.

Without this change, request context written by a parent fang could become invisible to a nested top-level fang on a child Ohkami after compression.

Problem

I added two regression tests (/src/ohkami/mod.rs):

  • parent_context_auth_is_visible_to_nested_top_level_fang_in_realistic_order
  • parent_context_auth_is_visible_to_nested_local_route_fangs_in_realistic_order

The first test failed before this change:

  • a parent fang writes request context
  • a nested top-level fang on a child Ohkami tries to read it
  • the request was rejected as unauthorized

The second test passed:

  • same parent context-writing fang
  • same nested routes
  • same child authorization fang, but attached locally per-route instead of as a top-level fang

This showed that the issue was specific to nested top-level fang composition, not local per-route fangs.

Root cause

In router/final.rs, compression of a node with its single static child merged fang lists in the wrong order:

  • previous behavior used base.fangses.append(child.fangses)
  • this made child fangs effectively wrap parent fangs after compression

That inverted the intended outer/inner behavior for compressed nested top-level fangs.

Fix

During compression, merge fang lists in the correct order so that:

  • child node fangs remain inner
  • base node fangs remain outer

This preserves the expected execution order and makes compressed behavior consistent with the non-compressed case.

Validation

Ran:

cargo test -p ohkami parent_context_auth_is_visible_to_nested_ --lib --features rt_tokio -- --nocapture
cargo test -p ohkami --lib --features rt_tokio

Both regression tests pass, and the relevant ohkami library test suite passes as well.

@iWhatty iWhatty requested a review from kanarus as a code owner April 11, 2026 01:51
@kanarus kanarus changed the title fix(router): preserve fang order during final compression fix(router): preserve fang order Apr 12, 2026
Copy link
Copy Markdown
Member

@kanarus kanarus left a comment

Choose a reason for hiding this comment

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

@iWhatty

  • After further thought, the root cause wasn't actually the fangs order during compression, but rather a bug in router::base::Router::apply_fangs itself (which is called from Ohkami::into_router).
  • Thanks for catching this! Your feedback helped me realize that the logic for adding fangs in apply_fangs shouldn't just be a simple push; they actually need to be added to the outside.
  • Since this wasn't an issue with your code, I went ahead and committed the fix myself.

Comment thread ohkami/src/ohkami/mod.rs
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

👍

@kanarus kanarus merged commit 9cd3064 into ohkami-rs:main Apr 12, 2026
11 checks passed
@kanarus kanarus added fix Fix a bug T-router labels Apr 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants