refactor(接口测试): 修复swagger导入必选未选上的缺陷

--bug=1027488 --user=王孝刚 【接口测试】github#25290,【接口测试】通过导入swagger json
格式文件,导入MeterSphere之后,没有勾选必填框 https://www.tapd.cn/55049933/s/1393416
This commit is contained in:
wxg0103 2023-07-14 11:16:22 +08:00 committed by fit2-zhao
parent 8c9372f938
commit 59ea61761a
2 changed files with 15 additions and 6 deletions

View File

@ -567,6 +567,13 @@ public class Swagger2Parser extends SwaggerAbstractParser {
} else if (value instanceof ObjectProperty) { } else if (value instanceof ObjectProperty) {
subItem.setType(PropertyConstant.OBJECT); subItem.setType(PropertyConstant.OBJECT);
subItem.setProperties(parseSchemaProperties(((ObjectProperty) value).getProperties(), refSet)); subItem.setProperties(parseSchemaProperties(((ObjectProperty) value).getProperties(), refSet));
List<String> required = new ArrayList<>();
((ObjectProperty) value).getProperties().forEach((key, property) -> {
if (property.getRequired()) {
required.add(key);
}
});
subItem.setRequired(required);
} else { } else {
handleBaseProperties(subItem, value); handleBaseProperties(subItem, value);
} }

View File

@ -875,6 +875,9 @@ public class Swagger3Parser extends SwaggerAbstractParser {
} }
} else if (StringUtils.equals(type, PropertyConstant.OBJECT)) { } else if (StringUtils.equals(type, PropertyConstant.OBJECT)) {
parsedParam.put(PropertyConstant.TYPE, PropertyConstant.OBJECT); parsedParam.put(PropertyConstant.TYPE, PropertyConstant.OBJECT);
if (requestBody.optJSONArray(PropertyConstant.REQUIRED) != null) {
parsedParam.put(PropertyConstant.REQUIRED, requestBody.optJSONArray(PropertyConstant.REQUIRED));
}
JSONObject properties = requestBody.optJSONObject(PropertyConstant.PROPERTIES); JSONObject properties = requestBody.optJSONObject(PropertyConstant.PROPERTIES);
JSONObject jsonObject = buildFormDataSchema(properties); JSONObject jsonObject = buildFormDataSchema(properties);
if (StringUtils.isNotBlank(requestBody.optString("description"))) { if (StringUtils.isNotBlank(requestBody.optString("description"))) {
@ -1062,11 +1065,7 @@ public class Swagger3Parser extends SwaggerAbstractParser {
property.put("example", value); property.put("example", value);
} }
property.put("description", obj.optString("description")); property.put("description", obj.optString("description"));
property.put(PropertyConstant.REQUIRED, obj.optString(PropertyConstant.REQUIRED)); property.put(PropertyConstant.REQUIRED, obj.optJSONArray(PropertyConstant.REQUIRED));
if (obj.optJSONObject(PropertyConstant.REQUIRED) != null) {
JSONObject childProperties = buildFormDataSchema(obj.optJSONObject(PropertyConstant.REQUIRED));
property.put(PropertyConstant.REQUIRED, childProperties.optJSONObject(PropertyConstant.REQUIRED));
}
if (obj.optJSONObject(PropertyConstant.PROPERTIES) != null) { if (obj.optJSONObject(PropertyConstant.PROPERTIES) != null) {
JSONObject childProperties = buildFormDataSchema(obj.optJSONObject(PropertyConstant.PROPERTIES)); JSONObject childProperties = buildFormDataSchema(obj.optJSONObject(PropertyConstant.PROPERTIES));
property.put(PropertyConstant.PROPERTIES, childProperties.optJSONObject(PropertyConstant.PROPERTIES)); property.put(PropertyConstant.PROPERTIES, childProperties.optJSONObject(PropertyConstant.PROPERTIES));
@ -1227,8 +1226,11 @@ public class Swagger3Parser extends SwaggerAbstractParser {
items.forEach(item -> { items.forEach(item -> {
if (item instanceof JSONObject) { if (item instanceof JSONObject) {
JSONObject itemRequired = ((JSONObject) item).optJSONObject(PropertyConstant.REQUIRED); JSONObject itemRequired = ((JSONObject) item).optJSONObject(PropertyConstant.REQUIRED);
if (itemRequired != null) {
finalRequired.put(itemRequired); finalRequired.put(itemRequired);
} }
finalRequired.putAll(((JSONObject) item).optJSONArray(PropertyConstant.REQUIRED));
}
}); });
required = finalRequired; required = finalRequired;
} }