diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/parse/Swagger2Parser.java b/backend/src/main/java/io/metersphere/api/dto/definition/parse/Swagger2Parser.java index a830b1d656..8655751191 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/parse/Swagger2Parser.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/parse/Swagger2Parser.java @@ -149,6 +149,14 @@ public class Swagger2Parser extends SwaggerAbstractParser { return getBodyType(contentType); } + private String getResponseBodyType(Operation operation) { + if (CollectionUtils.isEmpty(operation.getProduces())) { + return Body.JSON; + } + String contentType = operation.getProduces().get(0); + return getBodyType(contentType); + } + private void parsePathParameters(Parameter parameter, List rests) { PathParameter pathParameter = (PathParameter) parameter; rests.add(new KeyValue(pathParameter.getName(), "", getDefaultStringValue(parameter.getDescription()))); @@ -175,14 +183,17 @@ public class Swagger2Parser extends SwaggerAbstractParser { msResponse.getBody().setKvs(new ArrayList<>()); msResponse.setHeaders(new ArrayList<>()); msResponse.setType(RequestType.HTTP); - msResponse.getBody().setType(getBodyType(operation)); + msResponse.getBody().setType(getResponseBodyType(operation)); // todo 状态码要调整? msResponse.setStatusCode(new ArrayList<>()); - if (responses != null) { + if (responses != null && responses.size() > 0) { responses.forEach((responseCode, response) -> { msResponse.getStatusCode().add(new KeyValue(responseCode, responseCode)); + String body = parseSchema(response.getResponseSchema()); + if (StringUtils.isNotBlank(body)) { + msResponse.getBody().setRaw(body); + } parseResponseHeader(response, msResponse.getHeaders()); - parseResponseBodyParameters(response, msResponse.getBody()); }); } return msResponse; @@ -197,10 +208,6 @@ public class Swagger2Parser extends SwaggerAbstractParser { } } - 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()));