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 java8 optional retrieval and growing in SpEL #26209

Closed
NaveenGurram opened this issue Dec 3, 2020 · 3 comments
Closed

Support java8 optional retrieval and growing in SpEL #26209

NaveenGurram opened this issue Dec 3, 2020 · 3 comments
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: duplicate A duplicate of another issue

Comments

@NaveenGurram
Copy link

we are using SpEL to set some values to a DTO and i have grow null references set to true, and hoping SpEL has support to get and set optionals and also grow null references.

for below code snippet i get following error

java org.springframework.expression.spel.SpelEvaluationException: EL1010E: Property or field 'anotherDtoField' cannot be set on object of type 'java.util.Optional' - maybe not public or not writable?

when i try updating the optional in the sampleDto it works without any issues

    spelExpressionParser.parseExpression("#sampleDto.field1='abc'").getValue(standardEvaluationContext);

it would be nice for Spring to take care of setting and getting from optional as any other java field.

  @Test
  public void testSpel(){
    SpelExpressionParser spelExpressionParser = new SpelExpressionParser(new SpelParserConfiguration(true,true));
    StandardEvaluationContext standardEvaluationContext = new StandardEvaluationContext();
    SampleDto sampleDto = new SampleDto();
    standardEvaluationContext.setVariable("sampleDto",sampleDto);
    spelExpressionParser.parseExpression("#sampleDto.field2.anotherDtoField='abc'").getValue(standardEvaluationContext);
    assertEquals("abc", sampleDto.getField1());
  }

  class SampleDto{
    private Optional<String> field1;
    private Optional<AnotherDto> field2;
    public Optional<String> getField1() {
      return field1;
    }
    public void setField1(Optional<String> field1) {
      this.field1 = field1;
    }
    public Optional<AnotherDto> getField2() {
      return field2;
    }
    public void setField2(
        Optional<AnotherDto> field2) {
      this.field2 = field2;
    }
  }
  class AnotherDto{
    private String anotherDtoField;
    public String getAnotherDtoField() {
      return anotherDtoField;
    }
    public void setAnotherDtoField(String anotherDtoField) {
      this.anotherDtoField = anotherDtoField;
    }
  }
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Dec 3, 2020
@quaff
Copy link
Contributor

quaff commented Dec 4, 2020

Using Optional as field is not a good practice. https://stackoverflow.com/a/29033935/391148

@rstoyanchev rstoyanchev added the in: core Issues in core modules (aop, beans, core, context, expression) label Nov 10, 2021
@drekbour
Copy link

drekbour commented Oct 7, 2022

Using Optional as field is not a good practice. https://stackoverflow.com/a/29033935/391148

Agree but this feels dismissive and doesn't change a fairly valid request. Getter Optional<AnotherDto> getField2() could just as easily be computing it's return value.

The use-case that drew me here is not to auto-grow an Optional but evaluate through it like this: obj.optEnum == 'MYENUM_NAME' where Optional<MyEnum> getOptEnum(). The rules on absent etc are fairly self evident.

I think users just want native handling for Optional as they have for Collection. Links to #20433

@snicoll
Copy link
Member

snicoll commented Oct 2, 2023

Let's close this one in favor of #20433. It looks like we can look at this particular use case if we consider adding support for Optional at all.

@snicoll snicoll closed this as not planned Won't fix, can't repro, duplicate, stale Oct 2, 2023
@snicoll snicoll added status: duplicate A duplicate of another issue and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Oct 2, 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: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

6 participants