chore: 部分语法瑕疵调整

This commit is contained in:
fit2-zhao 2024-07-17 18:48:47 +08:00 committed by Craftsman
parent 87b4c9d901
commit bb8833b03c
1 changed files with 110 additions and 100 deletions

View File

@ -14,6 +14,7 @@ import io.metersphere.sdk.util.JSON;
import io.metersphere.sdk.util.Translator; import io.metersphere.sdk.util.Translator;
import io.swagger.v3.oas.models.responses.ApiResponse; import io.swagger.v3.oas.models.responses.ApiResponse;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
@ -57,7 +58,7 @@ public class Swagger3ExportParser implements ExportParser<ApiExportResponse> {
} else { } else {
moduleName = moduleMap.get(apiDefinition.getModuleId()); moduleName = moduleMap.get(apiDefinition.getModuleId());
} }
swaggerApiInfo.setTags(Arrays.asList(moduleName)); swaggerApiInfo.setTags(Collections.singletonList(moduleName));
//请求体 //请求体
JSONObject requestObject = JSONUtil.parseObject(new String(apiDefinition.getRequest() == null ? new byte[0] : apiDefinition.getRequest(), StandardCharsets.UTF_8)); JSONObject requestObject = JSONUtil.parseObject(new String(apiDefinition.getRequest() == null ? new byte[0] : apiDefinition.getRequest(), StandardCharsets.UTF_8));
JSONObject requestBody = buildRequestBody(requestObject, schemas); JSONObject requestBody = buildRequestBody(requestObject, schemas);
@ -89,7 +90,7 @@ public class Swagger3ExportParser implements ExportParser<ApiExportResponse> {
} }
response.setPaths(JSONUtil.parseObjectNode(paths.toString())); response.setPaths(JSONUtil.parseObjectNode(paths.toString()));
if (CollectionUtils.isNotEmpty(schemas)) { if (CollectionUtils.isNotEmpty(schemas)) {
components.put("schemas", schemas.get(0)); components.put("schemas", schemas.getFirst());
} }
response.setComponents(JSONUtil.parseObjectNode(components.toString())); response.setComponents(JSONUtil.parseObjectNode(components.toString()));
@ -130,7 +131,7 @@ public class Swagger3ExportParser implements ExportParser<ApiExportResponse> {
} }
private JSONObject buildResponseBody(JSONArray response, List<JSONObject> schemas) { private JSONObject buildResponseBody(JSONArray response, List<JSONObject> schemas) {
if (response.length() == 0) { if (response.isEmpty()) {
return new JSONObject(); return new JSONObject();
} }
JSONObject responseBody = new JSONObject(); JSONObject responseBody = new JSONObject();
@ -234,12 +235,12 @@ public class Swagger3ExportParser implements ExportParser<ApiExportResponse> {
} else if (bodyType != null && bodyType.equalsIgnoreCase(Body.BodyType.WWW_FORM.name())) { } else if (bodyType != null && bodyType.equalsIgnoreCase(Body.BodyType.WWW_FORM.name())) {
String wwwFormBody = body.optString("wwwFormBody"); String wwwFormBody = body.optString("wwwFormBody");
JSONObject wwwFormObject = JSONUtil.parseObject(wwwFormBody); JSONObject wwwFormObject = JSONUtil.parseObject(wwwFormBody);
JSONObject formData = getformDataProperties(wwwFormObject.optJSONArray("formValues")); JSONObject formData = getFormDataProperties(wwwFormObject.optJSONArray("formValues"));
bodyInfo = buildFormDataSchema(formData); bodyInfo = buildFormDataSchema(formData);
} else if (bodyType != null && bodyType.equalsIgnoreCase(Body.BodyType.FORM_DATA.name())) { } else if (bodyType != null && bodyType.equalsIgnoreCase(Body.BodyType.FORM_DATA.name())) {
String formDataBody = body.optString("formDataBody"); String formDataBody = body.optString("formDataBody");
JSONObject formDataObject = JSONUtil.parseObject(formDataBody); JSONObject formDataObject = JSONUtil.parseObject(formDataBody);
JSONObject formData = getformDataProperties(formDataObject.optJSONArray("formValues")); JSONObject formData = getFormDataProperties(formDataObject.optJSONArray("formValues"));
bodyInfo = buildFormDataSchema(formData); bodyInfo = buildFormDataSchema(formData);
} else if (bodyType != null && bodyType.equalsIgnoreCase(Body.BodyType.BINARY.name())) { } else if (bodyType != null && bodyType.equalsIgnoreCase(Body.BodyType.BINARY.name())) {
bodyInfo = buildBinary(); bodyInfo = buildBinary();
@ -290,7 +291,7 @@ public class Swagger3ExportParser implements ExportParser<ApiExportResponse> {
parsedParam.put(PropertyConstant.TYPE, PropertyConstant.ARRAY); parsedParam.put(PropertyConstant.TYPE, PropertyConstant.ARRAY);
if (items != null) { if (items != null) {
JSONObject itemsObject = new JSONObject(); JSONObject itemsObject = new JSONObject();
if (items.length() > 0) { if (!items.isEmpty()) {
items.forEach(item -> { items.forEach(item -> {
if (item instanceof JSONObject) { if (item instanceof JSONObject) {
JSONObject itemJson = buildJsonSchema((JSONObject) item); JSONObject itemJson = buildJsonSchema((JSONObject) item);
@ -335,11 +336,10 @@ public class Swagger3ExportParser implements ExportParser<ApiExportResponse> {
* @return * @return
*/ */
private JSONObject buildJson(String jsonValue) { private JSONObject buildJson(String jsonValue) {
JSONObject jsonObject = JSONUtil.parseObject(jsonValue); return JSONUtil.parseObject(jsonValue);
return jsonObject;
} }
private JSONObject getformDataProperties(JSONArray requestBody) { private JSONObject getFormDataProperties(JSONArray requestBody) {
JSONObject result = new JSONObject(); JSONObject result = new JSONObject();
for (Object item : requestBody) { for (Object item : requestBody) {
if (item instanceof JSONObject) { if (item instanceof JSONObject) {
@ -358,76 +358,83 @@ public class Swagger3ExportParser implements ExportParser<ApiExportResponse> {
for (String key : requestBody.keySet()) { for (String key : requestBody.keySet()) {
Object param = requestBody.get(key); Object param = requestBody.get(key);
JSONObject parsedParam = new JSONObject(); JSONObject parsedParam = new JSONObject();
if (param instanceof String) { switch (param) {
parsedParam.put(PropertyConstant.TYPE, PropertyConstant.STRING); case String ignored -> {
parsedParam.put("example", param == null ? StringUtils.EMPTY : param); parsedParam.put(PropertyConstant.TYPE, PropertyConstant.STRING);
} else if (param instanceof Integer) { parsedParam.put("example", ObjectUtils.defaultIfNull(param, StringUtils.EMPTY));
parsedParam.put(PropertyConstant.TYPE, PropertyConstant.INTEGER); }
parsedParam.put("format", "int64"); case Integer ignored -> {
parsedParam.put("example", param); parsedParam.put(PropertyConstant.TYPE, PropertyConstant.INTEGER);
} else if (param instanceof JSONObject) { parsedParam.put("format", "int64");
parsedParam.put(PropertyConstant.TYPE, PropertyConstant.OBJECT); parsedParam.put("example", param);
Object attribute = ((JSONObject) param).opt("attribute"); }
//build properties case JSONObject object -> {
JSONObject paramObject = buildRequestBodyXmlSchema((JSONObject) param); parsedParam.put(PropertyConstant.TYPE, PropertyConstant.OBJECT);
if (attribute != null && attribute instanceof JSONArray) { Object attribute = object.opt("attribute");
JSONObject jsonObject = buildXmlProperties(((JSONArray) attribute).getJSONObject(0)); //build properties
paramObject.remove("attribute"); JSONObject paramObject = buildRequestBodyXmlSchema(object);
for (String paramKey : paramObject.keySet()) { if (attribute != null && attribute instanceof JSONArray) {
Object paramChild = paramObject.get(paramKey); JSONObject jsonObject = buildXmlProperties(((JSONArray) attribute).getJSONObject(0));
if (paramChild instanceof String) { paramObject.remove("attribute");
JSONObject one = new JSONObject(); for (String paramKey : paramObject.keySet()) {
one.put(PropertyConstant.TYPE, PropertyConstant.OBJECT); Object paramChild = paramObject.get(paramKey);
one.put("properties", jsonObject); if (paramChild instanceof String) {
paramObject.remove("example"); JSONObject one = new JSONObject();
paramObject.remove(paramKey); one.put(PropertyConstant.TYPE, PropertyConstant.OBJECT);
paramObject.put(paramKey, one); one.put("properties", jsonObject);
} paramObject.remove("example");
if (paramChild instanceof JSONObject) { paramObject.remove(paramKey);
Object properties = ((JSONObject) paramChild).opt("properties"); paramObject.put(paramKey, one);
if (properties != null) {
for (String aa : jsonObject.keySet()) {
Object value = jsonObject.get(aa);
if (((JSONObject) properties).opt(aa) == null) {
((JSONObject) properties).put(aa, value);
}
}
} else {
((JSONObject) paramChild).put("properties", jsonObject);
} }
if (((JSONObject) paramChild).opt("type") == "string") { if (paramChild instanceof JSONObject) {
((JSONObject) paramChild).put("type", "object"); Object properties = ((JSONObject) paramChild).opt("properties");
((JSONObject) paramChild).remove("example"); if (properties != null) {
for (String aa : jsonObject.keySet()) {
Object value = jsonObject.get(aa);
if (((JSONObject) properties).opt(aa) == null) {
((JSONObject) properties).put(aa, value);
}
}
} else {
((JSONObject) paramChild).put("properties", jsonObject);
}
if (((JSONObject) paramChild).opt("type") == "string") {
((JSONObject) paramChild).put("type", "object");
((JSONObject) paramChild).remove("example");
}
} }
} }
} }
parsedParam.put("properties", paramObject);
if (StringUtils.isNotBlank(requestBody.optString("description"))) {
parsedParam.put("description", requestBody.optString("description"));
}
} }
parsedParam.put("properties", paramObject); case Boolean ignored -> {
if (StringUtils.isNotBlank(requestBody.optString("description"))) { parsedParam.put(PropertyConstant.TYPE, PropertyConstant.BOOLEAN);
parsedParam.put("description", requestBody.optString("description")); parsedParam.put("example", param);
} }
} else if (param instanceof Boolean) { case java.math.BigDecimal ignored -> {
parsedParam.put(PropertyConstant.TYPE, PropertyConstant.BOOLEAN); parsedParam.put(PropertyConstant.TYPE, "double");
parsedParam.put("example", param); parsedParam.put("example", param); // double 类型会被 fastJson 转换为 BigDecimal
} else if (param instanceof java.math.BigDecimal) { // double 类型会被 fastJson 转换为 BigDecimal }
parsedParam.put(PropertyConstant.TYPE, "double"); case null, default -> {
parsedParam.put("example", param); parsedParam.put(PropertyConstant.TYPE, PropertyConstant.OBJECT);
} else { // JSONOArray
parsedParam.put(PropertyConstant.TYPE, PropertyConstant.OBJECT);
if (param == null) { if (param == null) {
param = new JSONArray(); param = new JSONArray();
}
JSONObject jsonObjects = new JSONObject();
if (!((JSONArray) param).isEmpty()) {
((JSONArray) param).forEach(t -> {
JSONObject item = buildRequestBodyXmlSchema((JSONObject) t);
for (String s : item.keySet()) {
jsonObjects.put(s, item.get(s));
}
});
}
parsedParam.put(PropertyConstant.PROPERTIES, jsonObjects); // JSONOArray
} }
JSONObject jsonObjects = new JSONObject();
if (((JSONArray) param).length() > 0) {
((JSONArray) param).forEach(t -> {
JSONObject item = buildRequestBodyXmlSchema((JSONObject) t);
for (String s : item.keySet()) {
jsonObjects.put(s, item.get(s));
}
});
}
parsedParam.put(PropertyConstant.PROPERTIES, jsonObjects);
} }
schema.put(key, parsedParam); schema.put(key, parsedParam);
} }
@ -442,10 +449,9 @@ public class Swagger3ExportParser implements ExportParser<ApiExportResponse> {
Object param = kvs.opt(key); Object param = kvs.opt(key);
if (param instanceof String) { if (param instanceof String) {
property.put(PropertyConstant.TYPE, PropertyConstant.STRING); property.put(PropertyConstant.TYPE, PropertyConstant.STRING);
property.put("example", param == null ? StringUtils.EMPTY : param); property.put("example", ObjectUtils.defaultIfNull(param, StringUtils.EMPTY));
} }
if (param instanceof JSONObject) { if (param instanceof JSONObject obj) {
JSONObject obj = ((JSONObject) param);
property.put(PropertyConstant.TYPE, StringUtils.isNotEmpty(obj.optString(PropertyConstant.TYPE)) ? obj.optString(PropertyConstant.TYPE) : PropertyConstant.STRING); property.put(PropertyConstant.TYPE, StringUtils.isNotEmpty(obj.optString(PropertyConstant.TYPE)) ? obj.optString(PropertyConstant.TYPE) : PropertyConstant.STRING);
String value = obj.optString("value"); String value = obj.optString("value");
if (StringUtils.isBlank(value)) { if (StringUtils.isBlank(value)) {
@ -489,33 +495,38 @@ public class Swagger3ExportParser implements ExportParser<ApiExportResponse> {
for (String key : requestBody.keySet()) { for (String key : requestBody.keySet()) {
Object param = requestBody.get(key); Object param = requestBody.get(key);
JSONObject parsedParam = new JSONObject(); JSONObject parsedParam = new JSONObject();
if (param instanceof String) { switch (param) {
parsedParam.put(PropertyConstant.TYPE, PropertyConstant.STRING); case String ignored -> {
parsedParam.put("example", param == null ? StringUtils.EMPTY : param); parsedParam.put(PropertyConstant.TYPE, PropertyConstant.STRING);
} else if (param instanceof Integer) { parsedParam.put("example", ObjectUtils.defaultIfNull(param, StringUtils.EMPTY));
parsedParam.put(PropertyConstant.TYPE, PropertyConstant.INTEGER);
parsedParam.put("format", "int64");
parsedParam.put("example", param);
} else if (param instanceof JSONObject) {
parsedParam = buildRequestBodyJsonInfo((JSONObject) param);
} else if (param instanceof Boolean) {
parsedParam.put(PropertyConstant.TYPE, PropertyConstant.BOOLEAN);
parsedParam.put("example", param);
} else if (param instanceof java.math.BigDecimal) { // double 类型会被 fastJson 转换为 BigDecimal
parsedParam.put(PropertyConstant.TYPE, "double");
parsedParam.put("example", param);
} else { // JSONOArray
parsedParam.put(PropertyConstant.TYPE, PropertyConstant.ARRAY);
JSONObject item = new JSONObject();
if (param == null) {
param = new JSONArray();
} }
if (((JSONArray) param).length() > 0) { case Integer ignored -> {
if (((JSONArray) param).get(0) instanceof JSONObject) { /// parsedParam.put(PropertyConstant.TYPE, PropertyConstant.INTEGER);
item = buildRequestBodyJsonInfo((JSONObject) ((JSONArray) param).get(0)); parsedParam.put("format", "int64");
parsedParam.put("example", param);
}
case JSONObject jsonObject -> parsedParam = buildRequestBodyJsonInfo(jsonObject);
case Boolean ignored -> {
parsedParam.put(PropertyConstant.TYPE, PropertyConstant.BOOLEAN);
parsedParam.put("example", param);
}
case java.math.BigDecimal ignored -> {
parsedParam.put(PropertyConstant.TYPE, "double");
parsedParam.put("example", param); // double 类型会被 fastJson 转换为 BigDecimal
}
case null, default -> {
parsedParam.put(PropertyConstant.TYPE, PropertyConstant.ARRAY);
JSONObject item = new JSONObject();
if (param == null) {
param = new JSONArray();
} }
if (!((JSONArray) param).isEmpty()) {
if (((JSONArray) param).get(0) instanceof JSONObject) { ///
item = buildRequestBodyJsonInfo((JSONObject) ((JSONArray) param).get(0));
}
}
parsedParam.put(PropertyConstant.ITEMS, item); // JSONOArray
} }
parsedParam.put(PropertyConstant.ITEMS, item);
} }
schema.put(key, parsedParam); schema.put(key, parsedParam);
} }
@ -579,8 +590,7 @@ public class Swagger3ExportParser implements ExportParser<ApiExportResponse> {
JSONObject mock = item.optJSONObject(PropertyConstant.MOCK); JSONObject mock = item.optJSONObject(PropertyConstant.MOCK);
if (mock != null) { if (mock != null) {
if (StringUtils.isNotBlank(mock.optString("mock"))) { if (StringUtils.isNotBlank(mock.optString("mock"))) {
Object value = mock.get(PropertyConstant.MOCK); return mock.get(PropertyConstant.MOCK);
return value;
} }
} }
return null; return null;