fix(接口测试): 修复接口测试导出swagger3文件响应体内容格式错误问题

--bug=1020795 --user=郭雨琦
https://www.tapd.cn/55049933/bugtrace/bugs/view/1155049933001020795
This commit is contained in:
guoyuqi 2022-12-14 16:22:26 +08:00 committed by 刘瑞斌
parent 5e1567b6b4
commit 080d01417a
1 changed files with 12 additions and 7 deletions

View File

@ -345,7 +345,7 @@ public class Swagger3Parser extends SwaggerAbstractParser {
JsonSchemaItem jsonSchemaItem = parseSchema(schema, refSet); JsonSchemaItem jsonSchemaItem = parseSchema(schema, refSet);
if (jsonSchemaItem == null) { if (jsonSchemaItem == null) {
jsonSchemaItem = new JsonSchemaItem(); jsonSchemaItem = new JsonSchemaItem();
if (StringUtils.isNotBlank(schema.getType())) { if (schema != null && StringUtils.isNotBlank(schema.getType())) {
jsonSchemaItem.setType(schema.getType()); jsonSchemaItem.setType(schema.getType());
} }
} }
@ -575,7 +575,8 @@ public class Swagger3Parser extends SwaggerAbstractParser {
} }
private Schema getSchema(Schema schema) { private Schema getSchema(Schema schema) {
if (StringUtils.isBlank(schema.get$ref()) && StringUtils.isNotBlank(schema.getType()) && StringUtils.equals(schema.getType(),"string")) {
if (schema != null && StringUtils.isBlank(schema.get$ref()) && StringUtils.isNotBlank(schema.getType()) && StringUtils.equals(schema.getType(), "string")) {
ObjectSchema objectSchema = new ObjectSchema(); ObjectSchema objectSchema = new ObjectSchema();
objectSchema.setExample(schema.getExample()); objectSchema.setExample(schema.getExample());
schema = objectSchema; schema = objectSchema;
@ -702,7 +703,6 @@ public class Swagger3Parser extends SwaggerAbstractParser {
// 设置响应体 // 设置响应体
JSONObject responseObject = JSONUtil.parseObject(apiDefinition.getResponse()); JSONObject responseObject = JSONUtil.parseObject(apiDefinition.getResponse());
JSONObject jsonObject = buildResponseBody(responseObject); JSONObject jsonObject = buildResponseBody(responseObject);
swaggerApiInfo.setResponses(JSONUtil.parseObjectNode(jsonObject.toString())); swaggerApiInfo.setResponses(JSONUtil.parseObjectNode(jsonObject.toString()));
// 设置请求参数列表 // 设置请求参数列表
List<JSONObject> paramsList = buildParameters(requestObject); List<JSONObject> paramsList = buildParameters(requestObject);
@ -835,12 +835,12 @@ public class Swagger3Parser extends SwaggerAbstractParser {
parsedParam.put(PropertyConstant.ITEMS, item); parsedParam.put(PropertyConstant.ITEMS, item);
} else if (StringUtils.equals(type, PropertyConstant.OBJECT)) { } else if (StringUtils.equals(type, PropertyConstant.OBJECT)) {
parsedParam.put(PropertyConstant.TYPE, PropertyConstant.OBJECT); parsedParam.put(PropertyConstant.TYPE, PropertyConstant.OBJECT);
JSONObject properties = requestBody.optJSONObject(PropertyConstant.REQUIRED); JSONObject properties = requestBody.optJSONObject(PropertyConstant.PROPERTIES);
JSONObject jsonObject = buildFormDataSchema(properties); JSONObject jsonObject = buildFormDataSchema(properties);
if (StringUtils.isNotBlank(requestBody.optString("description"))) { if (StringUtils.isNotBlank(requestBody.optString("description"))) {
parsedParam.put("description", requestBody.optString("description")); parsedParam.put("description", requestBody.optString("description"));
} }
parsedParam.put(PropertyConstant.REQUIRED, jsonObject.optJSONObject(PropertyConstant.REQUIRED)); parsedParam.put(PropertyConstant.PROPERTIES, jsonObject.optJSONObject(PropertyConstant.PROPERTIES));
} else if (StringUtils.equals(type, PropertyConstant.INTEGER)) { } else if (StringUtils.equals(type, PropertyConstant.INTEGER)) {
parsedParam.put(PropertyConstant.TYPE, PropertyConstant.INTEGER); parsedParam.put(PropertyConstant.TYPE, PropertyConstant.INTEGER);
parsedParam.put("format", "int64"); parsedParam.put("format", "int64");
@ -944,7 +944,7 @@ public class Swagger3Parser extends SwaggerAbstractParser {
} }
properties.put(key, property); properties.put(key, property);
} }
schema.put(PropertyConstant.REQUIRED, properties); schema.put(PropertyConstant.PROPERTIES, properties);
return schema; return schema;
} }
@ -997,7 +997,12 @@ public class Swagger3Parser extends SwaggerAbstractParser {
statusCodeInfo.put("description", StringUtils.EMPTY); statusCodeInfo.put("description", StringUtils.EMPTY);
// 返回code // 返回code
JSONArray statusCode = response.optJSONArray("statusCode"); JSONArray statusCode = response.optJSONArray("statusCode");
responseBody.put(statusCode.toString(), statusCodeInfo); for (int i = 0; i < statusCode.length(); i++) {
JSONObject jsonObject = statusCode.getJSONObject(i);
jsonObject.get("name");
statusCodeInfo.put("description", jsonObject.get("value"));
responseBody.put(jsonObject.get("name").toString(), statusCodeInfo);
}
return responseBody; return responseBody;
} }