question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
borcherspmcommented, May 22, 2018

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.

@ApiModelProperty(value = "The page number to return.", required = true, example = "1")
private Integer page;
0reactions
stale[bot]commented, Jun 24, 2020

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

OpenAPI Specification - Version 2.0 - Swagger
Version 2.0 specification defines a set of files required to describe an API. These files can then be used by the Swagger-UI project...
Read more >
Newest 'swagger' Questions - Stack Overflow
Hey I have generated a Swagger Json Doc with a vendor extension at Path (api Operation) level. Now I want to Parse all...
Read more >
Release Notes Red Hat Fuse 7.0 | Red Hat Customer Portal
Vendor extensions to Swagger specifications support the following: Providing client credentials as parameters. Obtaining a new access token based on HTTP ...
Read more >
Generating Swagger example requests with Swashbuckle
The Swashbuckle documentation shows the Vehicle schema as my input parameter, but I would like to replace it with examples of the various ......
Read more >
Swagger vendor extensions - Apiary Help
Swagger can be extended with Swagger Vendor Extensions. ... It takes a JSON object containing a question and a collection of answers in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found