Layer7 API Management

  • 1.  How to expose an api with swagger and adjust the basePath?

    Posted Jan 29, 2018 10:29 AM

    I have some apis that I want to expose in the gateway, using the gateway as a proxy. The API has a Swagger file and I tried to use the "Publish Swagger Service" but that did not work since the base path on the swagger doc is just '/' but in the proxy should be something like '/apiname'.
    I thinked in replace the basePath attribute in the swagger.json
    I tried something like that:

     

    But I am getting this error:
    2018-01-29T13:04:07.919-0200 WARNING 61950 com.l7tech.external.assertions.evaluatejsonpathexpression.server.ServerEvaluateJsonPathExpressionAssertion: 9648: Error occurred evaluating JSON Path: 'Invalid container object'

    I already test the swagger against some json validators and it is in correct format. In the test tab it works too.

     

    Is there a better way to do this? There is any other way to change a variable in a json response?



  • 2.  Re: How to expose an api with swagger and adjust the basePath?

    Posted Jan 30, 2018 01:26 PM

    Doing some more tests the error change and seems that i can't process the json because of some 'trailing whitespaces'. I am getting errors like:

    com.l7tech.external.assertions.evaluatejsonpathexpressionv2.JsonPathEvaluator: JSON payload rejected because it contains trailing

    com.l7tech.external.assertions.evaluatejsonpathexpressionv2.server.ServerEvaluateJsonPathExpressionV2Assertion: 9648: Error occurred evaluating JSON Path: 'Expected EOF but found trailing

    Is there a way to work arround this problem?



  • 3.  Re: How to expose an api with swagger and adjust the basePath?
    Best Answer

    Broadcom Employee
    Posted Feb 08, 2018 06:17 PM

    Dear Eduardo Oliveira ,

    Not sure how you do it, but can you download the swagger file, modify as you wish, publish a new web api with resolution path like /yourswagger.json on your gateway to return  the modified swagger file in return template response assertion.

    When publish swagger file, refer to your gateway host and /yourswagger.json end point.

     

    See if that works for you.

     

    Regards,

    Mark



  • 4.  Re: How to expose an api with swagger and adjust the basePath?

    Posted Feb 20, 2018 01:58 PM

    Thanks for the help but this will not be very practical.

    This would work, but I don't want to modify every swagger of every api that I publish on the gateway. And in almost every api modification i will have to modify my swagger file again.
    I was able to resolve the problem by creating a custom assertion that basically do a replace in a string using a regex.

     

        @Override     public CustomAssertionStatus checkRequest(final CustomPolicyContext policyContext) {         logger.log(Level.FINE, "StringReplaceServiceInvocation.checkRequest(...)");         try{               if (!(customAssertion instanceof StringReplaceCustomAssertion)) {                 logger.log(Level.SEVERE,                         String.format("customAssertion must be of type [{%s}], but it is of type [{%s}] instead",                                 StringReplaceCustomAssertion.class.getSimpleName(),                                 customAssertion.getClass().getSimpleName()));                 return CustomAssertionStatus.FAILED;             }             String input=(String)policyContext.getVariable("input");             String pattern=(String)policyContext.getVariable("pattern");             String replace=(String)policyContext.getVariable("replace");             String output=input.replaceAll(pattern,replace);             policyContext.setVariable("output", output);         }catch(Exception ex){             StringWriter sw = new StringWriter();             ex.printStackTrace(new PrintWriter(sw));             logger.log(Level.SEVERE,"Error in assertion StringReplace:  " + sw.toString());             return CustomAssertionStatus.FAILED;         }         return CustomAssertionStatus.NONE;     }