OpenAPI: False positive validation error on Required properties of an Optional property

Issue #770 resolved
Saravana Thiyagaraj created an issue
---
openapi: 3.0.2
info:
  title: Sample
  version: 1.0.0
paths:
  "/sample":
    get:
      description: Get index
      operationId: getIndex
      parameters:
        - description: Date Range
          in: query
          name: dateRange
          style: form
          explode: true
          required: true
          schema:
            type: object
            properties:
              dateFrom:
                type: string
                format: date
                example: "2020-11-23"
              dateTo:
                type: string
                format: date
                example: "2020-11-23"
            required:
              - dateFrom
              - dateTo
      responses:
        '200':
          description: Successful response
          content:
            application/json:
              schema:
                "$ref": "#/components/schemas/IndexResponse"
        '400':
          description: Bad request response
          content:
            application/json:
              schema:
                "$ref": "#/components/schemas/BadRequestResponse"
        '500':
          description: Internal server error
    post:
      description: Update index
      operationId: postIndex
      requestBody:
        required: false
        content:
          application/json:
            schema:
              "$ref": "#/components/schemas/IndexRequest"
      responses:
        '200':
          description: The resource was update successfully
          content:
            application/json:
              schema:
                "$ref": "#/components/schemas/IndexResponse"
        '400':
          description: Bad request response
          content:
            application/json:
              schema:
                "$ref": "#/components/schemas/BadRequestResponse"
        '500':
          description: Internal server error
    delete:
      description: Delete index
      operationId: deleteIndex
      responses:
        '204':
          description: The resource was deleted successfully
        '500':
          description: Internal server error
components:
  schemas:
    IndexRequest:
      description: Index request
      type: object
      properties:
        name:
          description: Name
          example: some name
          nullable: false
          type: string
      required:
        - name
    IndexResponse:
      description: Index response
      type: object
      properties:
        name:
          description: Name
          example: some name
          nullable: false
          type: string
      required:
        - name
    BadRequestResponse:
      description: Bad request response
      type: object
      properties:
        error:
          description: Description of the error
          example: Missing query parameter fromDate
          nullable: false
          type: string
      required:
        - error

As per above spec, the dateRange property is an optional one. But if that property is provided, then both dateFrom and dateTo are required.

So the below requests are all valid:

and the ones below are invalid:

Actual

But when I run the validation, for the http://localhost:8080/sample case, I case the below 2 validation errors in the ValidationReport

  • INFO - Query parameter 'dateFrom' is required on path '/sample' but not found in request.: []
  • INFO - Query parameter 'dateTo' is required on path '/sample' but not found in request.: []

Expected

There should not be any validation errors, since the dateRange itself is an optional property.

Comments (2)

  1. Log in to comment