nullable in combination with anyOf is not correctly validated.

Issue #355 new
Thorsten Hake created an issue

The OpenAPI 3.0 specification allows the definition of “nullable” anyOf Schemas like the following:

Foo:
  type: object
  properties:
    nullableProperty:
      nullable: true
      anyOf:
        - type: string

However swagger-request-validator does not seem to honor the nullable flag in this situation. The following JSON will incorrectly be marked as invalid:

{"nullableProperty" : null}

I have attached a complete unit test to reproduce this issue.

Comments (3)

  1. Kristijan Faust

    Any updates on this one? This seems like a major issue and it is stale for almost a year now.

  2. Fabrice Gabolde

    This prevents very common use cases from being implemented, for instance given two business objects Widget and Spline where widgets have 0 or 1 splines we typically design response objects like this, mirroring the database rows:

    {"widgets": [
      {"id": 123,
       "name": "a widget with a spline",
       "spline_id": 789},
      {"id": 456,
       "name": "a widget without a spline",
       "spline_id": null}
    ]}
    

    Now normally you would write the Widget component schema such that it has a spline_id property which is a ref to SplineID which is not nullable... you don't want the Spline component schema's id property to be nullable, after all, you want the Widget’s own spline_id to be sometimes null and sometimes a valid SplineID. With the limitation described by the OP, I don’t know how to document and validate this.

    Or when creating Widgets, you want the user to be able to specify that the widget they’re creating has a Spline (by passing a valid ID), or no Spline (by passing null). (Making the field itself optional is a possibility but it adds complexity to the client.)

  3. Log in to comment