Skip to content

Commit

Permalink
Keep the - of header on init parameter names, otherwise the nested …
Browse files Browse the repository at this point in the history
…path will not work.

Signed-off-by: Mengqi Xu <2663479778@qq.com>
  • Loading branch information
remeio committed Jan 8, 2025
1 parent 57c0d95 commit 89a90f1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ protected void addBindValues(MutablePropertyValues mpvs, ServletRequest request)
String name = names.nextElement();
Object value = getHeaderValue(httpRequest, name);
if (value != null) {
name = StringUtils.uncapitalize(name.replace("-", ""));
name = transformedHeaderName(name);
addValueIfNotPresent(mpvs, "Header", name, value);
}
}
Expand All @@ -147,6 +147,10 @@ private static void addValueIfNotPresent(MutablePropertyValues mpvs, String labe
}
}

private static String transformedHeaderName(String headerName) {
return StringUtils.uncapitalize(headerName.replace("-", ""));
}

private @Nullable Object getHeaderValue(HttpServletRequest request, String name) {
if (!this.headerPredicate.test(name)) {
return null;
Expand Down Expand Up @@ -206,7 +210,7 @@ protected Set<String> initParameterNames(ServletRequest request) {
Enumeration<String> enumeration = httpServletRequest.getHeaderNames();
while (enumeration.hasMoreElements()) {
String headerName = enumeration.nextElement();
set.add(headerName.replaceAll("-", ""));
set.add(headerName);
}
}
return set;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,30 @@ void createBinderViaConstructor() {
assertThat(bean.someIntArray()).containsExactly(1, 2);
}

@Test
void createBinderViaConstructorNested() {
request.addHeader("Nested-Test-Bean.Some-Int-Array", "1");
request.addHeader("Nested-Test-Bean.Some-Int-Array", "2");

ServletRequestDataBinder binder = new ExtendedServletRequestDataBinder(null);
binder.setTargetType(ResolvableType.forClass(SimpleBean.class));
binder.setNameResolver(new BindParamNameResolver());
binder.construct(request);

SimpleBean bean = (SimpleBean) binder.getTarget();

assertThat(bean.nestedTestBean()).isNotNull();
assertThat(bean.nestedTestBean().someIntArray()).containsExactly(1, 2);
}

private record SimpleBean(@BindParam("Nested-Test-Bean") NestedTestBean nestedTestBean) {

}

private record NestedTestBean(@BindParam("Some-Int-Array") Integer[] someIntArray) {

}

@Test
void uriVarsAndHeadersAddedConditionally() {
request.addParameter("name", "John");
Expand Down

0 comments on commit 89a90f1

Please sign in to comment.