Skip to content

Commit 1cc767e

Browse files
committed
Polishing in ExtendedServletRequestDataBinder
1 parent 34d6dd9 commit 1cc767e

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

spring-context/src/main/java/org/springframework/validation/DataBinder.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -1412,6 +1412,9 @@ public interface ValueResolver {
14121412

14131413
/**
14141414
* Return the names of all property values.
1415+
* <p>Useful for proactive checks whether there are property values nested
1416+
* further below the path for a constructor arg. If not then the
1417+
* constructor arg can be considered missing and not to be instantiated.
14151418
* @since 6.1.2
14161419
*/
14171420
Set<String> getNames();

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ExtendedServletRequestDataBinder.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ protected void addBindValues(MutablePropertyValues mpvs, ServletRequest request)
125125
String name = names.nextElement();
126126
Object value = getHeaderValue(httpRequest, name);
127127
if (value != null) {
128-
name = StringUtils.uncapitalize(name.replace("-", ""));
128+
name = normalizeHeaderName(name);
129129
addValueIfNotPresent(mpvs, "Header", name, value);
130130
}
131131
}
@@ -173,6 +173,10 @@ private Object getHeaderValue(HttpServletRequest request, String name) {
173173
return values;
174174
}
175175

176+
private static String normalizeHeaderName(String name) {
177+
return StringUtils.uncapitalize(name.replace("-", ""));
178+
}
179+
176180

177181
/**
178182
* Resolver of values that looks up URI path variables.
@@ -209,8 +213,10 @@ protected Set<String> initParameterNames(ServletRequest request) {
209213
if (request instanceof HttpServletRequest httpServletRequest) {
210214
Enumeration<String> enumeration = httpServletRequest.getHeaderNames();
211215
while (enumeration.hasMoreElements()) {
212-
String headerName = enumeration.nextElement();
213-
set.add(headerName.replaceAll("-", ""));
216+
String name = enumeration.nextElement();
217+
if (headerPredicate.test(name)) {
218+
set.add(normalizeHeaderName(name));
219+
}
214220
}
215221
}
216222
return set;

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ExtendedServletRequestDataBinderTests.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,23 @@ void filteredHeaders(String headerName) {
119119
assertThat(mpvs).isEmpty();
120120
}
121121

122+
@Test
123+
void headerPredicateWithConstructorArgs() {
124+
ExtendedServletRequestDataBinder binder = new ExtendedServletRequestDataBinder(null);
125+
binder.addHeaderPredicate(name -> !name.equalsIgnoreCase("Some-Int-Array"));
126+
binder.setTargetType(ResolvableType.forClass(DataBean.class));
127+
binder.setNameResolver(new BindParamNameResolver());
128+
129+
request.addHeader("Some-Int-Array", "1");
130+
request.addHeader("Some-Int-Array", "2");
131+
132+
binder.construct(request);
133+
134+
DataBean bean = (DataBean) binder.getTarget();
135+
136+
assertThat(bean.someIntArray()).isNull();
137+
}
138+
122139
@Test
123140
void headerPredicate() {
124141
TestBinder binder = new TestBinder();

0 commit comments

Comments
 (0)