zydxnyy opened a new issue #2599:
URL: https://github.com/apache/servicecomb-java-chassis/issues/2599
```java
public void generate() {
scanMethodAnnotation();
scanMethodParameters();
scanResponse();
correctOperation();
postProcessOperation(swaggerGenerator, this);
}
```
生成swagger顺序是先生成参数的模型,然后生成返回体的模型。如果参数和返回体有共用的模型,有可能会出错。原因是下面的代码在生成方法参数模型的时候把所有加了`NotBlank`和`NotEmpty`的模型的required属性都设置为了true:
```java
protected void fillParameter(Swagger swagger, Parameter parameter, String
parameterName, JavaType type,
List<Annotation> annotations) {
...
if (parameter instanceof AbstractSerializableParameter) {
io.swagger.util.ParameterProcessor.applyAnnotations(swagger,
parameter, type, annotations);
annotations.stream().forEach(annotation -> {
if
(NOT_NULL_ANNOTATIONS.contains(annotation.annotationType().getSimpleName())){
parameter.setRequired(true);
}
});
return;
}
...
}
```
但是生成完参数模型之后,生成返回体的模型的时候,下面的代码重新读取了返回体生成了新的模型`tempModel`,然后跟已有的模型比对的时候找到一个key一样的,但是`required`属性被修改过的Model,导致判断为重复的模型,抛出异常
```java
public static void addDefinitions(Swagger swagger, Type paramType) {
JavaType javaType =
TypeFactory.defaultInstance().constructType(paramType);
if (javaType.isTypeOrSubTypeOf(DynamicEnum.class)) {
return;
}
Map<String, Model> models =
ModelConverters.getInstance().readAll(javaType);
for (Entry<String, Model> entry : models.entrySet()) {
if (null != swagger.getDefinitions()) {
Model tempModel = swagger.getDefinitions().get(entry.getKey());
if (null != tempModel && !tempModel.equals(entry.getValue())) {
LOGGER.warn("duplicate param model: " + entry.getKey());
throw new IllegalArgumentException("duplicate param model: " +
entry.getKey());
}
}
swagger.addDefinition(entry.getKey(), entry.getValue());
}
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]