Skip to content

Commit

Permalink
修复 solon-swagger2-knife4j 插件,递归类型的数据模型会栈溢出的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
noear committed Jul 19, 2023
1 parent 42f024a commit 8430856
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 11 deletions.
2 changes: 1 addition & 1 deletion UPDATE_LOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
* 优化 Nami 编码器匹配策略及头同步策略,尤其是仅单编码包引入时
* 优化 AbstractRoutingDataSource 的关闭处理
* 优化 ShardingDataSource 增加 Closeable 接口支持
* 修复 solon-swagger2-knife4j 插件,递归类型的数据结构会栈溢出的问题???
* 修复 solon-swagger2-knife4j 插件,递归类型的数据模型会栈溢出的问题
* dbvisitor 升为 5.3.3
* mybatis-flex 升为 1.5.0
* fastmybatis 升为 2.8.1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -629,10 +629,16 @@ private Model parseSwaggerModel(Class<?> clazz, Type type) {
}

if (itemClazz instanceof Class) {
ModelImpl swaggerModel = (ModelImpl) this.parseSwaggerModel((Class<?>) itemClazz, itemClazz);

RefProperty itemPr = new RefProperty(swaggerModel.getName(), RefFormat.INTERNAL);
fieldPr.setItems(itemPr);
if(itemClazz.equals(type)){
//避免出现循环依赖,然后 oom
RefProperty itemPr = new RefProperty(modelName, RefFormat.INTERNAL);
fieldPr.setItems(itemPr);
}else {
ModelImpl swaggerModel = (ModelImpl) this.parseSwaggerModel((Class<?>) itemClazz, itemClazz);

RefProperty itemPr = new RefProperty(swaggerModel.getName(), RefFormat.INTERNAL);
fieldPr.setItems(itemPr);
}
}


Expand All @@ -643,14 +649,24 @@ private Model parseSwaggerModel(Class<?> clazz, Type type) {


if (BuilderHelper.isModel(typeClazz)) {
ModelImpl swaggerModel = (ModelImpl) this.parseSwaggerModel(typeClazz, typeClazz);
if(typeClazz.equals(type)){
//避免出现循环依赖,然后 oom
RefProperty fieldPr = new RefProperty(modelName, RefFormat.INTERNAL);
if (apiField != null) {
fieldPr.setDescription(apiField.value());
}

RefProperty fieldPr = new RefProperty(swaggerModel.getName(), RefFormat.INTERNAL);
if (apiField != null) {
fieldPr.setDescription(apiField.value());
}
fieldList.put(field.getName(), fieldPr);
}else {
ModelImpl swaggerModel = (ModelImpl) this.parseSwaggerModel(typeClazz, typeClazz);

fieldList.put(field.getName(), fieldPr);
RefProperty fieldPr = new RefProperty(swaggerModel.getName(), RefFormat.INTERNAL);
if (apiField != null) {
fieldPr.setDescription(apiField.value());
}

fieldList.put(field.getName(), fieldPr);
}
} else {
ObjectProperty fieldPr = new ObjectProperty();
fieldPr.setName(field.getName());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.swagger.demo.controller.app;


import com.swagger.demo.model.bean.Node;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
Expand Down Expand Up @@ -36,4 +37,10 @@ public class DemoController {
public Map test2() {
return new HashMap();
}

@ApiOperation("复杂返回树")
@Mapping("test3")
public Node test3(){
return new Node();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.swagger.demo.model.bean;

import java.util.List;

/**
* @author noear 2023/7/19 created
*/
public class Node {
int id;
int pid;

List<Node> children;

Node last;
}

0 comments on commit 8430856

Please sign in to comment.