Uploaded image for project: 'REST Client'
  1. REST Client
  2. MGNLRESTCL-86

Hard to get content from JsonNode object in FTL

XMLWordPrintable

    • Icon: Improvement Improvement
    • Resolution: Fixed
    • Icon: Major Major
    • 2.0
    • None
    • None
    • None
    • 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 

      1. Templating function to convert JsonNodes into Maps, e.g. restfn.asContentMap
      2. 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?
      3. Create new datasource that can handle maps. (Maybe mapDatasource?) (Introduce Map propertySet that vaadin could understand.)
      4. Use JsonObject instead of JsonNode. See MGNLUI-5316; not actively considered for now (contribute to JsonPath or fork the library)
      5. Add a parameter to restfn.call to get the content as a map.
      6. 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.

        Acceptance criteria

              thien.quach Quach Hao Thien
              czimmermann Christopher Zimmermann
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

                Created:
                Updated:
                Resolved:

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0d
                  0d
                  Logged:
                  Time Spent - 3.75d
                  3.75d