Validator throws NPE when validating request header

Issue #43 resolved
Tuan Dinh created an issue

Hi,

I ran into an issue that if a mandatory header is missing, the validator throws NullPointerException (Although if you provide a header with incorrect format, the validation runs correctly).

Take the following example: Given the swagger:

{
  "swagger": "2.0",
  "info": {
    "title": "Simple Pet Creation API",
    "version": "1.0.0"
  },
  "tags": [{
    "name": "pet",
    "description": "Operations related to Pets"
  }],
  "basePath": "/",
  "paths": {
       "/create": {
        "post": {
            "summary": "Create A pet",
            "description": "NA",
            "consumes": [
                "application/json"
            ],
            "parameters": [
                {
                    "$ref": "#/parameters/date"
                },
                {
                    "$ref": "#/parameters/petRequest"
                }
            ],
            "responses": {
                "201": {
                    "type" : "object"
                },
                "default": {
                    "description": "Error response",
                    "type" : "object"
                }    
            }
          }
        } 
  },
  "definitions": {
    "petRequest": {
          "type": "object",
          "properties": {
          "name": {
              "type": "string"
          },
          "petType": {
             "type": "string"
          }
          },
          "required": [
            "name",
            "petType"
          ]
    },
    "petReponse": {
        "type": "object"
    }
    },
    "parameters": {
        "date": {
            "name": "Date",
            "in": "header",
            "required": true,
            "type": "string",
            "format": "date-time"
        },
        "petRequest": {
            "name": "petRequest",
            "in": "body",
            "required": true,
            "schema": {
                "$ref": "#/definitions/petRequest"
        }
    }
  }
}

where the a "date" header with "date-time" format is required.

The following test passes:

    @Test
    public void wrongHeaderShouldFail() {
        final Request request = SimpleRequest.Builder.post("/create")
                .withBody("{\"name\":\"Kiki\",\"petType\":\"Dog\"}")
                .withHeader("Date", "2017-05-25")
                .build();
        final ValidationReport report = validator.validateRequest(request);
        assertTrue(!report.hasErrors());
    }

with error:

Parameter '2017-05-25' is not a valid ISO8601 date time.

But this test:

    @Test
    public void missingHeaderShouldFail() {
        final Request request = SimpleRequest.Builder.post("/create")
                .withBody("{\"name\":\"Kiki\",\"petType\":\"Dog\"}")
                .build();
        final ValidationReport report = validator.validateRequest(request);
        assertTrue(!report.hasErrors());
    }

results in this:

java.lang.NullPointerException
    at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:210)
    at com.google.common.collect.SingletonImmutableList.<init>(SingletonImmutableList.java:40)
    at com.google.common.collect.ImmutableList.of(ImmutableList.java:89)
    at com.atlassian.oai.validator.report.SingletonValidationReport.<init>(SingletonValidationReport.java:18)
    at com.atlassian.oai.validator.report.ValidationReport.singleton(ValidationReport.java:57)
    at com.atlassian.oai.validator.interaction.RequestValidator.validateParameter(RequestValidator.java:344)
    at com.atlassian.oai.validator.interaction.RequestValidator.lambda$12(RequestValidator.java:329)
    at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
    at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175)
    at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1374)
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
    at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.util.stream.ReferencePipeline.reduce(ReferencePipeline.java:474)
    at com.atlassian.oai.validator.interaction.RequestValidator.validateHeaders(RequestValidator.java:334)

Should we address this ? Thanks, Tuan

Comments (4)

  1. Log in to comment