Polishing in ExtendedServletRequestDataBinder

This commit is contained in:
rstoyanchev 2025-01-22 11:09:17 +00:00
parent 34d6dd9b62
commit 1cc767e90b
3 changed files with 30 additions and 4 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2024 the original author or authors. * Copyright 2002-2025 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -1412,6 +1412,9 @@ public class DataBinder implements PropertyEditorRegistry, TypeConverter {
/** /**
* Return the names of all property values. * Return the names of all property values.
* <p>Useful for proactive checks whether there are property values nested
* further below the path for a constructor arg. If not then the
* constructor arg can be considered missing and not to be instantiated.
* @since 6.1.2 * @since 6.1.2
*/ */
Set<String> getNames(); Set<String> getNames();

View File

@ -125,7 +125,7 @@ public class ExtendedServletRequestDataBinder extends ServletRequestDataBinder {
String name = names.nextElement(); String name = names.nextElement();
Object value = getHeaderValue(httpRequest, name); Object value = getHeaderValue(httpRequest, name);
if (value != null) { if (value != null) {
name = StringUtils.uncapitalize(name.replace("-", "")); name = normalizeHeaderName(name);
addValueIfNotPresent(mpvs, "Header", name, value); addValueIfNotPresent(mpvs, "Header", name, value);
} }
} }
@ -173,6 +173,10 @@ public class ExtendedServletRequestDataBinder extends ServletRequestDataBinder {
return values; return values;
} }
private static String normalizeHeaderName(String name) {
return StringUtils.uncapitalize(name.replace("-", ""));
}
/** /**
* Resolver of values that looks up URI path variables. * Resolver of values that looks up URI path variables.
@ -209,8 +213,10 @@ public class ExtendedServletRequestDataBinder extends ServletRequestDataBinder {
if (request instanceof HttpServletRequest httpServletRequest) { if (request instanceof HttpServletRequest httpServletRequest) {
Enumeration<String> enumeration = httpServletRequest.getHeaderNames(); Enumeration<String> enumeration = httpServletRequest.getHeaderNames();
while (enumeration.hasMoreElements()) { while (enumeration.hasMoreElements()) {
String headerName = enumeration.nextElement(); String name = enumeration.nextElement();
set.add(headerName.replaceAll("-", "")); if (headerPredicate.test(name)) {
set.add(normalizeHeaderName(name));
}
} }
} }
return set; return set;

View File

@ -119,6 +119,23 @@ class ExtendedServletRequestDataBinderTests {
assertThat(mpvs).isEmpty(); assertThat(mpvs).isEmpty();
} }
@Test
void headerPredicateWithConstructorArgs() {
ExtendedServletRequestDataBinder binder = new ExtendedServletRequestDataBinder(null);
binder.addHeaderPredicate(name -> !name.equalsIgnoreCase("Some-Int-Array"));
binder.setTargetType(ResolvableType.forClass(DataBean.class));
binder.setNameResolver(new BindParamNameResolver());
request.addHeader("Some-Int-Array", "1");
request.addHeader("Some-Int-Array", "2");
binder.construct(request);
DataBean bean = (DataBean) binder.getTarget();
assertThat(bean.someIntArray()).isNull();
}
@Test @Test
void headerPredicate() { void headerPredicate() {
TestBinder binder = new TestBinder(); TestBinder binder = new TestBinder();