Error when validating response body against a schema defining oneOf

Issue #314 resolved
alvaro acevedo created an issue

Hi,

First of all, great library it helps me a lot !

A similar issue was reported in Issue #143 which was resolved but the issue still appears in version 2.12.0 of swagger-request-validator-core, method com.atlassian.oai.validator.schema.SchemaValidator.validate calls validate from com.fasterxml.jackson.databind.JsonNode which returns following error:

com.github.fge.jsonschema.core.report.ListProcessingReport: failure
--- BEGIN MESSAGES ---
error: object instance has properties which are not allowed by the schema: ["postal_code","street"]
    level: "error"
    schema: {"loadingURI":"#","pointer":"/properties/address"}
    instance: {"pointer":"/address"}
    domain: "validation"
    keyword: "additionalProperties"
    unwanted: ["postal_code","street"]
---  END MESSAGES  ---

When validating what it seems a valid schema and test

Open API :

openapi: 3.0.0
info:
  title: Polymorphism with oneOf in OAS
  version: '1.0'
  description: 'Exmaple to desmonstrate the usage polymorphism using oneOf in OAS.'
paths:
  /solicitors:
    post:
      summary: ''
      operationId: post-solicitors
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Legal'
              examples:
                example-1:
                  value:
                    id: 1
                    solicitor: My solicitor
                    address: 
                      street: Mark Lane 55
                      postal_code: NE1 3DQ
                example-2:
                  value:
                    id: 0
                    solicitor: string
                    address: 
                      email: support@yahoo.com
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Legal'
        description: ''
      description: Create a collateral
components:
  schemas:
    Legal:
      title: Legal
      type: object
      properties:
        id:
          type: integer
        solicitor:
          type: string
        address:
          oneOf:
            - properties:
                street:
                  type: string
                postal_code:
                  type: string
              required:
                - street
            - properties:
                email:
                  type: string
                  format: email
              required:
                - email
          type: object
      required:
        - id

Example of response:

{
  "id": 1,
  "solicitor": "My solicitor",
  "address": {
    "street": "Mark Lane 55",
    "postal_code": "NE1 3DQ"
  }
}

Please can you have a look at it?

Many thanks

Comments (3)

  1. alvaro acevedo reporter

    A quick update on this, I managed to successfully validate above response example by ignoring the additional properties to:

    com.atlassian.oai.validator.schema.SchemaValidator.ADDITIONAL_PROPERTIES_KEY : "validation.schema.additionalProperties"

    as there is definition of additionalProperties: true or false in any component, com.atlassian.oai.validator.schema.SchemaValidator set everything to additionalProperties:false - this results into an issue for the oneOf JSON Schema Validation

    {
    "title": "Legal",
    "required": [
    "id"
    ],
    "type": "object",
    "properties": {
    "id": {
    "type": "integer",
    "exampleSetFlag": false,
    "additionalProperties": false
    },
    "solicitor": {
    "type": "string",
    "exampleSetFlag": false,
    "additionalProperties": false
    },
    "address": {
    "type": "object",
    "exampleSetFlag": false,
    "oneOf": [
    {
    "required": [
    "street"
    ],
    "properties": {
    "street": {
    "type": "string",
    "exampleSetFlag": false,
    "additionalProperties": false
    },
    "postal_code": {
    "type": "string",
    "exampleSetFlag": false,
    "additionalProperties": false
    }
    },
    "exampleSetFlag": false,
    "additionalProperties": false
    },
    {
    "required": [
    "email"
    ],
    "properties": {
    "email": {
    "type": "string",
    "format": "email",
    "exampleSetFlag": false,
    "additionalProperties": false
    }
    },
    "exampleSetFlag": false,
    "additionalProperties": false
    }
    ],
    "additionalProperties": false
    }
    },
    "exampleSetFlag": false,
    "components": {
    "schemas": {
    "Legal": {
    "title": "Legal",
    "required": [
    "id"
    ],
    "type": "object",
    "properties": {
    "id": {
    "type": "integer",
    "exampleSetFlag": false,
    "additionalProperties": false
    },
    "solicitor": {
    "type": "string",
    "exampleSetFlag": false,
    "additionalProperties": false
    },
    "address": {
    "type": "object",
    "exampleSetFlag": false,
    "oneOf": [
    {
    "required": [
    "street"
    ],
    "properties": {
    "street": {
    "type": "string",
    "exampleSetFlag": false,
    "additionalProperties": false
    },
    "postal_code": {
    "type": "string",
    "exampleSetFlag": false,
    "additionalProperties": false
    }
    },
    "exampleSetFlag": false,
    "additionalProperties": false
    },
    {
    "required": [
    "email"
    ],
    "properties": {
    "email": {
    "type": "string",
    "format": "email",
    "exampleSetFlag": false,
    "additionalProperties": false
    }
    },
    "exampleSetFlag": false,
    "additionalProperties": false
    }
    ],
    "additionalProperties": false
    }
    },
    "exampleSetFlag": false,
    "additionalProperties": false
    }
    }
    },
    "additionalProperties": false
    }

  2. James Navin

    A fix to related issue #336 was released in v2.19.3

    Please test with the latest version. If the problem persists, feel free to re-open the issue.

  3. Log in to comment