allOf gives validation error
Version: 2.9,1
Given the following schema
openapi: 3.0.0
info:
version: "0.1"
title: ""
description: ""
paths:
/pets:
patch:
requestBody:
content:
application/json:
schema:
\$ref: '#/components/schemas/Cat'
responses:
'200':
description: Updated
components:
schemas:
Pet:
type: object
required:
- pet_type
properties:
pet_type:
type: string
Cat:
allOf:
- $ref: '#/components/schemas/Pet'
- type: object
properties:
hunts:
type: boolean
age:
type: integer
and JSON body
{
"pet_type": "string",
"hunts": true,
"age": 0
}
result in the following message:
ERROR - Object instance has properties which are not allowed by the schema: ["age","hunts","pet_type"]: []
ERROR - Instance failed to match all required schemas (matched only 0 out of 2): [/allOf/0: Object instance has properties which are not allowed by the schema: ["age","hunts"], /allOf/1: Object instance has properties which are not allowed by the schema: ["pet_type"]]
ERROR - Object instance has properties which are not allowed by the schema: ["age","hunts"]: []
ERROR - Object instance has properties which are not allowed by the schema: ["pet_type"]: []
Comments (3)
-
-
- changed status to resolved
-
Hi James,
I encountered the same issue.
My understanding is that JSON schema does not allow simulating inheritance like this (as you said above).
But if I understand https://swagger.io/docs/specification/data-models/oneof-anyof-allof-not/#allof correctly Open API differs from JSON schema and inheritance can be simulated like this. Therefore the example above should be valid.
Or am I missing or missunderstanding something?
By the way: Thank you very much for providing
swagger-request-validator
! - Log in to comment
Hi - thanks for raising this. It looks like this is a known problem with JSON schema and
allOf
. There are details in the FAQ. Please take a look there - if you feel this is a different problem please feel free to re-open the issue. There is also #271 which is looking at better ways to handle this scenario.