fix: swagger body参数循环依赖问题

This commit is contained in:
chenjianxing 2020-08-17 15:24:11 +08:00
parent 9692b8d2a5
commit c1c2bc7f76
3 changed files with 20 additions and 7 deletions

View File

@ -139,16 +139,21 @@ public class Swagger2Parser extends ApiImportAbstractParser {
simpleRef = refModel.getSimpleRef(); simpleRef = refModel.getSimpleRef();
} }
Model model = definitions.get(simpleRef); Model model = definitions.get(simpleRef);
JSONObject bodyParameters = getBodyJSONObjectParameters(model.getProperties(), definitions); HashSet<String> refSet = new HashSet<>();
refSet.add(simpleRef);
JSONObject bodyParameters = getBodyJSONObjectParameters(model.getProperties(), definitions, refSet);
body.setRaw(bodyParameters.toJSONString()); body.setRaw(bodyParameters.toJSONString());
} else if (schema instanceof ArrayModel) { } else if (schema instanceof ArrayModel) {
ArrayModel arrayModel = (ArrayModel) bodyParameter.getSchema(); ArrayModel arrayModel = (ArrayModel) bodyParameter.getSchema();
Property items = arrayModel.getItems(); Property items = arrayModel.getItems();
if (items instanceof RefProperty) { if (items instanceof RefProperty) {
RefProperty refProperty = (RefProperty) items; RefProperty refProperty = (RefProperty) items;
Model model = definitions.get(refProperty.getSimpleRef()); String simpleRef = refProperty.getSimpleRef();
HashSet<String> refSet = new HashSet<>();
refSet.add(simpleRef);
Model model = definitions.get(simpleRef);
JSONArray propertyList = new JSONArray(); JSONArray propertyList = new JSONArray();
propertyList.add(getBodyJSONObjectParameters(model.getProperties(), definitions)); propertyList.add(getBodyJSONObjectParameters(model.getProperties(), definitions, refSet));
body.setRaw(propertyList.toString()); body.setRaw(propertyList.toString());
} }
} }
@ -156,20 +161,26 @@ public class Swagger2Parser extends ApiImportAbstractParser {
body.setFormat("json"); body.setFormat("json");
} }
private JSONObject getBodyJSONObjectParameters(Map<String, Property> properties, Map<String, Model> definitions) { private JSONObject getBodyJSONObjectParameters(Map<String, Property> properties, Map<String, Model> definitions, HashSet<String> refSet) {
JSONObject jsonObject = new JSONObject(); JSONObject jsonObject = new JSONObject();
properties.forEach((key, value) -> { properties.forEach((key, value) -> {
if (value instanceof ObjectProperty) { if (value instanceof ObjectProperty) {
ObjectProperty objectProperty = (ObjectProperty) value; ObjectProperty objectProperty = (ObjectProperty) value;
jsonObject.put(key, getBodyJSONObjectParameters(objectProperty.getProperties(), definitions)); jsonObject.put(key, getBodyJSONObjectParameters(objectProperty.getProperties(), definitions, refSet));
} else if (value instanceof ArrayProperty) { } else if (value instanceof ArrayProperty) {
ArrayProperty arrayProperty = (ArrayProperty) value; ArrayProperty arrayProperty = (ArrayProperty) value;
Property items = arrayProperty.getItems(); Property items = arrayProperty.getItems();
if (items instanceof RefProperty) { if (items instanceof RefProperty) {
RefProperty refProperty = (RefProperty) items; RefProperty refProperty = (RefProperty) items;
Model model = definitions.get(refProperty.getSimpleRef()); String simpleRef = refProperty.getSimpleRef();
if (refSet.contains(simpleRef)) {
jsonObject.put(key, new JSONArray());
return;
}
refSet.add(simpleRef);
Model model = definitions.get(simpleRef);
JSONArray propertyList = new JSONArray(); JSONArray propertyList = new JSONArray();
propertyList.add(getBodyJSONObjectParameters(model.getProperties(), definitions)); propertyList.add(getBodyJSONObjectParameters(model.getProperties(), definitions, refSet));
jsonObject.put(key, propertyList); jsonObject.put(key, propertyList);
} else { } else {
jsonObject.put(key, new ArrayList<>()); jsonObject.put(key, new ArrayList<>());

@ -0,0 +1 @@
Subproject commit 356bb744a72304067b9222cb0cb2be01d4ebff2a

@ -0,0 +1 @@
Subproject commit 7e4d80cc2b870a8cac6dbb9fe6711ab6041faf6d