Merge branch '6.2.x'

This commit is contained in:
rstoyanchev 2025-01-28 15:37:38 +00:00
commit 52c187bf64
6 changed files with 54 additions and 11 deletions

View File

@ -1379,6 +1379,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

@ -309,15 +309,19 @@ public class HandlerMethod extends AnnotatedMethod {
} }
/** /**
* If the provided instance contains a bean name rather than an object instance, * If the {@link #getBean() handler} is a bean name rather than the actual
* the bean name is resolved before a {@link HandlerMethod} is created and returned. * handler instance, resolve the bean name through Spring configuration
* (e.g. for prototype beans), and return a new {@link HandlerMethod}
* instance with the resolved handler.
* <p>If the {@link #getBean() handler} is not String, return the same instance.
*/ */
public HandlerMethod createWithResolvedBean() { public HandlerMethod createWithResolvedBean() {
Object handler = this.bean; if (!(this.bean instanceof String beanName)) {
if (this.bean instanceof String beanName) { return this;
Assert.state(this.beanFactory != null, "Cannot resolve bean name without BeanFactory");
handler = this.beanFactory.getBean(beanName);
} }
Assert.state(this.beanFactory != null, "Cannot resolve bean name without BeanFactory");
Object handler = this.beanFactory.getBean(beanName);
Assert.notNull(handler, "No handler instance"); Assert.notNull(handler, "No handler instance");
return new HandlerMethod(this, handler, false); return new HandlerMethod(this, handler, false);
} }

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.
@ -72,6 +72,13 @@ class HandlerMethodTests {
testValidateReturnValue(target, List.of("getPerson"), false); testValidateReturnValue(target, List.of("getPerson"), false);
} }
@Test // gh-34277
void createWithResolvedBeanSameInstance() {
MyClass target = new MyClass();
HandlerMethod handlerMethod = getHandlerMethod(target, "addPerson");
assertThat(handlerMethod.createWithResolvedBean()).isSameAs(handlerMethod);
}
private static void testValidateArgs(Object target, List<String> methodNames, boolean expected) { private static void testValidateArgs(Object target, List<String> methodNames, boolean expected) {
for (String methodName : methodNames) { for (String methodName : methodNames) {
assertThat(getHandlerMethod(target, methodName).shouldValidateArguments()).isEqualTo(expected); assertThat(getHandlerMethod(target, methodName).shouldValidateArguments()).isEqualTo(expected);

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);
} }
} }
@ -171,6 +171,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.
@ -206,8 +210,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();

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2022 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.
@ -72,6 +72,12 @@ public interface WebSocketSession extends Closeable {
/** /**
* Return the address on which the request was received. * Return the address on which the request was received.
* <p><strong>Note:</strong> The localAddress is not always possible to access,
* which is the case with the Standard WebSocket client. In 6.2.x
* {@link org.springframework.web.socket.client.standard.StandardWebSocketClient}
* returns an address based on the local host and the port of the target
* address (not the same as the local port). In 7.0, the same will return
* {@code null} instead.
*/ */
@Nullable InetSocketAddress getLocalAddress(); @Nullable InetSocketAddress getLocalAddress();