Skip to content

Add support to any Iterable types on PropertyAccessor #907

New issue

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

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

Already on GitHub? # to your account

Conversation

ljtfreitas
Copy link

Sometimes, for better clarity of the application model, we create objects that encapsulate a Collection, and this object comes to represent a collection of a certain type in the application model. To simplify for-each operations, this object must implement java.lang.Iterable. Example:

public class MyType {
    @NotNull
    private String name;
}

public class MyTypesGroup implements Iterable<MyType> {
    private Collection <MyType> myTypes = new ArrayList <MyType> ();

    @Override
    public Iterator<MyType> iterator() {
         return myTypes.iterator();
    }

    // model methods
}

public class MyAggregateType {
     @Valid
     private MyTypesGroup myTypesGroup MyTypesGroup = new ();
}

Databind, get and set properties operations, typically used with BeanWrapper, they are not supported for these objects (in the above example, something like "myTypesGroup[0].name").

In my use case, with Hibernate Validator, the path will be generated "myTypesGroup[0].name" (Hibernate Validator au), occurring an exception when the Spring tries to extract the property value (to generate the error message on the Spring MVC)

This pull request adds java.lang.Iterable as a supported type for reading properties by PropertyAccessors.

I have signed and agree to the terms of the Spring ICLA.

Thanks (and sorry for any English errors :))

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jan 24, 2019
@pivotal-issuemaster
Copy link

@ljtfreitas Please sign the Contributor License Agreement!

Click here to manually synchronize the status of this Pull Request.

See the FAQ for frequently asked questions.

@rstoyanchev rstoyanchev added the in: core Issues in core modules (aop, beans, core, context, expression) label Nov 11, 2021
@pivotal-cla
Copy link

@ljtfreitas Please sign the Contributor License Agreement!

Click here to manually synchronize the status of this Pull Request.

See the FAQ for frequently asked questions.

@jhoeller jhoeller self-assigned this Nov 22, 2021
@rstoyanchev rstoyanchev added this to the 6.0.x milestone Nov 23, 2021
@rstoyanchev rstoyanchev added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Nov 23, 2021
@jhoeller jhoeller modified the milestones: 6.0.x, 6.x Backlog Jan 11, 2023
@jhoeller jhoeller modified the milestones: 6.x Backlog, 6.1.0-M2 Jul 11, 2023
@jhoeller
Copy link
Contributor

As part of the wider binding and validation theme in 6.1, I've picked up this one in a custom fashion, unified with our Set/Collection handling (just doing the size check after the iteration rather than before).

Your pull request was a great starting point there, thanks for your efforts! Sorry for not revisiting it earlier...

@jhoeller jhoeller closed this in c20a2e4 Jul 11, 2023
sbrannen added a commit that referenced this pull request Jan 29, 2025
The changes in commit c20a2e4 introduced a regression with regard to
binding to a Map property when the Map also happens to implement
Iterable.

Although that is perhaps not a very common scenario, this commit
reorders the if-blocks in AbstractNestablePropertyAccessor's
getPropertyValue(PropertyTokenHolder) method so that a Map is
considered before an Iterable, thereby allowing an Iterable-Map to be
accessed as a Map.

See gh-907
Closes gh-34332
sbrannen added a commit that referenced this pull request Jan 29, 2025
The changes in commit c20a2e4 introduced a regression with regard to
binding to a Map property when the Map also happens to implement
Iterable.

Although that is perhaps not a very common scenario, this commit
reorders the if-blocks in AbstractNestablePropertyAccessor's
getPropertyValue(PropertyTokenHolder) method so that a Map is
considered before an Iterable, thereby allowing an Iterable-Map to be
accessed as a Map.

See gh-907
Closes gh-34332

(cherry picked from commit b9e43d0)
# for free to join this conversation on GitHub. Already have an account? # 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.

6 participants