Avoid nested constructor binding if there are no request parameters

Closes gh-31821
This commit is contained in:
rstoyanchev
2023-12-13 18:03:30 +00:00
parent 0970b1dc7a
commit ec0ec7a0d6
5 changed files with 103 additions and 7 deletions

View File

@@ -17,7 +17,10 @@
package org.springframework.web.bind;
import java.lang.reflect.Array;
import java.util.Enumeration;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.http.HttpServletRequest;
@@ -213,6 +216,9 @@ public class ServletRequestDataBinder extends WebDataBinder {
private final WebDataBinder dataBinder;
@Nullable
private Set<String> parameterNames;
protected ServletRequestValueResolver(ServletRequest request, WebDataBinder dataBinder) {
this.request = request;
this.dataBinder = dataBinder;
@@ -261,6 +267,23 @@ public class ServletRequestDataBinder extends WebDataBinder {
}
return null;
}
@Override
public Set<String> getNames() {
if (this.parameterNames == null) {
this.parameterNames = initParameterNames(this.request);
}
return this.parameterNames;
}
protected Set<String> initParameterNames(ServletRequest request) {
Set<String> set = new LinkedHashSet<>();
Enumeration<String> enumeration = request.getParameterNames();
while (enumeration.hasMoreElements()) {
set.add(enumeration.nextElement());
}
return set;
}
}
}

View File

@@ -18,6 +18,7 @@ package org.springframework.web.bind.support;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import reactor.core.publisher.Mono;
@@ -164,6 +165,11 @@ public class WebExchangeDataBinder extends WebDataBinder {
public Object resolveValue(String name, Class<?> type) {
return this.map.get(name);
}
@Override
public Set<String> getNames() {
return this.map.keySet();
}
}
}