我们可以在 x-mediation-script 中使用 $ref 吗?

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

我将使用 x-mediation-script 对一些数据进行硬编码。我想在哪里使用将在 setPayloadjson 中调用的 $ref。这有可能吗?需要任何样本的建议

"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"},

    }
    }
  }

无法使用 $ref 从中介脚本访问 swagger 内容,原因是,

  1. x-mediation-script 在 JS 中,无法在代码中使用 swagger 语法。
  2. API 管理器不处理脚本。因此,在发布 API 时,只会将 x-mediation-script 内容复制到突触文件中。

作为解决方案,在x-mediation-script中创建一个JS变量并使用它。

    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.