4

I want to be able to have something like:

some-property: '#{${ENV_VAR_1:} == "someVal" ? boo + "someVal" : ${ENV_VAR_1}}'

Is something like this possible?

When I try this currently, I get the error:

Caused by: java.lang.IllegalArgumentException: Operand must not be null at org.springframework.util.Assert.notNull(Assert.java:198) ~[spring-core-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.expression.spel.ast.SpelNodeImpl.(SpelNodeImpl.java:77) ~[spring-expression-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.expression.spel.ast.Ternary.(Ternary.java:40) ~[spring-expression-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.expression.spel.standard.InternalSpelExpressionParser.eatExpression(InternalSpelExpressionParser.java:183) ~[spring-expression-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.expression.spel.standard.InternalSpelExpressionParser.doParseExpression(InternalSpelExpressionParser.java:131) ~[spring-expression-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.expression.spel.standard.SpelExpressionParser.doParseExpression(SpelExpressionParser.java:61) ~[spring-expression-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.expression.spel.standard.SpelExpressionParser.doParseExpression(SpelExpressionParser.java:33) ~[spring-expression-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.expression.common.TemplateAwareExpressionParser.parseExpressions(TemplateAwareExpressionParser.java:121) ~[spring-expression-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.expression.common.TemplateAwareExpressionParser.parseTemplate(TemplateAwareExpressionParser.java:62) ~[spring-expression-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.expression.common.TemplateAwareExpressionParser.parseExpression(TemplateAwareExpressionParser.java:49) ~[spring-expression-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.context.expression.StandardBeanExpressionResolver.evaluate(StandardBeanExpressionResolver.java:142) ~[spring-context-5.1.3.RELEASE.jar:5.1.3.RELEASE] ... 51 common frames omitted

Don Rhummy
  • 24,730
  • 42
  • 175
  • 330
  • I think that you can find your answer at this link: [https://stackoverflow.com/questions/23934213/conditional-check-in-yaml-file-to-show-the-proper-content](https://stackoverflow.com/questions/23934213/conditional-check-in-yaml-file-to-show-the-proper-content) – Matteo Tomai Jan 09 '19 at 14:45
  • Found a beautiful solution without SpEL right here: https://stackoverflow.com/a/56321961/2032157 – Alexander Melnichuk Apr 10 '20 at 02:28

4 Answers4

7

you can also do like below using SPEL in YML -

some-property: '#{"${ENV_VAR_1:}".equals("someVal")?"boo" + "someVal":"${ENV_VAR_1:}"}'
fortm
  • 4,066
  • 5
  • 49
  • 79
2

I think you can use multi-profile instead of if/else. like this:

---
spring:
    profiles: VAR_1
some-property: boo
---
spring:
    profiles: VAR_2, VAR_3
some-property: foo
Juey
  • 123
  • 1
  • 6
1

In your application.yaml add an environment variable reference to your spring.profiles.active.

Then add profiles for each condition and a profile for the default case:

spring:
  profiles:
    active:
      - ${ENV_VAR_1:default}


---
spring:
  profiles: default

some-property: defaultValue

---
spring:
  profiles: env1

some-property: env1Value

---
spring:
  profiles: env2

some-property: env2Value  
Jeff
  • 3,712
  • 2
  • 22
  • 24
0

You're definitely on the right track; you're passing in a SPEL expression that looks like it's attempting to get evaluated.

It appears that one of the operands is null (hence the Operand must not be null).

You'll have to very this, but it looks like:

${ENV_VAR_1:}

Is evaluating to null. It is clearly parsing a ternary, which is part of your stacktrace, but one of those operands needs to have a value. I'd create a default for the ENV_VARs, like ${ENV_VAR_1:default} and see if the parsing works for you. Otherwise, check if boo is null and test with a static value for that.

Dovmo
  • 8,121
  • 3
  • 30
  • 44