From d380b20a6df25b87c4c4bcabbaa8c543d989bded Mon Sep 17 00:00:00 2001 From: guoyuqi Date: Thu, 21 Jul 2022 20:07:26 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E8=A7=A3=E5=86=B3swagger3=E5=AF=BC=E5=85=A5=E6=97=B6=E9=AB=98?= =?UTF-8?q?=E7=BA=A7=E9=85=8D=E7=BD=AE=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --user=郭雨琦 --bug=1014740 https://www.tapd.cn/55049933/bugtrace/bugs/view/1155049933001014740 --- .../dto/definition/parse/Swagger3Parser.java | 17 ++++++++++++++++- .../request/variable/JsonSchemaItem.java | 6 +++++- .../api/definition/components/body/ApiBody.vue | 12 +++++++----- 3 files changed, 28 insertions(+), 7 deletions(-) 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 aef9e0a3a4..9998a14640 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 @@ -456,6 +456,9 @@ public class Swagger3Parser extends SwaggerAbstractParser { private JsonSchemaItem parseSchema(Schema schema, Set refSet) { if (schema == null) return null; + if (StringUtils.isBlank(schema.get$ref()) && schema.getProperties() == null && refSet.isEmpty()) { + return null; + } JsonSchemaItem item = new JsonSchemaItem(); if (schema.getRequired() != null) { item.setRequired(schema.getRequired()); @@ -478,16 +481,28 @@ public class Swagger3Parser extends SwaggerAbstractParser { } else if (schema instanceof ObjectSchema) { item.setType("object"); item.setProperties(parseSchemaProperties(schema, refSet)); + } else if (schema instanceof StringSchema) { + item.setType("string"); + } else if (schema instanceof IntegerSchema) { + item.setType("integer"); + } else if (schema instanceof NumberSchema) { + item.setType("number"); + } else if (schema instanceof BooleanSchema) { + item.setType("boolean"); } else { return null; } - if (schema.getExample() != null) { item.getMock().put("mock", schema.getExample()); } else { item.getMock().put("mock", ""); } + item.setDescription(schema.getDescription()); + item.setPattern(schema.getPattern()); + item.setMaxLength(schema.getMaxLength()); + item.setMinLength(schema.getMinLength()); + return item; } diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/variable/JsonSchemaItem.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/variable/JsonSchemaItem.java index 103c5772fe..d91772962c 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/variable/JsonSchemaItem.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/variable/JsonSchemaItem.java @@ -22,7 +22,11 @@ public class JsonSchemaItem { private Map mock; private Map properties; private JsonSchemaItem additionalProperties; - private List required ; + private List required; + private String pattern; + private Integer maxLength; + private Integer minLength; + @JSONField(name = "$schema") private String schema; diff --git a/frontend/src/business/components/api/definition/components/body/ApiBody.vue b/frontend/src/business/components/api/definition/components/body/ApiBody.vue index b90c948acf..22808d53e9 100644 --- a/frontend/src/business/components/api/definition/components/body/ApiBody.vue +++ b/frontend/src/business/components/api/definition/components/body/ApiBody.vue @@ -222,11 +222,13 @@ export default { const MsConvert = new Convert(); if (this.body.format === 'JSON-SCHEMA') { - if (this.body.raw && !this.body.jsonSchema) { - this.body.jsonSchema = MsConvert.format(JSON.parse(this.body.raw)); - } else { - let data = MsConvert.format(JSON.parse(this.body.raw)); - this.body.jsonSchema = this.deepAssign(this.body.jsonSchema, data); + if (this.body.raw) { + if (!this.body.jsonSchema) { + this.body.jsonSchema = MsConvert.format(JSON.parse(this.body.raw)); + } else { + let data = MsConvert.format(JSON.parse(this.body.raw)); + this.body.jsonSchema = this.deepAssign(this.body.jsonSchema, data); + } } } else { if (this.body.jsonSchema) {