fix(接口测试): 修复swagger导入,参数值未带过来的缺陷

--bug=1040513 --user=王孝刚 【接口测试】定义-导入swagger接口-参数值未带过来 https://www.tapd.cn/55049933/s/1523939
This commit is contained in:
wxg0103 2024-05-30 17:53:20 +08:00 committed by 刘瑞斌
parent 6a96d2d826
commit 1ef21305fb
3 changed files with 91 additions and 6 deletions

View File

@ -144,7 +144,7 @@ public class Swagger3Parser<T> extends ApiImportAbstractParser<ApiDefinitionImpo
Content content = requestBody.getContent();
if (content != null) {
content.forEach((key, value) -> {
setResponseBodyData(key, value, body);
setRequestBodyData(key, value, body);
});
} else {
body.setBodyType(Body.BodyType.NONE.name());
@ -181,6 +181,31 @@ public class Swagger3Parser<T> extends ApiImportAbstractParser<ApiDefinitionImpo
body.setWwwFormBody(wwwFormBody);
}
private void parseFormBody(JsonSchemaItem item, Body body) {
FormDataBody formDataBody = new FormDataBody();
if (item == null) {
body.setFormDataBody(formDataBody);
return;
}
List<String> required = item.getRequired();
List<FormDataKV> formDataKVS = new ArrayList<>();
item.getProperties().forEach((key, value) -> {
if (value != null && !StringUtils.equals(PropertyConstant.OBJECT, value.getType())) {
FormDataKV formDataKV = new FormDataKV();
formDataKV.setKey(key);
formDataKV.setValue(String.valueOf(value.getExample()));
formDataKV.setRequired(CollectionUtils.isNotEmpty(required) && required.contains(key));
formDataKV.setDescription(value.getDescription());
formDataKV.setParamType(value.getType());
formDataKV.setMinLength(value.getMinLength());
formDataKV.setMaxLength(value.getMaxLength());
formDataKVS.add(formDataKV);
}
});
formDataBody.setFormValues(formDataKVS);
body.setFormDataBody(formDataBody);
}
private void parseResponse(ApiResponses responseBody, List<HttpResponse> response) {
if (responseBody != null) {
responseBody.forEach((key, value) -> {
@ -202,7 +227,7 @@ public class Swagger3Parser<T> extends ApiImportAbstractParser<ApiDefinitionImpo
}
if (value.getContent() != null) {
value.getContent().forEach((k, v) -> {
setResponseBodyData(k, v, body);
setRequestBodyData(k, v, body);
});
} else {
body.setBodyType(Body.BodyType.NONE.name());
@ -225,7 +250,7 @@ public class Swagger3Parser<T> extends ApiImportAbstractParser<ApiDefinitionImpo
}
private void setResponseBodyData(String k, io.swagger.v3.oas.models.media.MediaType value, ResponseBody body) {
private void setRequestBodyData(String k, io.swagger.v3.oas.models.media.MediaType value, ResponseBody body) {
//TODO body 默认如果json格式
JsonSchemaItem jsonSchemaItem = parseSchema(value.getSchema());
switch (k) {
@ -272,7 +297,7 @@ public class Swagger3Parser<T> extends ApiImportAbstractParser<ApiDefinitionImpo
}
}
private void setResponseBodyData(String k, io.swagger.v3.oas.models.media.MediaType value, Body body) {
private void setRequestBodyData(String k, io.swagger.v3.oas.models.media.MediaType value, Body body) {
//TODO body 默认如果json格式
JsonSchemaItem jsonSchemaItem = parseSchema(value.getSchema());
switch (k) {
@ -308,6 +333,7 @@ public class Swagger3Parser<T> extends ApiImportAbstractParser<ApiDefinitionImpo
if (StringUtils.isBlank(body.getBodyType())) {
body.setBodyType(Body.BodyType.FORM_DATA.name());
}
parseFormBody(jsonSchemaItem, body);
}
case MediaType.APPLICATION_OCTET_STREAM_VALUE -> {
if (StringUtils.isBlank(body.getBodyType())) {

View File

@ -1,6 +1,9 @@
package io.metersphere.api.controller;
import io.metersphere.api.constants.*;
import io.metersphere.api.constants.ApiConstants;
import io.metersphere.api.constants.ApiDefinitionDocType;
import io.metersphere.api.constants.ApiDefinitionStatus;
import io.metersphere.api.constants.ApiImportPlatform;
import io.metersphere.api.controller.result.ApiResultCode;
import io.metersphere.api.domain.*;
import io.metersphere.api.dto.ApiFile;
@ -1258,7 +1261,7 @@ public class ApiDefinitionControllerTests extends BaseTest {
Assertions.assertEquals("admin", apiDefinitionInfo.getDeleteUser());
Assertions.assertNotNull(apiDefinitionInfo.getDeleteTime());
this.requestGetWithOk(SINGLE_DELETE_TO_GC+"/"+apiDefinition.getId());
this.requestGetWithOk(SINGLE_DELETE_TO_GC + "/" + apiDefinition.getId());
// @存在多个版本
String id = "1004";
@ -1603,6 +1606,16 @@ public class ApiDefinitionControllerTests extends BaseTest {
paramMap.add("request", JSON.toJSONString(request));
this.requestMultipartWithOkAndReturn(IMPORT, paramMap);
paramMap.clear();
inputStream = new FileInputStream(new File(
this.getClass().getClassLoader().getResource("file/openapi4.json")
.getPath()));
file = new MockMultipartFile("file", "openapi4.json", MediaType.APPLICATION_OCTET_STREAM_VALUE, inputStream);
paramMap.add("file", file);
request.setCoverModule(false);
request.setCoverData(false);
paramMap.add("request", JSON.toJSONString(request));
this.requestMultipartWithOkAndReturn(IMPORT, paramMap);
paramMap.clear();
paramMap.add("file", file);
request.setCoverModule(false);

View File

@ -0,0 +1,46 @@
{
"openapi": "3.0.1",
"info": {
"version": "1.0",
"title": "ms-chengtest",
"description": "",
"termsOfService": ""
},
"externalDocs": {},
"servers": [],
"tags": [],
"paths": {
"/swagger3": {
"post": {
"summary": "swagger3.0参数值",
"responses": {},
"parameters": [],
"requestBody": {
"content": {
"multipart/form-data": {
"schema": {
"type": "object",
"properties": {
"a": {
"type": "string",
"description": "",
"example": "b"
},
"test1": {
"type": "string",
"description": "",
"example": "1"
}
}
}
}
}
},
"tags": [
"未规划接口"
]
}
}
},
"components": {}
}