2.9.0 NumberFormatException when generation swagger.json doc breaking vendor extensions
See original GitHub issue- What version of the library are you using: 2.9.0
What kind of issue is this?
- Bug report
So something sneaked into the 2.9.0 version which breaks proper generation of the swagger.json definition. I will try to be as elaborate as possible.
Since 2.9.0 scalarExample
has been introduced in springfox.documentation.builders.ParameterBuilder.java
.
The ParameterBuilder scalarExample(Object scalarExample)
method uses the following fallback method this.scalarExample = defaultIfAbsent(scalarExample, this.scalarExample);
to decide to go for a given example or use the default one, which is null
.
When I debug how the builder method is invoked , I always see the scalarExample argument being set to an empty string eg. ""
. Tracing things back I bumped into io.swagger.annotations.ApiParam
which has its default value set to that empty string. String example() default "";
. So this seems like a mismatch between what the builder expects and what is actually passed. The defaultIfAbsent
method will always pick the empty string as it never will be null
.
So the server runs but when I request the swagger.json via http://{server}/v2/api-docs
I see a nasty NumberFormatException
being thrown. This happens in io.swagger.models.parameters.AbstractSerializableParameter
but it is due to the empty string being set as scalarExample. It tries to parse it via Long.valueOf but this is clearly not a good idea 😁
The code in that class looks like
@JsonProperty("x-example")
public Object getExample() {
if (example == null) {
return null;
}
try {
if (BaseIntegerProperty.TYPE.equals(type)) {
return Long.valueOf(example);
} else if (DecimalProperty.TYPE.equals(type)) {
return Double.valueOf(example);
} else if (BooleanProperty.TYPE.equals(type)) {
if ("true".equalsIgnoreCase(example) || "false".equalsIgnoreCase(defaultValue)) {
return Boolean.valueOf(example);
}
}
} catch (NumberFormatException e) {
LOGGER.warn(String.format("Illegal DefaultValue %s for parameter type %s", defaultValue, type), e);
}
return example;
}
My suggestion is to take empty strings into account in the defaultIfAbsent
method
Issue Analytics
- State:
- Created 5 years ago
- Reactions:3
- Comments:10 (2 by maintainers)
If you have any request/response classes that are being used as models in your swagger, I figured out what was causing the errors for me. I had an Integer field that had the annotation ApiModelProperty. If I add the
example = "1"
to the annotation the NumberFormatException goes away.This issue has been automatically closed because it has not had recent activity. Please re-open a new issue if this is still an issue.