openapi-spec-validator icon indicating copy to clipboard operation
openapi-spec-validator copied to clipboard

Nullable `allOf`s with default `null` values fail validation

Open pranjalv123 opened this issue 6 months ago • 0 comments

Hi,

In OpenAPI 3.0 it's a common pattern to represent a reference that can be null as an allOf with nullable: true.

However, if you set the default to null, openapi-spec-validator attempts to validate null against the referenced schema, and returns an error. The default should take into account the nullability of the schema that it's part of, not just the referenced allOf.

{
  "info": {
    "title": "Example",
    "version": "v1",
  },
  "openapi": "3.0.2",
  "components": {
    "schemas": {
      "FooSchema": {
        "title": "FooSchema",
        "type": "object"
      },
      "BarSchema": {
        "allOf": [
          {
            "$ref": "#/components/schemas/FooSchema"
          }
        ],
        "nullable": true,
        "default": null
      }
    }
  },
  "paths": {}
}
Failed validating 'type' in schema['allOf'][0]:
    {'title': 'FooSchema', 'type': 'object'}

On instance:
    None

pranjalv123 avatar Jul 01 '25 13:07 pranjalv123