[MGNLRESTCL-86] Hard to get content from JsonNode object in FTL Created: 21/Oct/19 Updated: 31/Dec/19 Resolved: 16/Dec/19 |
|
| Status: | Closed |
| Project: | REST Client |
| Component/s: | None |
| Affects Version/s: | None |
| Fix Version/s: | 2.0 |
| Type: | Improvement | Priority: | Major |
| Reporter: | Christopher Zimmermann | Assignee: | Quach Hao Thien |
| Resolution: | Fixed | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | 0d | ||
| Time Spent: | 3.75d | ||
| Original Estimate: | Not Specified | ||
| Issue Links: |
|
||||||||
| Template: |
|
||||||||
| Acceptance criteria: |
Empty
|
||||||||
| Task DoD: |
[ ]*
Doc/release notes changes? Comment present?
[ ]*
Downstream builds green?
[ ]*
Solution information and context easily available?
[ ]*
Tests
[ ]*
FixVersion filled and not yet released
[ ] 
Architecture Decision Record (ADR)
|
||||||||
| Documentation update required: |
Yes
|
||||||||
| Date of First Response: | |||||||||
| Epic Link: | Declarative REST clients | ||||||||
| Sprint: | Declarative REST 12 | ||||||||
| Story Points: | 5 | ||||||||
| Description |
|
User Story: As a template developer I want to work with content from restfn in the same way that I work with content from the other magnolia templating libaries (cmsfn, catfn...), so that I only have to learn things once and can apply that knowledge elsewhere and therefore be more productive. Acceptance Criteria:
Context and Problem description In order for RestClients to be used in Apps, Link fields and Findbar resolvers, they must use the JsonNode entityClass. However it is hard to work with JsonNode in FTL.
// With JsonNode [#assign title = item.get("fields").get("title").textValue()] [#assign authors = item.get("fields").get("authors").get(0).textValue()] [#assign story = item.get("fields").get("story").get("content").get(0).get("content").get(0).get("value").textValue()] An alternative is to set entityClass to java.util.HashMap on the restClient call. Then cmsfn.dump() gets content as expected.
// With HashMap
[#assign title = item.fields.title]
[#assign authors = item.fields.authors[0]]
[#assign story = item.fields.story.content[0].content[0].value]
However, if you use the HashMap entity class, then that restClient call is incompatible with the app, link field, findbar resolver etc.
Note: Some possible solutions
Another workaround is to then always to create 2 calls on the restClient, one for the FTL and one for the other things. But that is unpleasant and harder to understand for a developer, even with the fancy YAML shortcuts. |
| Comments |
| Comment by Mikaël Geljić [ 12/Nov/19 ] |
|
mdrapela |
| Comment by Quach Hao Thien [ 06/Dec/19 ] |
|
hi czimmermann, could you please provide the restClient was used in this ticket? I need it for testing |
| Comment by Christopher Zimmermann [ 09/Dec/19 ] |
|
Sure. So here is the FTL I have that is using it: And here is the REST Client: (You can see I had two different calls configured using the different entityClass') |
| Comment by Quach Hao Thien [ 11/Dec/19 ] |
|
Hi czimmermann, as I observed, the problem of cannot access jsonNode by children elements in your example
item.get("fields").title
Do you think it solve your problem? |
| Comment by Christopher Zimmermann [ 11/Dec/19 ] |
|
thien.quach THanks, I did not know that. That only slightly helps though, and therefore it does not solve the problem. Developers should be able to use the same syntax that they were familar with from maps. (Examples in "Context and Problem description" above.) |
| Comment by Quach Hao Thien [ 12/Dec/19 ] |
|
yes, I'll provide one method to convert JsonNode to Map, and you can use it by
[#assign itemMap = restfn.asMap(items)/]
${itemMap.items[0].fields.title}
=> Kayaking is Kool
and then dump it as well
${cmsfn.dump(itemMap, 5, true)}
=>
Hash (6)
total = 2 (Integer)
limit = 100 (Integer)
skip = 0 (Integer)
includes = Hash (1)
Asset = Sequence (2)
0 = Hash (2)
sys = Hash (8)
...
|
| Comment by Christopher Zimmermann [ 12/Dec/19 ] |
|
Looks good! |