Skip to content

Commit

Permalink
Do not convert @Header name when the name is specified (#5841)
Browse files Browse the repository at this point in the history
Motivation:
We must not convert the header name specified in `@Header` when creating an annotated service.
This is a regression introduced by #5547, affecting 1.29.0 through 1.29.3.

Modifications:
- Do not convert the header name specified in `@Header`.

Result:
- An annotated service that has a `@Header` with the specified name now works correctly.
  • Loading branch information
minwoox authored Jul 30, 2024
1 parent 97d2a6e commit cd1973b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
final class AnnotatedElementNameUtil {

/**
* Returns the value of {@link Header}, {@link Param}, {@link Attribute} if the value is not blank.
* Returns the value of {@link Param}, {@link Attribute} if the value is not blank.
* If the value is blank, it returns the name of the specified {@code nameRetrievalTarget} object
* which is an instance of {@link Header}, {@link Param}, {@link Attribute} or {@link Field}.
* which is an instance of {@link Param}, {@link Attribute} or {@link Field}.
*/
static String findName(Object nameRetrievalTarget, String value) {
requireNonNull(nameRetrievalTarget, "nameRetrievalTarget");
Expand All @@ -44,6 +44,27 @@ static String findName(Object nameRetrievalTarget, String value) {
return getName(nameRetrievalTarget);
}

/**
* Returns the value of the {@link Header} annotation which is specified on the {@code element} if
* the value is not blank. If the value is blank, it returns the name of the specified
* {@code nameRetrievalTarget} object which is an instance of {@link Parameter} or {@link Field}.
*
* <p>Note that the name of the specified {@code nameRetrievalTarget} will be converted as
* {@link CaseFormat#LOWER_HYPHEN} that the string elements are separated with one hyphen({@code -})
* character. The value of the {@link Header} annotation will not be converted because it is clearly
* specified by a user.
*/
static String findName(Header header, Object nameRetrievalTarget) {
requireNonNull(nameRetrievalTarget, "nameRetrievalTarget");

final String value = header.value();
if (DefaultValues.isSpecified(value)) {
checkArgument(!value.isEmpty(), "value is empty.");
return value;
}
return toHeaderName(getName(nameRetrievalTarget));
}

/**
* Returns the name of the specified element or the default name if it can't get.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import static com.linecorp.armeria.internal.server.annotation.AnnotatedElementNameUtil.findName;
import static com.linecorp.armeria.internal.server.annotation.AnnotatedElementNameUtil.getName;
import static com.linecorp.armeria.internal.server.annotation.AnnotatedElementNameUtil.getNameOrDefault;
import static com.linecorp.armeria.internal.server.annotation.AnnotatedElementNameUtil.toHeaderName;
import static com.linecorp.armeria.internal.server.annotation.AnnotatedServiceFactory.findDescription;
import static com.linecorp.armeria.internal.server.annotation.AnnotatedServiceTypeUtil.stringToType;
import static com.linecorp.armeria.internal.server.annotation.DefaultValues.getSpecifiedValue;
Expand Down Expand Up @@ -471,7 +470,7 @@ private static AnnotatedValueResolver of(AnnotatedElement annotatedElement,

final Header header = annotatedElement.getAnnotation(Header.class);
if (header != null) {
final String name = toHeaderName(findName(typeElement, header.value()));
final String name = findName(header, typeElement);
return ofHeader(name, annotatedElement, typeElement, type, description);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,12 @@ public String customHeader5(@Header List<Integer> numbers,
String.join(":", strings);
}

@Post("/headerNameSpecified")
public String headerNameSpecified(@Header("X-x-FoO-bAr") String id) {
// Because the header name is specified, it's not converted.
return id;
}

@Get("/headerDefault")
public String headerDefault(RequestContext ctx,
@Header @Default("hello") String username,
Expand Down Expand Up @@ -1224,6 +1230,10 @@ void testRequestHeaderInjection() throws Exception {
request.addHeader("strings", "minwoox");
testBody(hc, request, "1:2:1/minwoox:giraffe");

request = post("/11/headerNameSpecified");
request.addHeader("X-x-FoO-bAr", "qwerty");
testBody(hc, request, "qwerty");

request = get("/11/headerDefault");
testBody(hc, request, "hello/world/(null)");

Expand Down

0 comments on commit cd1973b

Please sign in to comment.