From 0d57785de632979ab813993c67c385010df5e6a6 Mon Sep 17 00:00:00 2001 From: wxg0103 <727495428@qq.com> Date: Thu, 25 May 2023 14:31:13 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8Dswagger=E5=AF=BC=E5=85=A5=E5=A4=B1=E8=B4=A5?= =?UTF-8?q?=E7=9A=84=E7=BC=BA=E9=99=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1026085 --user=王孝刚 [接口测试]github#24323接口导入-导入 Swagger 文件报“格式错误” https://www.tapd.cn/55049933/s/1375755 --- .../dto/definition/parse/Swagger2Parser.java | 41 ++++++++++--------- .../dto/definition/parse/Swagger3Parser.java | 23 ++++++----- 2 files changed, 35 insertions(+), 29 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 ffc6ba30ab..24dc9f0a83 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 @@ -24,6 +24,7 @@ import io.swagger.models.auth.AuthorizationValue; import io.swagger.models.parameters.*; import io.swagger.models.properties.*; import io.swagger.parser.SwaggerParser; +import lombok.val; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.MapUtils; import org.apache.commons.lang3.StringUtils; @@ -226,28 +227,30 @@ public class Swagger2Parser extends SwaggerAbstractParser { // todo 路径变量 {xxx} 是否要转换 for (Parameter parameter : parameters) { - switch (parameter.getIn()) { - case SwaggerParameterType.PATH: - parsePathParameters(parameter, request.getRest()); - break; - case SwaggerParameterType.QUERY: - parseQueryParameters(parameter, request.getArguments()); - break; - case SwaggerParameterType.FORM_DATA: - parseFormDataParameters((FormParameter) parameter, request.getBody()); - break; - case SwaggerParameterType.BODY: - parseRequestBodyParameters(parameter, request.getBody()); - break; - case SwaggerParameterType.HEADER: - parseHeaderParameters(parameter, request.getHeaders()); - break; - case SwaggerParameterType.COOKIE: - parseCookieParameters(parameter, request.getHeaders()); - break; + if (StringUtils.isNotBlank(parameter.getIn())) { + switch (parameter.getIn()) { + case SwaggerParameterType.PATH: + parsePathParameters(parameter, request.getRest()); + break; + case SwaggerParameterType.QUERY: + parseQueryParameters(parameter, request.getArguments()); + break; + case SwaggerParameterType.FORM_DATA: + parseFormDataParameters((FormParameter) parameter, request.getBody()); + break; + case SwaggerParameterType.BODY: + parseRequestBodyParameters(parameter, request.getBody()); + break; + case SwaggerParameterType.HEADER: + parseHeaderParameters(parameter, request.getHeaders()); + break; + case SwaggerParameterType.COOKIE: + parseCookieParameters(parameter, request.getHeaders()); + break; // case SwaggerParameterType.FILE: // parsePathParameters(parameter, request); // break; + } } } } diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/parse/Swagger3Parser.java b/backend/src/main/java/io/metersphere/api/dto/definition/parse/Swagger3Parser.java index 249d84e881..4be687e71a 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/parse/Swagger3Parser.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/parse/Swagger3Parser.java @@ -34,6 +34,7 @@ import io.swagger.v3.parser.core.models.AuthorizationValue; import io.swagger.v3.parser.core.models.SwaggerParseResult; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.MapUtils; +import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.http.HttpMethod; @@ -626,16 +627,18 @@ public class Swagger3Parser extends SwaggerAbstractParser { Schema schema = getSchema(parameter.getSchema()); Set refSet = new HashSet<>(); JsonSchemaItem jsonSchemaItem = parseSchema(schema, refSet); - if (MapUtils.isEmpty(jsonSchemaItem.getProperties())) { - arguments.add(new KeyValue(queryParameter.getName(), getDefaultValue(queryParameter, jsonSchemaItem), getDefaultStringValue(queryParameter.getDescription()), parameter.getRequired(), getMin(jsonSchemaItem), getMax(jsonSchemaItem))); - } else { - Map properties = jsonSchemaItem.getProperties(); - properties.forEach((key, value) -> { - arguments.add(new KeyValue(key, getDefaultValue(queryParameter, value), - getDefaultStringValue(value.getDescription()), - parameter.getRequired(), - getMin(value), getMax(value))); - }); + if (ObjectUtils.isNotEmpty(jsonSchemaItem)){ + if (MapUtils.isEmpty(jsonSchemaItem.getProperties())) { + arguments.add(new KeyValue(queryParameter.getName(), getDefaultValue(queryParameter, jsonSchemaItem), getDefaultStringValue(queryParameter.getDescription()), parameter.getRequired(), getMin(jsonSchemaItem), getMax(jsonSchemaItem))); + } else { + Map properties = jsonSchemaItem.getProperties(); + properties.forEach((key, value) -> { + arguments.add(new KeyValue(key, getDefaultValue(queryParameter, value), + getDefaultStringValue(value.getDescription()), + parameter.getRequired(), + getMin(value), getMax(value))); + }); + } } }