fix(接口测试): 解决swagger3导入导出json-schema是否必填值错误问题

--user=郭雨琦 swagger3导出json-schema是否必填值数据结构错误,导入无法保存json-schema是否必填值项
This commit is contained in:
guoyuqi 2022-03-17 11:41:35 +08:00 committed by xiaomeinvG
parent 27533deb57
commit 67a636e060
1 changed files with 11 additions and 10 deletions

View File

@ -458,13 +458,18 @@ public class Swagger3Parser extends SwaggerAbstractParser {
private JsonSchemaItem parseSchema(Schema schema, Set<String> refSet) { private JsonSchemaItem parseSchema(Schema schema, Set<String> refSet) {
if (schema == null) return null; if (schema == null) return null;
JsonSchemaItem item = new JsonSchemaItem(); JsonSchemaItem item = new JsonSchemaItem();
if(schema.getRequired()!=null){
item.setRequired(schema.getRequired());
}
if (StringUtils.isNotBlank(schema.get$ref())) { if (StringUtils.isNotBlank(schema.get$ref())) {
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());
Schema modelByRef = getModelByRef(schema.get$ref()); Schema modelByRef = getModelByRef(schema.get$ref());
if (modelByRef != null) if (modelByRef != null){
item.setProperties(parseSchemaProperties(modelByRef, refSet)); item.setProperties(parseSchemaProperties(modelByRef, refSet));
item.setRequired(modelByRef.getRequired());
}
} 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");
@ -715,6 +720,9 @@ public class Swagger3Parser extends SwaggerAbstractParser {
String type = requestBody.getString("type"); String type = requestBody.getString("type");
JSONObject parsedParam = new JSONObject(); JSONObject parsedParam = new JSONObject();
if(required!=null){
parsedParam.put("required",required);
}
if (StringUtils.isNotBlank(type)) { if (StringUtils.isNotBlank(type)) {
if (StringUtils.equals(type, "array")) { if (StringUtils.equals(type, "array")) {
JSONObject items = requestBody.getJSONObject("items"); JSONObject items = requestBody.getJSONObject("items");
@ -727,18 +735,11 @@ public class Swagger3Parser extends SwaggerAbstractParser {
} else if (StringUtils.equals(type, "object")) { } else if (StringUtils.equals(type, "object")) {
parsedParam.put("type", "object"); parsedParam.put("type", "object");
JSONObject properties = requestBody.getJSONObject("properties"); JSONObject properties = requestBody.getJSONObject("properties");
JSONObject swaggerProperties = new JSONObject();
properties.keySet().forEach((k) -> {
JSONObject item = buildJsonSchema(properties.getJSONObject(k), required);
if (required != null && required.contains(k)) {
item.put("required", true);
}
swaggerProperties.put(k, item);
});
if (StringUtils.isNotBlank(requestBody.getString("description"))) { if (StringUtils.isNotBlank(requestBody.getString("description"))) {
parsedParam.put("description", requestBody.getString("description")); parsedParam.put("description", requestBody.getString("description"));
} }
parsedParam.put("properties", swaggerProperties); parsedParam.put("properties", properties);
} else if (StringUtils.equals(type, "integer")) { } else if (StringUtils.equals(type, "integer")) {
parsedParam.put("type", "integer"); parsedParam.put("type", "integer");
parsedParam.put("format", "int64"); parsedParam.put("format", "int64");