Skip to content

Allow auto grow for entries without default constructor in SpEL #25367

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

Closed
wants to merge 4 commits into from
Closed

Conversation

MartinKnopf
Copy link

Hi,

I wanted to add a BigDecimal to a list using SpEL. Auto grow only works for element types with a default constructor and since BigDecimal does not have a default constructor auto grow does not work with my list.

Here is an example:

public class SpelTest {

    public List<BigDecimal> values;
    
    SpelExpressionParser parser;

    @Before
    public void setup() {
        values = new ArrayList<>();
        parser = new SpelExpressionParser(new SpelParserConfiguration(true, true));
    }

    @Test
    public void shouldChangeValue() {
        values.add(BigDecimal.ONE);

        parser.parseExpression("values[0]").setValue(this, "123.4");

        assertThat(values.get(0)).isEqualTo(BigDecimal.valueOf(123.4)); // passes
    }

    @Test
    public void shouldAddValue() {
        parser.parseExpression("values[0]").setValue(this, "123.4");

        assertThat(values.get(0)).isEqualTo(BigDecimal.valueOf(123.4)); // fails
    }
}

Changing the first entry passes but adding an entry fails with

Caused by: java.lang.NoSuchMethodException: java.math.BigDecimal.<init>()
    at java.base/java.lang.Class.getConstructor0(Class.java:3349)
    at java.base/java.lang.Class.getDeclaredConstructor(Class.java:2553)
    at org.springframework.util.ReflectionUtils.accessibleConstructor(ReflectionUtils.java:185)
    at org.springframework.expression.spel.ast.Indexer$CollectionIndexingValueRef.growCollectionIfNecessary(Indexer.java:715)
    ... 55 more

My solution inserts null when no default constructor can be found for the element type. Not sure if this is a good solution but at least it does not seem to break any tests. Please be nice :-)

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jul 7, 2020
@sbrannen
Copy link
Member

sbrannen commented Jul 9, 2020

@aclement, would you mind taking a quick look at this to assess the implementation within the context of SpEL?

@sbrannen sbrannen added in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement labels Jul 9, 2020
@sbrannen sbrannen changed the title Allow auto grow for entries without default constructor Allow auto grow for entries without default constructor in SpEL Jul 9, 2020
@sbrannen sbrannen requested a review from aclement July 9, 2020 15:48
@aclement
Copy link
Contributor

I think I'm ok with that change. Might be worth a line in the section in the docs around Parser configuration to mention you might get nulls in the array if it is expanded and there is no default ctor for that element type. As I recall SpEL compilation doesn't currently support collection growing so there is no impact on that mode with changes in this area.

@sbrannen sbrannen removed the status: waiting-for-triage An issue we've not yet triaged or decided on label Jul 13, 2020
@sbrannen sbrannen added this to the 5.3 M2 milestone Jul 13, 2020
@sbrannen
Copy link
Member

I think I'm ok with that change.

Thanks for the quick review, @aclement. Much appreciated!

Might be worth a line in the section in the docs around Parser configuration to mention you might get nulls in the array if it is expanded and there is no default ctor for that element type. As I recall SpEL compilation doesn't currently support collection growing so there is no impact on that mode with changes in this area.

@HorseD, would you mind adding such documentation to the reference manual (core-expressions.adoc) and including that in this PR?

@sbrannen sbrannen added the status: waiting-for-feedback We need additional information before we can continue label Jul 14, 2020
@sbrannen sbrannen self-assigned this Jul 14, 2020
@MartinKnopf
Copy link
Author

Hey,

I added a note to the reference manual plus one more unit test.

Have a good day!

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Jul 16, 2020
@sbrannen
Copy link
Member

I added a note to the reference manual plus one more unit test.

Thanks

Have a good day!

You, too!

@sbrannen sbrannen closed this in 35c0ae7 Jul 16, 2020
sbrannen added a commit that referenced this pull request Jul 16, 2020
@sbrannen
Copy link
Member

This has been merged into master in 35c0ae7 and slightly revised in b0570cd.

Thanks

sbrannen added a commit to sbrannen/spring-framework that referenced this pull request Jul 16, 2020
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: feedback-provided Feedback has been provided type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants