From 378c31e3ca7dfab1af41aab906a605734bc8b610 Mon Sep 17 00:00:00 2001 From: chenjianxing Date: Tue, 30 Mar 2021 12:05:52 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20swagger2=20=E5=AF=BC=E5=85=A5=E5=93=8D?= =?UTF-8?q?=E5=BA=94=E4=BD=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dto/definition/parse/Swagger2Parser.java | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) 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()));