Skip to content

[BUG] Rewrite metadata lookup uses a null path prefix when context-path is absent #7796

Closed

Description

@Aias00

Current Behavior

A rewrite rule with rewriteMetaData=true cannot select the rewritten path's metadata when the context-path plugin has not populated exchange attributes.

RewritePlugin reads REWRITE_CONTEXT_PATH, falls back to CONTEXT_PATH, and concatenates the result with rewriteUri without a default for the absent prefix:

final String contextPath = Optional.ofNullable((String) exchange.getAttribute(Constants.REWRITE_CONTEXT_PATH))
        .orElseGet(() -> exchange.getAttribute(Constants.CONTEXT_PATH));
MetaData metaData = MetaDataCache.getInstance().obtain(contextPath + rewriteUri);

A normal rewrite-only request has neither attribute. For a rewritten path /target, the lookup key becomes null/target, rather than /target. The URI rewrite itself succeeds, but metadata selection misses and the context's RPC type is set to HTTP even if enabled Dubbo metadata exists at /target.

Expected Behavior

The rewrite plugin's rewriteMetaData option should resolve metadata for the rewritten path when no context-path plugin is configured. An absent context-path prefix should contribute no text to the lookup key.

Reproduction And Validation

Run the actual current RewritePlugin.doExecute, RewritePluginDataHandler, DefaultShenyuContextBuilder, DivideShenyuContextDecorator, MetaDataCache and path matching helper:

  1. Cache enabled metadata with ID target-id, path /target, and rpcType=dubbo.
  2. Create a mock HTTP exchange for /old. Build its context with the actual default context builder and HTTP decorator. Leave both context-path attributes absent, as they are without that plugin.
  3. Apply this rule through the real rewrite data handler:
{"regex":"^/old$","replace":"/target","rewriteMetaData":true}
  1. Invoke the real rewrite execution method through a small subclass, with a terminal no-op chain. Inspect the rewritten path, selected metadata and context RPC type.
  2. Repeat as a control, manually setting only CONTEXT_PATH to the empty string before execution. This is a diagnostic control, not a claimed normal production configuration.

Observed:

TARGET_CACHE_PRESENT=true
NO_CONTEXT_PATH rewritten=/target metadataId=null rpcType=http
EMPTY_PREFIX_CONTROL rewritten=/target metadataId=target-id rpcType=dubbo

The target record is present in both runs. No active context-path or external provider is required to trigger the faulty lookup. The probe compiles the current production classes above; it does not replace the lookup, context builder, rule parsing, or cache with mocks. Only the exchange and terminal chain are in-process fixtures.

Location And Impact

This prevents the metadata/protocol switch requested by the rewrite rule. Existing old metadata, when present, is also not replaced on a lookup miss. The executed probe demonstrates the no-original-metadata case and the incorrect HTTP result; it does not run a complete Dubbo invocation.

Priority: P2 / Medium. Confidence: high. Coverage: confirmed for the actual rewrite execution and cache/context behavior; no full gateway/backend deployment was run.

Suggested Fix

Default an absent prefix to an empty string and keep the metadata lookup key consistent with the path semantics of rewrite/context-path. Review already-prefixed paths when composing the key so the fix does not introduce or preserve duplicate prefixes. Do not require users to enable context-path merely to use rewriteMetaData.

Environment And Related Issues

Apache ShenYu 2.7.2-SNAPSHOT, master at b2bc43376a0146684faf6d45a6832d201fc61f45.

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions