Skip to content

[BUG] ModifyResponse replaceHeaderKeys changes header value instead of header name #6510

Description

@Aias00

Search before asking

  • I had searched in the issues and found no similar issues.

Apache ShenYu Component

shenyu-plugin

What happened

ModifyResponseRuleHandle.replaceHeaderKeys is documented as a header-name replacement map:

/**
 * replace header map
 * key: oldHeaderKey, value: newHeaderKey.
 */
private Map<String, String> replaceHeaderKeys;

But ModifyResponsePlugin applies the map like this:

if (MapUtils.isNotEmpty(this.ruleHandle.getReplaceHeaderKeys())) {
    Map<String, String> replaceHeaderMap = this.ruleHandle.getReplaceHeaderKeys();
    replaceHeaderMap.forEach((key, value) -> httpHeaders.replace(key, Collections.singletonList(value)));
}

HttpHeaders.replace(key, values) keeps the same header key and replaces its values. Therefore a rule such as:

{
  "replaceHeaderKeys": {
    "X-Old": "X-New"
  }
}

will return:

X-Old: X-New

instead of renaming the header to:

X-New: <original X-Old values>

Expected behavior

replaceHeaderKeys should rename response header keys according to the documented oldHeaderKey -> newHeaderKey mapping while preserving the original header values, or the field should be renamed/re-documented if the intended behavior is value replacement. The current implementation does neither and produces incorrect response headers.

How to reproduce

  1. Enable the modify-response plugin.
  2. Configure a rule with replaceHeaderKeys mapping X-Old to X-New.
  3. Let the upstream return X-Old: abc.
  4. The gateway returns X-Old: X-New instead of X-New: abc.

Debug logs

No response

Environment

Current master branch.

Are you willing to submit a PR?

  • Yes I am willing to submit a PR!

No activity

Activity on this issue will appear here.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions