Validation fails on field using "oneOf" when one of the schemas is an empty one

Issue #388 new
Endre Gabriel created an issue

I have an Open API Spec including a field using "oneOf" clause and if one of the possible schemas is an empty one, then the validation fails with different error messages however the input is valid.

E.g.

OpenAPI spec

openapi: 3.0.1
info:
  title: Dummy API
  version: 3.1.0
paths:
  "/Pet":
    get:
      operationId: pets
      responses:
        '200':
          description: Ok
          content:
            application/json:
              schema:
                oneOf:
                - "$ref": "#/components/schemas/Dog"
                - "$ref": "#/components/schemas/Cat"
                - "$ref": "#/components/schemas/Mouse"
components:
  schemas:
    Dog:
      required:
      - name
      - barkTone
      type: object
      properties:
        name:
          type: string
        barkTone:
          type: string
    Cat:
      type: object
    Mouse:
      type: object
      properties:
        name:
          type: string
        cheeseType:
          type: string

Json Request 1

{
    "name": "Safranek",
    "barkTone": "high"
}

Validation result 1

[ERROR][RESPONSE][GET /Pet] Instance failed to match exactly one schema (matched 2 out of 3)

  • /oneOf/2: Object instance has properties which are not allowed by the schema: ["barkTone"]

  • [ERROR][] Object instance has properties which are not allowed by the schema: ["barkTone"]

Json Request 2

{
    "name": "Safranek",
    "cheeseType": "gouda"
}

Validation result 2

[ERROR][RESPONSE][GET /Pet] Instance failed to match exactly one schema (matched 2 out of 3)

  • /oneOf/0: Object instance has properties which are not allowed by the schema: ["cheeseType"]

  • /oneOf/0: Object has missing required properties (["barkTone"])

  • [ERROR][] Object instance has properties which are not allowed by the schema: ["cheeseType"]

  • [ERROR][] Object has missing required properties (["barkTone"])

Json Request 3

{
}

Validation result 3

[ERROR][RESPONSE][GET /Pet] Instance failed to match exactly one schema (matched 2 out of 3)

  • /oneOf/0: Object has missing required properties (["barkTone","name"])

  • [ERROR][] Object has missing required properties (["barkTone","name"])

If I add properties also to Cat, then no validation error occurs.

    Cat:
      type: object
      properties:
        name:
          type: string
        tailLength:
          type: string

My testcase:

    @Test
    public void testValdator() throws IOException {
        OpenApiInteractionValidator validator = OpenApiInteractionValidator
            .createFor( "/api/Pets.yaml" )
//            .withLevelResolver(
//                LevelResolver.create().withLevel("validation.schema.additionalProperties", Level.IGNORE).build())
            .build();

        InputStream json = getClass().getResourceAsStream("/testInput/pets-add.json");
        Response response = SimpleResponse.Builder
            .ok()
            .withContentType("application/json")
            .withBody( json )
            .build();
        ValidationReport report = validator.validateResponse( "/Pet", Request.Method.GET, response );
        if ( report.hasErrors() ) {
            log.error( "Not valid: {}", SimpleValidationReportFormat.getInstance().apply( report ) );
        }
    }

The validation faily also in the case if additionalProperties is set to IGNORED, however more issues are displayed in the error message.

Used library version: 2.28.0

Comments (0)

  1. Log in to comment