question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Nullable object property fails to validate `null`

See original GitHub issue

Describe the bug

I’m working with an API that requires differentiating between properties that are T | undefined or T | null.

In OpenAPI 3.0, T | null properties seem to be modelled like:

schemas:
  TestObject:
    type: object
    properties:
      otherObjectMaybeNullA:
        $ref: "#/components/schemas/OtherObject"
        nullable: true
      otherObjectMaybeNullB:
        anyOf:
        - $ref: "#/components/schemas/OtherObject"
        nullable: true
    required:
    - otherObjectMaybeNullA
    - otherObjectMaybeNullB

  OtherObject:
    type: object

In both cases the validator does not accept null as a valid value. Errors are 'should be object' and 'should match some schema in anyOf'.

To Reproduce

Validate above against { otherObjectMaybeNullA: null, otherObjectMaybeNullB: null }.

Actual behavior

Validation fails.

Expected behavior

Validation should pass.

Examples and context

See above.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:2
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
dotMortiscommented, Nov 11, 2020

@cdimascio

v4.5.0

Nullable objects can’t be validated if properties are defined in the openapi.json.

This one works (nestedObject can be null or object)

ObjectOne:
  type: object
  additionalProperties: false
  required: 
    - id
    - username
  properties:
    id:
      type: string
      format: uuid
      readOnly: true
    username:
      type: string
    nestedObject:
      nullable: true
      type: object

This one can’t be null (error: .response.ObjectOne.nestedObject should be object)

ObjectOne:
  type: object
  additionalProperties: false
  required: 
    - id
    - username
  properties:
    id:
      type: string
      format: uuid
      readOnly: true
    username:
      type: string
    nestedObject:
      nullable: true
      type: object
      required:
        - name
        - id
      properties:
      	id:
          type: string
          format: uuid
        name:
          type: string
1reaction
bluenote10commented, Apr 2, 2020

Thanks for looking into this! Yes, that seems to be a bit of a mess in OpenAPI 3.0. My interpretation of Clarify Semantics of nullable in OpenAPI 3.0 was that at least the second form is valid. Other indicators:

  • The online swagger validator shows the second form as valid, and actually outputs a warning for the first case to change it into the second form.
  • When I raised the same issue for swagger-typescript-api support for both forms was added.

Looks like I’m a bit stuck in terms of work-arounds because:

  • I can’t migrate to OAS 3.1.0 yet, because other libraries depending on the API definitions don’t support it yet.
  • Regarding moving nullable: true to OtherObject. This other object is intended to appear in non-nullable form in other places. I naively thought I can wrap it into OtherObjectNullable, but that again results in having to combine $ref with nullable: true. This maybe leaves duplicating all fields in OtherObject and OtherObjectNullable as an option, which calls for trouble when changing the fields and forgetting to sync them.
Read more comments on GitHub >

github_iconTop Results From Across the Web

Yup how to validate mandatory fields on null object
The solution is to include both nullable() and required() to the shape. Example const requiredOption = Yup.object() .shape({ value: ...
Read more >
Solved: Parse JSON - validation failed for null values - h...
I am attempting to parse the JSON, which mostly works... except when null values appear in the string, at which point the validation...
Read more >
Avoid Check for Null Statement in Java - Baeldung
2. What Is NullPointerException? · Calling an instance method of a null object · Accessing or modifying a field of a null object...
Read more >
Handling JSON null and empty arrays and objects - IBM
When serializing to JSON, if a value of a property in the data object is null, then it will be serialized as a...
Read more >
How to allow None (null) for fields in JSON Schema?
however if my fb_id is set to None, the validation fails. ... If you also want to allow null values for fb_id, you...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found