[MGNLRESTCL-16] restfn.call() doesn't work if you need to pass multiple parameters of the same class Created: 23/Nov/15  Updated: 07/Apr/20  Resolved: 05/Mar/18

Status: Closed
Project: REST Client
Component/s: None
Affects Version/s: 1.0.6
Fix Version/s: 1.5.2

Type: Bug Priority: Major
Reporter: Roman Kovařík Assignee: Saimir Gasa
Resolution: Fixed Votes: 1
Labels: quickwin
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

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)
Bug DoR:
[ ]* Steps to reproduce, expected, and actual results filled
[ ]* Affected version filled
Documentation update required:
Yes
Date of First Response:
Sprint: Saigon 135, Saigon 136
Story Points: 3

 Description   

info.magnolia.resteasy.client.functions.RestTemplatingFunctions#getParameterTypes uses set instead of a collection so the same classes override each other.



 Comments   
Comment by Ngoc Nguyenthanh [ 28/Feb/18 ]
Steps to reproduce
  • Register a rest client in the rest module
    'rest-client': 
      jcr:primaryType: 'mgnl:content'
      'icndbClient': 
        'baseUrl': 'http://api.icndb.com'
        'class': 'info.magnolia.resteasy.client.RestEasyClientDefinition'
        'clientFactoryClass': 'info.magnolia.resteasy.client.factory.RestEasyClientFactory'
        'components': 
    
  • Create a Java class for the service
    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.PathParam;
    import javax.ws.rs.Produces;
    import javax.ws.rs.QueryParam;
    import javax.ws.rs.core.MediaType;
    
    import org.codehaus.jackson.JsonNode;
    
    public interface IcndbService {
        @GET
        @Path("/jokes/{select}")
        @Produces(MediaType.APPLICATION_JSON)
        JsonNode joke(@PathParam("select") String select, @QueryParam("firstName") String firstName, @QueryParam("lastName") String lastName);
    }
    
  • Open "Dev -> Rest Client app" test your service method to make sure it works.
  • Create a page template
    templateScript: /hello-magnolia/templates/pages/hello.ftl
    renderType: freemarker
    visible: true
    title: Hello template
    
  • Put the restfn.call in your Freemarker script
    <html>
    <body>
    [#assign jokesService = restfn.getService("icndbClient", "info.magnolia.dev.rest.IcndbService")]
    [#assign response = restfn.call(jokesService, "joke", "random", "Tiger", "Lilly") /]
    <i>${response.get("value").get("joke").getTextValue()!"Nix found"}</i>
    </body>
    </html>
    
  • Create a page with your template
Observed result
  • Page can't rendered
  • Log throw
    2018-02-28 14:02:34,044 ERROR ndering.engine.ModeDependentRenderExceptionHandler: Error while rendering [/hello] with template [hello-magnolia:pages/hello] for URI [/hello.html?mgnlPreview=false&mgnlChannel=desktop]:
    RenderException: freemarker.core._TemplateModelException: Java method "info.magnolia.resteasy.client.functions.RestTemplatingFunctions.call(Object, String, Object...)" threw an exception when invoked on info.magnolia.resteasy.client.functions.RestTemplatingFunctions object "info.magnolia.resteasy.client.functions.RestTemplatingFunctions@7e7d67fc"; see cause exception in the Java stack trace.
    
    ----
    FTL stack trace ("~" means nesting-related):
    	- Failed at: #assign response = restfn.call(jokesS...  [in template "hello-magnolia/templates/pages/hello.ftl" at line 13, column 1]
    ----
    info.magnolia.rendering.engine.RenderException: freemarker.core._TemplateModelException: Java method "info.magnolia.resteasy.client.functions.RestTemplatingFunctions.call(Object, String, Object...)" threw an exception when invoked on info.magnolia.resteasy.client.functions.RestTemplatingFunctions object "info.magnolia.resteasy.client.functions.RestTemplatingFunctions@7e7d67fc"; see cause exception in the Java stack trace.
    
    ----
    FTL stack trace ("~" means nesting-related):
    	- Failed at: #assign response = restfn.call(jokesS...  [in template "hello-magnolia/templates/pages/hello.ftl" at line 13, column 1]
    ----
    	at info.magnolia.rendering.renderer.FreemarkerRenderer.onRender(FreemarkerRenderer.java:101) ~[magnolia-rendering-5.6.3-SNAPSHOT.jar:?]
    	at info.magnolia.rendering.renderer.AbstractRenderer.render(AbstractRenderer.java:166) ~[magnolia-rendering-5.6.3-SNAPSHOT.jar:?]
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_92]
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_92]
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_92]
    	at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_92]
    
Expected result
  • Page can rendered successfully with result from the rest service call.
Root cause
  • RestTemplatingFunctions#getParameterTypes uses a HashSet with class as its key.
  • Multiple parameters with a same class will cause of the issue due to only a parameter is available.
Comment by Ngoc Nguyenthanh [ 28/Feb/18 ]

Documentation update is required:

Generated at Mon Feb 12 10:42:22 CET 2024 using Jira 9.4.2#940002-sha1:46d1a51de284217efdcb32434eab47a99af2938b.