refactor: 处理swagger响应体
This commit is contained in:
parent
57c720938d
commit
ec4ff14bf6
|
@ -7,6 +7,7 @@ import io.metersphere.api.dto.ApiTestImportRequest;
|
||||||
import io.metersphere.api.dto.definition.ApiDefinitionResult;
|
import io.metersphere.api.dto.definition.ApiDefinitionResult;
|
||||||
import io.metersphere.api.dto.definition.parse.ApiDefinitionImport;
|
import io.metersphere.api.dto.definition.parse.ApiDefinitionImport;
|
||||||
import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy;
|
import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy;
|
||||||
|
import io.metersphere.api.dto.definition.response.HttpResponse;
|
||||||
import io.metersphere.api.dto.parse.ApiImport;
|
import io.metersphere.api.dto.parse.ApiImport;
|
||||||
import io.metersphere.api.dto.scenario.Body;
|
import io.metersphere.api.dto.scenario.Body;
|
||||||
import io.metersphere.api.dto.scenario.KeyValue;
|
import io.metersphere.api.dto.scenario.KeyValue;
|
||||||
|
@ -20,7 +21,6 @@ import org.apache.commons.collections.CollectionUtils;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.sql.Connection;
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
public class Swagger2Parser extends ApiImportAbstractParser {
|
public class Swagger2Parser extends ApiImportAbstractParser {
|
||||||
|
@ -65,7 +65,7 @@ public class Swagger2Parser extends ApiImportAbstractParser {
|
||||||
apiDefinition.setRequest(JSON.toJSONString(request));
|
apiDefinition.setRequest(JSON.toJSONString(request));
|
||||||
apiDefinition.setId(request.getId());
|
apiDefinition.setId(request.getId());
|
||||||
results.add(apiDefinition);
|
results.add(apiDefinition);
|
||||||
|
parseResponse(operation.getResponses());
|
||||||
|
|
||||||
// List<String> tags = operation.getTags();
|
// List<String> tags = operation.getTags();
|
||||||
// if (tags != null) {
|
// if (tags != null) {
|
||||||
|
@ -142,7 +142,7 @@ public class Swagger2Parser extends ApiImportAbstractParser {
|
||||||
parseFormDataParameters((FormParameter) parameter, request.getBody());
|
parseFormDataParameters((FormParameter) parameter, request.getBody());
|
||||||
break;
|
break;
|
||||||
case SwaggerParameterType.BODY:
|
case SwaggerParameterType.BODY:
|
||||||
parseBodyParameters(parameter, request.getBody());
|
parseRequestBodyParameters(parameter, request.getBody());
|
||||||
break;
|
break;
|
||||||
case SwaggerParameterType.HEADER:
|
case SwaggerParameterType.HEADER:
|
||||||
parseHeaderParameters(parameter, request.getHeaders());
|
parseHeaderParameters(parameter, request.getHeaders());
|
||||||
|
@ -207,14 +207,45 @@ public class Swagger2Parser extends ApiImportAbstractParser {
|
||||||
addHeader(headers, headerParameter.getName(), "", getDefaultStringValue(headerParameter.getDescription()));
|
addHeader(headers, headerParameter.getName(), "", getDefaultStringValue(headerParameter.getDescription()));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void parseBodyParameters(Parameter parameter, Body body) {
|
private void parseResponse(Map<String, Response> responses) {
|
||||||
BodyParameter bodyParameter = (BodyParameter) parameter;
|
HttpResponse msResponse = new HttpResponse();
|
||||||
Model schema = bodyParameter.getSchema();
|
msResponse.setBody(new Body());
|
||||||
|
msResponse.setHeaders(new ArrayList<>());
|
||||||
|
msResponse.setType(RequestType.HTTP);
|
||||||
|
// todo 状态码要调整?
|
||||||
|
msResponse.setStatusCode(new ArrayList<>());
|
||||||
|
if (responses != null) {
|
||||||
|
responses.forEach((responseCode, response) -> {
|
||||||
|
msResponse.getStatusCode().add(new KeyValue(responseCode, responseCode));
|
||||||
|
parseResponseHeader(response, msResponse.getHeaders());
|
||||||
|
parseResponseBodyParameters(response, msResponse.getBody());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void parseResponseHeader(Response response, List<KeyValue> msHeaders) {
|
||||||
|
Map<String, Property> headers = response.getHeaders();
|
||||||
|
if (headers != null) {
|
||||||
|
headers.forEach((k, v) -> {
|
||||||
|
msHeaders.add(new KeyValue(k, "", v.getDescription()));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void parseResponseBodyParameters(Response response, Body body) {
|
||||||
|
body.setRaw(parseSchema(response.getResponseSchema()));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void parseRequestBodyParameters(Parameter parameter, Body body) {
|
||||||
|
BodyParameter bodyParameter = (BodyParameter) parameter;
|
||||||
|
body.setRaw(parseSchema(bodyParameter.getSchema()));
|
||||||
|
}
|
||||||
|
|
||||||
|
private String parseSchema(Model schema) {
|
||||||
// 引用模型
|
// 引用模型
|
||||||
if (schema instanceof RefModel) {
|
if (schema instanceof RefModel) {
|
||||||
String simpleRef = "";
|
String simpleRef = "";
|
||||||
RefModel refModel = (RefModel) bodyParameter.getSchema();
|
RefModel refModel = (RefModel) schema;
|
||||||
String originalRef = refModel.getOriginalRef();
|
String originalRef = refModel.getOriginalRef();
|
||||||
if (refModel.getOriginalRef().split("/").length > 3) {
|
if (refModel.getOriginalRef().split("/").length > 3) {
|
||||||
simpleRef = originalRef.replace("#/definitions/", "");
|
simpleRef = originalRef.replace("#/definitions/", "");
|
||||||
|
@ -226,11 +257,12 @@ public class Swagger2Parser extends ApiImportAbstractParser {
|
||||||
refSet.add(simpleRef);
|
refSet.add(simpleRef);
|
||||||
if (model != null) {
|
if (model != null) {
|
||||||
JSONObject bodyParameters = getBodyParameters(model.getProperties(), refSet);
|
JSONObject bodyParameters = getBodyParameters(model.getProperties(), refSet);
|
||||||
body.setRaw(bodyParameters.toJSONString());
|
//body.setRaw(bodyParameters.toJSONString());
|
||||||
|
return bodyParameters.toJSONString();
|
||||||
}
|
}
|
||||||
} else if (schema instanceof ArrayModel) {
|
} else if (schema instanceof ArrayModel) {
|
||||||
//模型数组
|
//模型数组
|
||||||
ArrayModel arrayModel = (ArrayModel) bodyParameter.getSchema();
|
ArrayModel arrayModel = (ArrayModel) schema;
|
||||||
Property items = arrayModel.getItems();
|
Property items = arrayModel.getItems();
|
||||||
if (items instanceof RefProperty) {
|
if (items instanceof RefProperty) {
|
||||||
RefProperty refProperty = (RefProperty) items;
|
RefProperty refProperty = (RefProperty) items;
|
||||||
|
@ -240,10 +272,11 @@ public class Swagger2Parser extends ApiImportAbstractParser {
|
||||||
Model model = definitions.get(simpleRef);
|
Model model = definitions.get(simpleRef);
|
||||||
JSONArray propertyList = new JSONArray();
|
JSONArray propertyList = new JSONArray();
|
||||||
propertyList.add(getBodyParameters(model.getProperties(), refSet));
|
propertyList.add(getBodyParameters(model.getProperties(), refSet));
|
||||||
body.setRaw(propertyList.toString());
|
// body.setRaw(propertyList.toString());
|
||||||
|
return propertyList.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
body.setFormat("json");
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
private JSONObject getBodyParameters(Map<String, Property> properties, HashSet<String> refSet) {
|
private JSONObject getBodyParameters(Map<String, Property> properties, HashSet<String> refSet) {
|
||||||
|
|
Loading…
Reference in New Issue