Repository navigation
Description
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:
- Cache enabled metadata with ID
target-id, path/target, andrpcType=dubbo. - 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. - Apply this rule through the real rewrite data handler:
{"regex":"^/old$","replace":"/target","rewriteMetaData":true}- 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.
- Repeat as a control, manually setting only
CONTEXT_PATHto 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
- RewritePlugin.java:84: missing-prefix fallback can yield null.
- RewritePlugin.java:86: string concatenation creates the invalid lookup key.
- RewritePlugin.java:94: failed metadata selection resets the RPC type to HTTP.
- DefaultShenyuContextBuilder.java:85: normal request metadata selection does not populate context-path attributes.
- AbstractDubboPlugin.java:145: Dubbo execution is gated by its RPC type in
skip(see the method in this file).
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.
- [BUG] Rewrite plugin can throw when replace uses placeholders but regex has no placeholder #6509 and [BUG] RewritePlugin placeholder branch drops first character of path-variable value + StringIndexOutOfBounds when regex lacks { #6883 concern placeholder substring/replacement handling. This reproduction uses a plain anchored regex and no placeholders; the path replacement succeeds.
- [BUG] Clearing rule handles leaves previous transformations active in gateway caches #7759 concerns stale rule handles after clearing configuration. Here the new handle is present and parsed, and its replacement is applied.
- [BUG] Partial metadata deletion events throw and leave stale gateway metadata caches #7790 concerns metadata deletion and stale caches. Here the target metadata is enabled, present, and directly retrievable; the caller supplies the wrong lookup key.
- Checked the refreshed historical issue-title baseline, live related issue searches/bodies, and open PR titles before filing.
Activity
konlue commentedon Oct 18, 2026
/assign
fix(rewrite): look up rewrite metadata with an empty prefix when cont…