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

Unable to evaluate Map as Context #102

Open
tarkal opened this issue May 2, 2022 · 5 comments
Open

Unable to evaluate Map as Context #102

tarkal opened this issue May 2, 2022 · 5 comments

Comments

@tarkal
Copy link

tarkal commented May 2, 2022

I need to be able to use this with a context that is a Map<String, Object>. The context is created at run time so I cant define it as a POJO. Unfortunately the library currently doesn't seem to be able to handle a Map context. The output file seems to just skip over every single placeholder.

The test context I'm passing in looks like this in JSON:

{
   "iterator": {
      "name":"John Doe"
   }
}

I have tried the following placeholders but none work:

${iterator.name}
${iterator['name']}
${['iterator']['name']}
${['iterator'].['name']}

Any suggestions?

@tarkal
Copy link
Author

tarkal commented May 3, 2022

OK this not a fix but it is a workaround until this issue is resolved. You can create a POJO that has a single property of type Map<String, Object> then it seems to work. I created a property called ctx and then it works with this:

${ctx['iterator']['name']}

@caring-coder
Copy link

FYI, you can have a Map as a context and make spel evaluate it by configurating the EvaluationContext thusly:

DocxStamperConfiguration config = new DocxStamperConfiguration();
config.setEvaluationContextConfigurer(ctx -> ctx.addPropertyAccessor(new MapAccessor()));
DocxStamper<Map<String, Object>> stamper = new DocxStamper<>(config);
stamper.stamp(/*template*/, /*data map*/, /*output*/);

You should at least be able to have this in the word template

${iterator['name']}

@tarkal
Copy link
Author

tarkal commented May 16, 2022

Where does MapAccessor() come from?

@caring-coder
Copy link

caring-coder commented May 16, 2022

From org.springframework.context.expression
It's the package that helps support the 'Spel' Spring expression language.

@tarkal
Copy link
Author

tarkal commented May 16, 2022

Amazing, it wasn't imported with docx-stamper. For anyone else you can get it here:

<dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-context</artifactId>
     <version>${springVersion}</version>
</dependency>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants