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

Resource collection injection by @Value regression #31693

Closed
quaff opened this issue Nov 27, 2023 · 1 comment · Fixed by #31699
Closed

Resource collection injection by @Value regression #31693

quaff opened this issue Nov 27, 2023 · 1 comment · Fixed by #31699
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: superseded An issue that has been superseded by another type: regression A bug that is also a regression

Comments

@quaff
Copy link
Contributor

quaff commented Nov 27, 2023

import static org.assertj.core.api.Assertions.assertThat;

import java.util.List;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

@SpringJUnitConfig
public class ResourcesInjectionByFieldTests {

	@Value("classpath:application.yaml")
	private Resource[] resourcesArray;

	@Value("classpath:application.yaml")
        // @Value("classpath*:application.yaml") will works with 6.1
	private List<Resource> resourcesList;

	@Test
	void test() {
		assertThat(resourcesArray).isNotEmpty();
		assertThat(resourcesList).isNotEmpty();
	}

}

This test case failed after upgrading spring framework from 6.0 to 6.1, I believe it introduced by 84b3335 which fixed #24845. It's possible that this will fix a spring boot regression.

15:51:49.983 [main] INFO org.springframework.test.context.support.AnnotationConfigContextLoaderUtils -- Could not detect default configuration classes for test class [com.example.demo.ResourcesInjectionByFieldTests]: ResourcesInjectionByFieldTests does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
15:51:50.141 [main] ERROR org.springframework.test.context.TestContextManager -- Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener] to prepare test instance [com.example.demo.ResourcesInjectionByFieldTests@737a135b]
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.example.demo.ResourcesInjectionByFieldTests': Unsatisfied dependency expressed through field 'resourcesList': Failed to convert value of type 'java.lang.String' to required type 'java.util.List'; Cannot convert value of type 'java.lang.String' to required type 'java.util.List': PropertyEditor [org.springframework.core.io.support.ResourceArrayPropertyEditor] returned inappropriate value of type 'org.springframework.core.io.ClassPathResource'
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:772)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:752)
	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:145)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:493)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1420)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:397)
	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:142)
	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:97)
	at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:247)
	at org.springframework.test.context.junit.jupiter.SpringExtension.postProcessTestInstance(SpringExtension.java:163)
...
Caused by: org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.util.List'; Cannot convert value of type 'java.lang.String' to required type 'java.util.List': PropertyEditor [org.springframework.core.io.support.ResourceArrayPropertyEditor] returned inappropriate value of type 'org.springframework.core.io.ClassPathResource'
	at org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:87)
	at org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:71)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1379)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1348)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:769)
	... 76 common frames omitted
Caused by: java.lang.IllegalArgumentException: Cannot convert value of type 'java.lang.String' to required type 'java.util.List': PropertyEditor [org.springframework.core.io.support.ResourceArrayPropertyEditor] returned inappropriate value of type 'org.springframework.core.io.ClassPathResource'
	at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:268)
	at org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:80)
	... 80 common frames omitted
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Nov 27, 2023
@jhoeller jhoeller added type: regression A bug that is also a regression in: core Issues in core modules (aop, beans, core, context, expression) and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Nov 27, 2023
@jhoeller jhoeller self-assigned this Nov 27, 2023
@jhoeller jhoeller added this to the 6.1.2 milestone Nov 27, 2023
@jhoeller
Copy link
Contributor

Superseded by PR #31699.

@jhoeller jhoeller added the status: superseded An issue that has been superseded by another label Nov 30, 2023
@jhoeller jhoeller removed this from the 6.1.2 milestone Nov 30, 2023
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) status: superseded An issue that has been superseded by another type: regression A bug that is also a regression
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants