-
Improvement
-
Resolution: Fixed
-
Major
-
None
-
None
-
None
-
-
Empty show more show less
-
Yes
-
Declarative REST 12
-
5
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:
- Get content from the rest client in a format similar to cmsfn functions.
- Easy to access, simple syntax
- cmsfn.dump() function outputs useful, expected content.
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.
- If you use cmsfn.dump(), it just outputs: "ObjectNode (#1500182346)", instead of a helpful map of all contained content.
- To use it in freemarker, you need to use a bunch of .getItems() calls, which is unexpected and different from the 'normal' Magnolia context and content objects.
// 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
- Templating function to convert JsonNodes into Maps, e.g. restfn.asContentMap
- and/or templating function taking a JsonPath expression as input to resolve sub-objects, e.g. restfn.read equivalent of JsonPath.read
- can't JsonPath#read be used directly in FTLs?
- also how hard is it to dump the raw Json content from FTL straight, without conversion? Do we need a restfn.dump at all?
Create new datasource that can handle maps. (Maybe mapDatasource?) (Introduce Map propertySet that vaadin could understand.)- Use JsonObject instead of JsonNode. See MGNLUI-5316; not actively considered for now (contribute to JsonPath or fork the library)
- Add a parameter to restfn.call to get the content as a map.
- Add a new function to restfn, something like restfn.callMap(), restfn.callAndGetMap()
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.
- is causing
-
MGNLRESTCL-118 DOC: Converting JsonNode to Map in FreeMarker
- Closed