From 4dcd789ac51362a267812f52e5edd2b780172124 Mon Sep 17 00:00:00 2001 From: chenjianxing Date: Tue, 7 Sep 2021 11:42:08 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20--bug=3D1003650=20--user=3D=E9=99=88?= =?UTF-8?q?=E5=BB=BA=E6=98=9F=20=E3=80=90github#3059=E3=80=91=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3api=E5=AF=BC=E5=87=BA=E6=88=90swagger=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E6=97=B6=E5=80=99=EF=BC=8Cjson-schema=E4=B8=AD?= =?UTF-8?q?=E5=A1=AB=E5=86=99=E7=9A=84=E5=BF=85=E5=A1=AB=E5=92=8C=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=E6=8F=8F=E8=BF=B0=E4=B8=8D=E8=83=BD=E8=A2=AB=E5=AF=BC?= =?UTF-8?q?=E5=87=BA=E6=9D=A5=20https://www.tapd.cn/55049933/s/1044291?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dto/definition/parse/Swagger3Parser.java | 85 +++++++++++++++++-- 1 file changed, 80 insertions(+), 5 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/parse/Swagger3Parser.java b/backend/src/main/java/io/metersphere/api/dto/definition/parse/Swagger3Parser.java index de782409bc..2ccdd2b503 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/parse/Swagger3Parser.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/parse/Swagger3Parser.java @@ -592,6 +592,70 @@ public class Swagger3Parser extends SwaggerAbstractParser { return schema; } + private JSONObject buildJsonSchema(JSONObject requestBody, JSONArray required) { + String type = requestBody.getString("type"); + + JSONObject parsedParam = new JSONObject(); + if (StringUtils.isNotBlank(type)) { + if (StringUtils.equals(type, "array")) { + JSONObject items = requestBody.getJSONObject("items"); + parsedParam.put("type", "array"); + JSONObject item = buildJsonSchema(items, required); + if (StringUtils.isNotBlank(requestBody.getString("description"))) { + parsedParam.put("description", requestBody.getString("description")); + } + parsedParam.put("items", item); + } else if(StringUtils.equals(type, "object")) { + parsedParam.put("type", "object"); + JSONObject properties = requestBody.getJSONObject("properties"); + JSONObject swaggerProperties = new JSONObject(); + properties.keySet().forEach((k) -> { + JSONObject item = buildJsonSchema(properties.getJSONObject(k), required); + if (required != null && required.contains(k)) { + item.put("required", true); + } + swaggerProperties.put(k, item); + }); + if (StringUtils.isNotBlank(requestBody.getString("description"))) { + parsedParam.put("description", requestBody.getString("description")); + } + parsedParam.put("properties", swaggerProperties); + } else if(StringUtils.equals(type, "integer")) { + parsedParam.put("type", "integer"); + parsedParam.put("format", "int64"); + setCommonJsonSchemaParam(parsedParam, requestBody); + } else if(StringUtils.equals(type, "boolean")) { + parsedParam.put("type", "boolean"); + setCommonJsonSchemaParam(parsedParam, requestBody); + } else if(StringUtils.equals(type, "number")) { // double 类型会被 fastJson 转换为 BigDecimal + parsedParam.put("type", "double"); + setCommonJsonSchemaParam(parsedParam, requestBody); + } else { + parsedParam.put("type", "string"); + setCommonJsonSchemaParam(parsedParam, requestBody); + } + } + return parsedParam; + } + public void setCommonJsonSchemaParam(JSONObject parsedParam, JSONObject requestBody) { + if (StringUtils.isNotBlank(requestBody.getString("description"))) { + parsedParam.put("description", requestBody.getString("description")); + } + Object jsonSchemaValue = getJsonSchemaValue(requestBody); + if (jsonSchemaValue != null) { + parsedParam.put("example", jsonSchemaValue); + } + } + + public Object getJsonSchemaValue(JSONObject item) { + JSONObject mock = item.getJSONObject("mock"); + if (mock != null) { + Object value = mock.get("mock"); + return value; + } + return null; + } + // 设置一个 json 对象的属性在 swagger 格式中的类型、值 private JSONObject buildSchema(JSONObject requestBody) { JSONObject schema = new JSONObject(); @@ -730,11 +794,22 @@ public class Swagger3Parser extends SwaggerAbstractParser { } else if(bodyType.equals("JSON")) { try { if (StringUtils.equals(body.getString("format"), "JSON-SCHEMA")) { - String jsonSchema = JSONSchemaGenerator.getJson(body.getString("jsonSchema")); - try { - bodyInfo = buildRequestBodyJsonInfo(JSONObject.parseObject(jsonSchema)); - } catch (Exception e) { - bodyInfo = buildRequestBodyJsonInfo(JSONObject.parseArray(jsonSchema)); +// String jsonSchema = JSONSchemaGenerator.getJson(body.getString("jsonSchema")); + String jsonSchema = body.getString("jsonSchema"); +// bodyInfo = buildRequestBodyJsonInfo(JSONObject.parseObject(jsonSchema)); + if (StringUtils.isNotBlank(jsonSchema)) { + JSONObject jsonObject = JSONObject.parseObject(jsonSchema); + JSONArray required = new JSONArray(); + if (jsonObject != null) { + required = jsonObject.getJSONArray("required"); + } + if (required == null) { + JSONObject items = jsonObject.getJSONObject("items"); + if (items != null) { + required = items.getJSONArray("required"); + } + } + bodyInfo = buildJsonSchema(jsonObject, required); } } else { try{ // 若请求体是一个 object