fix(接口定义): 修复 swagger 导出时,请求体为 array 导出失败的情况
This commit is contained in:
parent
ee0a41fa02
commit
d929cc484a
|
@ -498,6 +498,14 @@ public class Swagger3Parser extends SwaggerAbstractParser {
|
||||||
swaggerParam.setDescription((String) param.get("description"));
|
swaggerParam.setDescription((String) param.get("description"));
|
||||||
swaggerParam.setName((String) param.get("name"));
|
swaggerParam.setName((String) param.get("name"));
|
||||||
swaggerParam.setRequired((boolean) param.get("required"));
|
swaggerParam.setRequired((boolean) param.get("required"));
|
||||||
|
// 请求头 value 没有导出
|
||||||
|
// JSONObject schema = new JSONObject();
|
||||||
|
// swaggerParam.setSchema(schema);
|
||||||
|
// if(type.equals("headers")) {
|
||||||
|
// schema.put("type", "string");
|
||||||
|
// schema.put("example", param.getString("value"));
|
||||||
|
// swaggerParam.setSchema(schema);
|
||||||
|
// }
|
||||||
paramsList.add(JSON.parseObject(JSON.toJSONString(swaggerParam)));
|
paramsList.add(JSON.parseObject(JSON.toJSONString(swaggerParam)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -522,6 +530,55 @@ public class Swagger3Parser extends SwaggerAbstractParser {
|
||||||
return schema;
|
return schema;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 请求体是 array 类型的情况
|
||||||
|
/* 例子:"schema":{
|
||||||
|
"type":"array",
|
||||||
|
"items":{
|
||||||
|
"type":"object",
|
||||||
|
"properties":{
|
||||||
|
"ids":{
|
||||||
|
"type":"string"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} */
|
||||||
|
private JSONObject buildRequestBodyJsonInfo(JSONArray requestBody) {
|
||||||
|
if(requestBody == null)
|
||||||
|
return null;
|
||||||
|
JSONObject schema = new JSONObject();
|
||||||
|
schema.put("type", "array");
|
||||||
|
JSONObject items = new JSONObject();
|
||||||
|
|
||||||
|
if(requestBody.size() > 0) {
|
||||||
|
Object example = requestBody.get(0);
|
||||||
|
if(example instanceof JSONObject) {
|
||||||
|
items.put("type", "object");
|
||||||
|
items.put("properties", buildSchema((JSONObject) example));
|
||||||
|
} else if(example instanceof java.lang.String) {
|
||||||
|
items.put("type", "string");
|
||||||
|
} else if(example instanceof java.lang.Integer) {
|
||||||
|
items.put("type", "integer");
|
||||||
|
items.put("format", "int64");
|
||||||
|
} else if(example instanceof java.lang.Boolean) {
|
||||||
|
items.put("type", "boolean");
|
||||||
|
} else if(example instanceof java.math.BigDecimal) {
|
||||||
|
items.put("type", "double");
|
||||||
|
}
|
||||||
|
else { // JSONOArray
|
||||||
|
items.put("type", "array");
|
||||||
|
JSONObject item = new JSONObject();
|
||||||
|
if(((JSONArray) example).size() > 0) {
|
||||||
|
if(((JSONArray) example).get(0) instanceof JSONObject) {
|
||||||
|
item = buildRequestBodyJsonInfo((JSONObject) ((JSONArray) example).get(0));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
items.put("items", item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
schema.put("items", items);
|
||||||
|
return schema;
|
||||||
|
}
|
||||||
|
|
||||||
// 设置一个 json 对象的属性在 swagger 格式中的类型、值
|
// 设置一个 json 对象的属性在 swagger 格式中的类型、值
|
||||||
private JSONObject buildSchema(JSONObject requestBody) {
|
private JSONObject buildSchema(JSONObject requestBody) {
|
||||||
JSONObject schema = new JSONObject();
|
JSONObject schema = new JSONObject();
|
||||||
|
@ -650,16 +707,25 @@ public class Swagger3Parser extends SwaggerAbstractParser {
|
||||||
put("Form Data", org.springframework.http.MediaType.MULTIPART_FORM_DATA_VALUE);
|
put("Form Data", org.springframework.http.MediaType.MULTIPART_FORM_DATA_VALUE);
|
||||||
put("WWW_FORM", org.springframework.http.MediaType.APPLICATION_FORM_URLENCODED_VALUE);
|
put("WWW_FORM", org.springframework.http.MediaType.APPLICATION_FORM_URLENCODED_VALUE);
|
||||||
}};
|
}};
|
||||||
JSONObject bodyInfo = new JSONObject();
|
Object bodyInfo = new Object();
|
||||||
JSONObject body = respOrReq.getJSONObject("body");
|
JSONObject body = respOrReq.getJSONObject("body");
|
||||||
|
|
||||||
try {
|
|
||||||
if(body != null) { // 将请求体转换成相应的格式导出
|
if(body != null) { // 将请求体转换成相应的格式导出
|
||||||
String bodyType = body.getString("type");
|
String bodyType = body.getString("type");
|
||||||
if(bodyType == null) {
|
if(bodyType == null) {
|
||||||
|
|
||||||
}else if(bodyType.equals("JSON")) {
|
}else if(bodyType.equals("JSON")) {
|
||||||
|
try{ // 若请求体是一个 object
|
||||||
|
bodyInfo = buildRequestBodyJsonInfo(body.getJSONArray("raw"));
|
||||||
|
} catch (Exception e) {
|
||||||
|
try { // 若请求体是一个 array
|
||||||
bodyInfo = buildRequestBodyJsonInfo(body.getJSONObject("raw"));
|
bodyInfo = buildRequestBodyJsonInfo(body.getJSONObject("raw"));
|
||||||
|
} catch (Exception e1) { // 若请求体 json 不合法,则忽略错误,原样字符串导出/导入
|
||||||
|
bodyInfo = new JSONObject();
|
||||||
|
((JSONObject) bodyInfo).put("type", "string");
|
||||||
|
((JSONObject) bodyInfo).put("example", body.get("raw").toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
} else if(bodyType.equals("XML")) {
|
} else if(bodyType.equals("XML")) {
|
||||||
String xmlText = body.getString("raw");
|
String xmlText = body.getString("raw");
|
||||||
JSONObject xmlToJson = XMLUtils.XmlToJson(xmlText);
|
JSONObject xmlToJson = XMLUtils.XmlToJson(xmlText);
|
||||||
|
@ -669,18 +735,13 @@ public class Swagger3Parser extends SwaggerAbstractParser {
|
||||||
bodyInfo = buildformDataSchema(formData);
|
bodyInfo = buildformDataSchema(formData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
|
||||||
LogUtil.error(e.getMessage(), e);
|
|
||||||
}
|
|
||||||
|
|
||||||
String type = respOrReq.getJSONObject("body").getString("type");
|
String type = respOrReq.getJSONObject("body").getString("type");
|
||||||
JSONObject content = new JSONObject();
|
JSONObject content = new JSONObject();
|
||||||
JSONObject schema = bodyInfo; // 请求体部分
|
Object schema = bodyInfo; // 请求体部分
|
||||||
JSONObject typeName = new JSONObject();
|
JSONObject typeName = new JSONObject();
|
||||||
if (schema != null) {
|
if (schema != null) {
|
||||||
schema.put("type", null);
|
typeName.put("schema", schema);//schema.getJSONObject("properties").size() == 0? "" :
|
||||||
schema.put("format", null);
|
|
||||||
typeName.put("schema", schema);
|
|
||||||
}
|
}
|
||||||
if (type != null && StringUtils.isNotBlank(type)) {
|
if (type != null && StringUtils.isNotBlank(type)) {
|
||||||
content.put(typeMap.get(type), typeName);
|
content.put(typeMap.get(type), typeName);
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package io.metersphere.api.dto.definition.parse.swagger;
|
package io.metersphere.api.dto.definition.parse.swagger;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import lombok.*;
|
import lombok.*;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
|
@ -9,4 +10,5 @@ public class SwaggerParams {
|
||||||
private String in; // 参数类型,可选值为 path,header,query 等
|
private String in; // 参数类型,可选值为 path,header,query 等
|
||||||
private String description;
|
private String description;
|
||||||
private boolean required; // 是否是必填参数
|
private boolean required; // 是否是必填参数
|
||||||
|
private JSONObject schema;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue