Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support record canonical constructor in BeanUtils #33707

Closed
wants to merge 1 commit into from

Conversation

EugeneNik
Copy link
Contributor

@EugeneNik EugeneNik commented Oct 15, 2024

Affected version where noticed: spring-beans: 6.2.0-RC1
I'm not sure is it crucial or not, but method becomes to be invoked after migration from my MvcResult to MvcTestResult and to MockMvcTester

When one of the requestParameters is implemented as record with multiple public constructors, binding fails with

java.lang.IllegalStateException: No primary or single unique constructor found for class org.myconfs.security.jwt.JwtUser
	at org.springframework.beans.BeanUtils.getResolvableConstructor(BeanUtils.java:265)
	at org.springframework.validation.DataBinder.createObject(DataBinder.java:928)
	at org.springframework.validation.DataBinder.construct(DataBinder.java:907)
	at org.springframework.web.bind.ServletRequestDataBinder.construct(ServletRequestDataBinder.java:116)
	at org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.constructAttribute(ServletModelAttributeMethodProcessor.java:157)
	at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:148)
	at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:122)
	at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:224)
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:178)
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:118)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:986)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:891)
	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1088)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:978)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1014)
	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:903)
	at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:564)
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:885)

class implementation:

public record JwtUser(Long id, String username, String email, String password,
                      Collection<GrantedAuthority> authorities, boolean enabled,
                      LocalDateTime lastPermissionResetDate) implements UserDetails {

    public JwtUser(String username, String password, Collection<GrantedAuthority> authorities) {
        this(null, username, "", password, authorities, false, null);
    }

    public static JwtUser withLastPermissionResetDate(JwtUser user, LocalDateTime lastPermissionResetDate) {
        return new JwtUser(user.id, user.username, user.email, user.password, user.authorities, user.enabled, lastPermissionResetDate);
    }

  ...
}

There is a suggestion to improve BeanUtils.getResolvableConstructor method to work with records with more than 1 public constructor using the fact that there is only one canonical record's constructor, which shall contain all record components

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Oct 15, 2024
@sdeleuze sdeleuze added in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Oct 16, 2024
@sdeleuze sdeleuze self-assigned this Oct 16, 2024
@sdeleuze sdeleuze added this to the 6.2.0-RC2 milestone Oct 16, 2024
sdeleuze pushed a commit to sdeleuze/spring-framework that referenced this pull request Oct 16, 2024
@sdeleuze sdeleuze closed this in effe606 Oct 16, 2024
@sdeleuze
Copy link
Contributor

Thanks for your contribution, I applied some significant refinements as part of effe606, but the principle is very close.

@sdeleuze sdeleuze changed the title Add resolution of record's canonical constructor if multiple public constructors exist Support record canonical constructor in BeanUtils Oct 16, 2024
@EugeneNik
Copy link
Contributor Author

Thank you! Looks good

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants