Aias00 opened a new issue, #7796:
URL: https://github.com/apache/shenyu/issues/7796

   ## 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:
   
   ```java
   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:
   
   ```json
   {"regex":"^/old$","replace":"/target","rewriteMetaData":true}
   ```
   
   4. 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.
   5. 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:
   
   ```text
   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](https://github.com/apache/shenyu/blob/b2bc43376a0146684faf6d45a6832d201fc61f45/shenyu-plugin/shenyu-plugin-rewrite/src/main/java/org/apache/shenyu/plugin/rewrite/RewritePlugin.java#L84):
 missing-prefix fallback can yield null.
   - 
[RewritePlugin.java:86](https://github.com/apache/shenyu/blob/b2bc43376a0146684faf6d45a6832d201fc61f45/shenyu-plugin/shenyu-plugin-rewrite/src/main/java/org/apache/shenyu/plugin/rewrite/RewritePlugin.java#L86):
 string concatenation creates the invalid lookup key.
   - 
[RewritePlugin.java:94](https://github.com/apache/shenyu/blob/b2bc43376a0146684faf6d45a6832d201fc61f45/shenyu-plugin/shenyu-plugin-rewrite/src/main/java/org/apache/shenyu/plugin/rewrite/RewritePlugin.java#L94):
 failed metadata selection resets the RPC type to HTTP.
   - 
[DefaultShenyuContextBuilder.java:78](https://github.com/apache/shenyu/blob/b2bc43376a0146684faf6d45a6832d201fc61f45/shenyu-plugin/shenyu-plugin-global/src/main/java/org/apache/shenyu/plugin/global/DefaultShenyuContextBuilder.java#L78):
 normal request metadata selection does not populate context-path attributes.
   - 
[AbstractDubboPlugin.java:145](https://github.com/apache/shenyu/blob/b2bc43376a0146684faf6d45a6832d201fc61f45/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-dubbo/shenyu-plugin-dubbo-common/src/main/java/org/apache/shenyu/plugin/dubbo/common/AbstractDubboPlugin.java#L145):
 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`.
   
   - #6509 and #6883 concern placeholder substring/replacement handling. This 
reproduction uses a plain anchored regex and no placeholders; the path 
replacement succeeds.
   - #7759 concerns stale rule handles after clearing configuration. Here the 
new handle is present and parsed, and its replacement is applied.
   - #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.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to