fix(接口测试): 修复导入导出swagger类型接口,描述信息丢失问题

--bug=1007229 --user=赵勇 [github#6835]接口导出后导入,响应内容中的描述信息没有了,同时请求参数中的值没有了 https://www.tapd.cn/55049933/s/1060958
This commit is contained in:
fit2-zhao 2021-10-28 15:13:09 +08:00 committed by fit2-zhao
parent e9664eddc0
commit db92b50838
2 changed files with 16 additions and 22 deletions

View File

@ -109,6 +109,7 @@ public class Swagger3Parser extends SwaggerAbstractParser {
if (operation != null) { if (operation != null) {
MsHTTPSamplerProxy request = buildRequest(operation, pathName, method); MsHTTPSamplerProxy request = buildRequest(operation, pathName, method);
ApiDefinitionWithBLOBs apiDefinition = buildApiDefinition(request.getId(), operation, pathName, method, importRequest); ApiDefinitionWithBLOBs apiDefinition = buildApiDefinition(request.getId(), operation, pathName, method, importRequest);
apiDefinition.setDescription(operation.getDescription());
parseParameters(operation, request); parseParameters(operation, request);
parseRequestBody(operation.getRequestBody(), request.getBody()); parseRequestBody(operation.getRequestBody(), request.getBody());
addBodyHeader(request); addBodyHeader(request);
@ -296,11 +297,10 @@ public class Swagger3Parser extends SwaggerAbstractParser {
private void parseKvBody(Schema schema, Body body, Object data, Map<String, Schema> infoMap) { private void parseKvBody(Schema schema, Body body, Object data, Map<String, Schema> infoMap) {
if (data instanceof JSONObject) { if (data instanceof JSONObject) {
((JSONObject) data).forEach((k, v) -> { ((JSONObject) data).forEach((k, v) -> {
KeyValue kv = new KeyValue(k, v.toString()); Schema dataSchema = (Schema) v;
KeyValue kv = new KeyValue(k, String.valueOf(dataSchema.getExample()), dataSchema.getDescription());
Schema schemaInfo = infoMap.get(k); Schema schemaInfo = infoMap.get(k);
if (schemaInfo != null) { if (schemaInfo != null) {
kv.setDescription(schemaInfo.getDescription());
// kv.setRequired(schemaInfo.getRequired());
if (schemaInfo instanceof BinarySchema) { if (schemaInfo instanceof BinarySchema) {
kv.setType("file"); kv.setType("file");
} }
@ -311,10 +311,10 @@ public class Swagger3Parser extends SwaggerAbstractParser {
body.getKvs().add(kv); body.getKvs().add(kv);
}); });
} else { } else {
KeyValue kv = new KeyValue(schema.getName(), data.toString(), schema.getDescription()); Schema dataSchema = (Schema) data;
KeyValue kv = new KeyValue(schema.getName(), String.valueOf(dataSchema.getExample()), schema.getDescription());
Schema schemaInfo = infoMap.get(schema.getName()); Schema schemaInfo = infoMap.get(schema.getName());
if (schemaInfo != null) { if (schemaInfo != null) {
kv.setDescription(schemaInfo.getDescription());
if (schemaInfo instanceof BinarySchema) { if (schemaInfo instanceof BinarySchema) {
kv.setType("file"); kv.setType("file");
} }
@ -370,18 +370,7 @@ public class Swagger3Parser extends SwaggerAbstractParser {
return getDefaultValueByPropertyType(schema); return getDefaultValueByPropertyType(schema);
} else { } else {
if (schema.getType() != null) { // 特判属性不是对象的情况直接将基本类型赋值进去 if (schema.getType() != null) { // 特判属性不是对象的情况直接将基本类型赋值进去
if (StringUtils.equals(schema.getType(), "string")) { return schema;
Object exampleObj = schema.getExample();
String example = null;
if (exampleObj != null) {
example = exampleObj.toString();
}
return example == null ? "" : example;
} else if (StringUtils.equals(schema.getType(), "boolean")) {
return schema.getExample();
} else if (StringUtils.equals(schema.getType(), "double")) {
return schema.getExample();
}
} }
Object propertiesResult = parseSchemaProperties(schema, refSet, infoMap); Object propertiesResult = parseSchemaProperties(schema, refSet, infoMap);
return propertiesResult == null ? getDefaultValueByPropertyType(schema) : propertiesResult; return propertiesResult == null ? getDefaultValueByPropertyType(schema) : propertiesResult;
@ -490,6 +479,7 @@ public class Swagger3Parser extends SwaggerAbstractParser {
// 设置请求参数列表 // 设置请求参数列表
List<JSONObject> paramsList = buildParameters(requestObject); List<JSONObject> paramsList = buildParameters(requestObject);
swaggerApiInfo.setParameters(paramsList); swaggerApiInfo.setParameters(paramsList);
swaggerApiInfo.setDescription(apiDefinition.getDescription());
JSONObject methodDetail = JSON.parseObject(JSON.toJSONString(swaggerApiInfo)); JSONObject methodDetail = JSON.parseObject(JSON.toJSONString(swaggerApiInfo));
if (paths.getJSONObject(apiDefinition.getPath()) == null) { if (paths.getJSONObject(apiDefinition.getPath()) == null) {
paths.put(apiDefinition.getPath(), new JSONObject()); paths.put(apiDefinition.getPath(), new JSONObject());
@ -711,8 +701,12 @@ public class Swagger3Parser extends SwaggerAbstractParser {
JSONObject properties = new JSONObject(); JSONObject properties = new JSONObject();
for (String key : kvs.keySet()) { for (String key : kvs.keySet()) {
JSONObject property = new JSONObject(); JSONObject property = new JSONObject();
property.put("type", "string"); JSONObject obj = ((JSONObject) kvs.get(key));
property.put("example", kvs.getString(key)); property.put("type", StringUtils.isNotEmpty(obj.getString("type")) ? obj.getString("type") : "string");
String value = obj.getString("value");
property.put("example", value);
property.put("description", obj.getString("description"));
property.put("required", obj.getString("required"));
properties.put(key, property); properties.put(key, property);
} }
schema.put("properties", properties); schema.put("properties", properties);
@ -724,9 +718,8 @@ public class Swagger3Parser extends SwaggerAbstractParser {
for (Object item : requestBody) { for (Object item : requestBody) {
if (item instanceof JSONObject) { if (item instanceof JSONObject) {
String name = ((JSONObject) item).getString("name"); String name = ((JSONObject) item).getString("name");
String value = ((JSONObject) item).getString("value");
if (name != null) { if (name != null) {
result.put(name, value); result.put(name, item);
} }
} }
} }
@ -755,7 +748,7 @@ public class Swagger3Parser extends SwaggerAbstractParser {
for (Object item : headValueList) { for (Object item : headValueList) {
if (item instanceof JSONObject && ((JSONObject) item).getString("name") != null) { if (item instanceof JSONObject && ((JSONObject) item).getString("name") != null) {
JSONObject head = new JSONObject(), headSchema = new JSONObject(); JSONObject head = new JSONObject(), headSchema = new JSONObject();
head.put("description", ""); head.put("description", ((JSONObject) item).getString("description"));
head.put("example", ((JSONObject) item).getString("value")); head.put("example", ((JSONObject) item).getString("value"));
headSchema.put("type", "string"); headSchema.put("type", "string");
head.put("schema", headSchema); head.put("schema", headSchema);

View File

@ -15,4 +15,5 @@ public class SwaggerApiInfo {
private List<JSONObject> parameters; // 对应 API 的请求参数 private List<JSONObject> parameters; // 对应 API 的请求参数
private JSONObject requestBody; private JSONObject requestBody;
private JSONObject responses; private JSONObject responses;
private String description;
} }