NullPointerException using $ref parameters

Issue #59 resolved
Mantas Indrašius created an issue

I'm using swagger-request-validator-core:1.1.1

Given the document fragment:

    "/user/businesses": {
      "x-swagger-router-controller": "businesses",
      "get": {
        "description": "return user businesses",
        "operationId": "getList",
        "parameters": [
          {
            "$ref": "#/parameters/os"
          },
        ]
  "parameters": {
    "os": {
      "name": "os",
      "in": "query",
      "description": "Operating System ID",
      "required": true,
      "type": "string"
    },
  },

I get a NullPointerException while validating requestHeaders, most probably because the "in" part of the parameter is null before resolving the $ref.

The offending piece of code in RequestValidator:

    @Nonnull
    private ValidationReport validateHeaders(@Nonnull final Request request,
                                             @Nonnull final ApiOperation apiOperation) {
        return apiOperation
                .getOperation()
                .getParameters()
                .stream()
                .filter(p -> p.getIn().equalsIgnoreCase("HEADER"))
                .map(p -> validateParameter(
                        apiOperation, p,
                        request.getHeaderValues(p.getName()),
                        "validation.request.parameter.header.missing")
                )
                .reduce(ValidationReport.empty(), ValidationReport::merge);
    }

p.getIn() is possibly null in this case.

Comments (8)

  1. James Navin

    Interesting. Ive not come across $ref in param definitions before. This is definitely a bug / unsupported use case at the moment.

  2. Nakul Vashishth

    Hi James Navin, This is a defect which comes when we use :

    SwaggerRequestResponseValidator.createFor(swaggerJson); where swaggerJson is actually swagger spec instead of url or file name.

    This issue still persist in 1.2.0.

    If we write that string in a file and give file name it work fine. It would be great if you can please look at this.

    If you need more information, please ask.

    Regards Nakul Vashishth Software Architect

  3. Abraham Grief

    I am using com.atlassian.oai:swagger-request-validator-restassured:1.2.3 in my project, and was experiencing this issue. This issue is apparently caused by a bug in the class io.swagger.parser.SwaggerParser within dependent library io.swagger:swagger-parser:1.0.26 where the resolution of parameter references are being unintentionally discarded.

    I was able to fix this problem in my own project by adding a dependency on io.swagger:swagger-parser:1.0.32. The commit that fixed the bug in SwaggerParser is here: https://github.com/swagger-api/swagger-parser/commit/041e8c46b1cfccd909edce081080837124e0d321#diff-e4cd5d19efebd31cb9fc321bd4217112

    Hope this helps.

  4. James Navin

    Thanks @heenenee ++ thats super useful.

    Ive set aside some time for this project next week - will look at it then.

  5. James Navin

    I was able to reproduce the problem when the parameters block was at the wrong level in the spec (and hence could not be resolved).

    Im adding some filtering to remove the NPE, but it may be that your swagger spec is malformed.

  6. Log in to comment