Skip to content

Commit

Permalink
Polishing in ExtendedServletRequestDataBinder
Browse files Browse the repository at this point in the history
  • Loading branch information
rstoyanchev committed Jan 28, 2025
1 parent 34d6dd9 commit 1cc767e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -1412,6 +1412,9 @@ public interface ValueResolver {

/**
* 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
*/
Set<String> getNames();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,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 = normalizeHeaderName(name);
addValueIfNotPresent(mpvs, "Header", name, value);
}
}
Expand Down Expand Up @@ -173,6 +173,10 @@ private Object getHeaderValue(HttpServletRequest request, String name) {
return values;
}

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


/**
* Resolver of values that looks up URI path variables.
Expand Down Expand Up @@ -209,8 +213,10 @@ protected Set<String> initParameterNames(ServletRequest request) {
if (request instanceof HttpServletRequest httpServletRequest) {
Enumeration<String> enumeration = httpServletRequest.getHeaderNames();
while (enumeration.hasMoreElements()) {
String headerName = enumeration.nextElement();
set.add(headerName.replaceAll("-", ""));
String name = enumeration.nextElement();
if (headerPredicate.test(name)) {
set.add(normalizeHeaderName(name));
}
}
}
return set;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,23 @@ void filteredHeaders(String headerName) {
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
void headerPredicate() {
TestBinder binder = new TestBinder();
Expand Down

0 comments on commit 1cc767e

Please sign in to comment.