oneOf validation fails happy path case

Issue #321 resolved
Ben Yarger created an issue

Given the following schema:

openapi: 3.0.0
info:
  title: Validation
  description: >-
  version: 1.0.0
servers:
  - url: 'http://test.com'
    description: QATE
paths:
  /test:
    get:
      summary: placeholder summary
      responses:
        '200':
          description: Success
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/test'
components:
  schemas:
    test:
      oneOf:
        - $ref: '#/components/schemas/typeA'
        - $ref: '#/components/schemas/typeB'
    typeA:
      type: object
      properties:
        typeAValueA:
          type: string
    typeB:
      type: object
      properties:
        typeBValueB:
          type: string

and the test response

{
    "typeAValueA": "good"
}

We expect the validation to pass.

The validation fails with the following message:

Instance failed to match exactly one schema (matched 2 out of 2)

We are using:

.withLevelResolver(LevelResolverFactory.withAdditionalPropertiesIgnored())

When we execute the same validation directly against the same JSON schema validator library used by this tool we get the expected success result.

The JSON schema we use in this case is:

{ 
  "oneOf": 
    [ 
      { 
        "type": "object", 
        "additionalProperties": false,
        "properties": 
          { 
            "typeAValueA": 
              { 
                "type": "string" 
              }
          } 
      }, 
      { 
        "type": "object", 
        "additionalProperties": false,
        "properties": 
          { 
            "typeBValueB": 
              { 
                "type": "string" 
              } 
          } 
        } 
      ], 
  "$schema": "http://json-schema.org/draft-04/schema#" 
}

The inclusion of additionalProperties, in this case, is irrelevant due to the match of the response payload of the test response to the typeAValueA field and type.

Comments (3)

  1. James Navin

    Thanks for raising this.

    I think I can see what’s happening. Your schema does not set additionalProperties: false explicitly, and because you have set the additional properties validation to “ignored” the validator does not automatically inject these for you.

    Also, none of your properties are marked as required in the schema.

    Because of that, both typeA and typeB schemas will actually match any input (e.g. they have no required properties and allow any additional properties to be present). Because of that the oneOf validation result is actually correct.

    Some things you could try:

    • Specify required properties so that the schemas do not overlap; or
    • Explicitly add additionalProperties: false to your typeA and typeB schemas so that they do not accept arbitrary properties

    Let me know if that helps.

  2. Ben Yarger reporter

    Confirmed that proposed solution resolves the issue. Evaluated the JSON schema evaluator without additionalProperties: false and experienced the same issue. Solution is documented in reply to original ticket.

  3. Log in to comment