refactor(接口测试): 优化接口定义导入导出

This commit is contained in:
WangXu10 2024-07-26 13:51:05 +08:00 committed by Craftsman
parent 85f06fb2a5
commit 467be9b526
2 changed files with 9 additions and 8 deletions

View File

@ -219,9 +219,7 @@ public class Swagger3ExportParser implements ExportParser<ApiExportResponse> {
((JSONObject) bodyInfo).put("example", body.optJSONObject("rawBody").optString("value"));
}
} else if (bodyType != null && bodyType.equalsIgnoreCase(Body.BodyType.XML.name())) {
String xmlText = body.optString("xmlBody");
JSONObject xmlObject = JSONUtil.parseObject(xmlText);
xmlText = xmlObject.optString("value");
String xmlText = body.optJSONObject("xmlBody").optString("value");
String xml = XMLUtil.delXmlHeader(xmlText);
int startIndex = xml.indexOf("<", 0);
int endIndex = xml.indexOf(">", 0);
@ -235,6 +233,7 @@ public class Swagger3ExportParser implements ExportParser<ApiExportResponse> {
schemas = new LinkedList<>();
}
schemas.add(jsonObject);
jsonInfo = xml;
} else if (bodyType != null && bodyType.equalsIgnoreCase(Body.BodyType.WWW_FORM.name())) {
String wwwFormBody = body.optString("wwwFormBody");
JSONObject wwwFormObject = JSONUtil.parseObject(wwwFormBody);

View File

@ -290,8 +290,7 @@ public class Swagger3Parser extends ApiImportAbstractParser<ApiDefinitionImport>
case MediaType.APPLICATION_XML_VALUE -> {
body.setBodyType(Body.BodyType.XML.name());
XmlBody xml = new XmlBody();
//xml.setValue(XMLUtils.jsonToXmlStr(jsonValue));
String xmlBody = parseXmlBody(value.getSchema(), jsonSchemaItem);
String xmlBody = parseXmlBody(value, jsonSchemaItem);
xml.setValue(xmlBody);
body.setXmlBody(xml);
}
@ -335,8 +334,7 @@ public class Swagger3Parser extends ApiImportAbstractParser<ApiDefinitionImport>
case MediaType.APPLICATION_XML_VALUE -> {
body.setBodyType(Body.BodyType.XML.name());
XmlBody xml = new XmlBody();
//xml.setValue(XMLUtils.jsonToXmlStr(jsonValue));
String xmlBody = parseXmlBody(value.getSchema(), jsonSchemaItem);
String xmlBody = parseXmlBody(value, jsonSchemaItem);
xml.setValue(xmlBody);
body.setXmlBody(xml);
}
@ -361,8 +359,12 @@ public class Swagger3Parser extends ApiImportAbstractParser<ApiDefinitionImport>
}
}
private String parseXmlBody(Schema schema, JsonSchemaItem jsonSchemaItem) {
private String parseXmlBody(io.swagger.v3.oas.models.media.MediaType value, JsonSchemaItem jsonSchemaItem) {
Schema schema = value.getSchema();
JSONObject object = new JSONObject();
if (value.getExample() != null) {
return value.getExample().toString();
}
if (jsonSchemaItem != null && MapUtils.isNotEmpty(jsonSchemaItem.getProperties())) {
if (StringUtils.isNotBlank(schema.get$ref()) && schema.get$ref().split("/").length > 3) {