Allow configuration of SpEL
Closed this issue · 4 comments
Is your feature request related to a problem? Please describe.
We want to have our .docx templates as simple as possible. It might be confusing for users, that are creating templates, to think about which values might be null, so we want to avoid nullsafe operators like ?. in the templates.
Describe the solution you'd like
Instead we would like to configure the SpelExpressionParser to handle null values:
val parser = SpelExpressionParser(SpelParserConfiguration(
/* autoGrowNullReferences = */ true, /* autoGrowCollections = */ true
))
Describe alternatives you've considered
Currently it is not possible to change the configuration of the SpelExpressionParser, it always uses the default configuration:
https://github.com/thombergs/docx-stamper/blob/8243bd7ab969599379d34396b09ee9ed4caccd1a/src/main/java/org/wickedsource/docxstamper/el/ExpressionResolver.java#L49
So instead we would have to subclass a lot to change this setting.
Additional context
Another alternative would be to be extra careful in the template to only create non-null values, but this might be error prone
While i understand your point, what would be the resulting document look like ?
I feel you can already deal with null values by setting the DocxStamperConfiguration().setFailOnUnresolvedExpression(false), and it make the stamper leave all null values untouched, and keep the placeholder as is in the resulting document.
I see. Actually we are using the templates like this
${user.address.street ?: '______________________'}
To have a fallback for empty / null values configurable in the template. With the setting fallOnUnresolvedException(false) you would see the whole template instead of underscores right?
Understood, i'll do a new version allowing this configuration, you can check file src/test/java/org/wickedsource/docxstamper/NullPointerResolutionTest.java in pull request #177 to tell me if it's enough for your case
Thanks, I tested that version locally (mvn install and use it in my project) and it worked 😄 looking forward to the release