Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep the - of header on init parameter names, otherwise the nested path will not work. #34193

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading