fix: swagger导入报错

--bug=1009378 --user=陈建星 [github #8948]客户那边 从yapi导出一个 swagger 2.0规范的文档,导入MS提示解析错误,postman导入是ok的。 https://www.tapd.cn/55049933/s/10881544
This commit is contained in:
chenjianxing 2021-12-29 15:36:11 +08:00 committed by fit2-zhao
parent 5ec50ae8af
commit 59d7d673b3
1 changed files with 10 additions and 3 deletions

View File

@ -359,7 +359,10 @@ public class Swagger3Parser extends SwaggerAbstractParser {
return new JSONObject(true); return new JSONObject(true);
} }
refSet.add(schema.get$ref()); refSet.add(schema.get$ref());
Object propertiesResult = parseSchemaPropertiesToJson(getModelByRef(schema.get$ref()), refSet, infoMap); Schema modelByRef = getModelByRef(schema.get$ref());
Object propertiesResult = null;
if (modelByRef != null)
propertiesResult = parseSchemaPropertiesToJson(modelByRef, refSet, infoMap);
return propertiesResult == null ? getDefaultValueByPropertyType(schema) : propertiesResult; return propertiesResult == null ? getDefaultValueByPropertyType(schema) : propertiesResult;
} else if (schema instanceof ArraySchema) { } else if (schema instanceof ArraySchema) {
JSONArray jsonArray = new JSONArray(); JSONArray jsonArray = new JSONArray();
@ -436,7 +439,9 @@ public class Swagger3Parser extends SwaggerAbstractParser {
if (ref.split("/").length > 3) { if (ref.split("/").length > 3) {
ref = ref.replace("#/components/schemas/", ""); ref = ref.replace("#/components/schemas/", "");
} }
return this.components.getSchemas().get(ref); if (this.components.getSchemas() != null)
return this.components.getSchemas().get(ref);
return null;
} }
private JsonSchemaItem parseSchema(Schema schema, Set<String> refSet) { private JsonSchemaItem parseSchema(Schema schema, Set<String> refSet) {
@ -446,7 +451,9 @@ public class Swagger3Parser extends SwaggerAbstractParser {
if (refSet.contains(schema.get$ref())) return item; if (refSet.contains(schema.get$ref())) return item;
item.setType("object"); item.setType("object");
refSet.add(schema.get$ref()); refSet.add(schema.get$ref());
item.setProperties(parseSchemaProperties(getModelByRef(schema.get$ref()), refSet)); Schema modelByRef = getModelByRef(schema.get$ref());
if (modelByRef != null)
item.setProperties(parseSchemaProperties(modelByRef, refSet));
} else if (schema instanceof ArraySchema) { } else if (schema instanceof ArraySchema) {
Schema items = ((ArraySchema) schema).getItems(); Schema items = ((ArraySchema) schema).getItems();
item.setType("array"); item.setType("array");