Search code examples
wso2wso2-api-managerwso2-data-services-serverswagger-editorwso2-esb

Can we use $ref in x-mediation-script?


I'm going to hard code some data using x-mediation-script. Where as I want to use $ref which will be called in setPayloadjson. Is this possible can we do it? need suggestion with any of the sample

"x-mediation-script": "mc.setProperty('CONTENT_TYPE', 'application/json');mc.setPayloadJSON('$ref', '#/definitions/out');"

"definitions":{
  "out":{
    "type" : "object",
    "required": ["NAME"],
    "properties": {
      "NAME2": {"type": "string"},
      "NAME3": {"type": "string"},
      "NAME3": {"type": "string"},
      "NAME4": {"type": "string"},

    }
    }
  }

Solution

  • It is not possible to access swagger content from the mediation script using $ref due to,

    1. x-mediation-script is in JS and could not use swagger syntax in code.
    2. API Manager does not process the script. Therefore, when publishing the API, only the x-mediation-script content is copied to the synapse file.

    As the solution, create a JS variable in the x-mediation-script and use it.

        mc.setProperty('CONTENT_TYPE', 'application/json'); // Set the content type of the payload to the message context
        var town = mc.getProperty('uri.var.town');          // Get the path parameter 'town' and store in a variable
        mc.setPayloadJSON('{ "Town" : "'+town+'"}');        // Set the new payload to the message context.