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 properties with a default of null are not possible according to the 3.0 spec

See original GitHub issue

From the spec

default - The default value represents what would be assumed by the consumer of the input as the value of the schema if one is not provided. Unlike JSON Schema, the value MUST conform to the defined type for the Schema Object defined at the same level. For example, if type is string, then default can be “foo” but cannot be 1.

This, combined with the fact that properties can be nullable, means that it’s not possible to have a property with a default value of null.

You can see below that the folks over at Swagger have interpreted the spec literally, as they should. It’s the spec that seems to be wrong.

Here’s a sample response body on my /todos/{id} route in the Swagger UI…

{
  "id": 0,
  "title": "string",
  "complete": false,
  "note": "null",
  "dueDate": "null",
  "categoryId": 0
}

And here’s the OpenAPI schema for my todo objects…

Todo:
  description: A todo item helps you track what you need to get done
  type: object
  required:
    - title
    - complete
  properties:
    id:
      type: integer
    title:
      type: string
    complete:
      type: boolean
      default: false
    note:
      type: string
      nullable: true
      default: null
    dueDate:
      type: string
      nullable: true
      default: null
    categoryId:
      type: integer
      nullable: true
      default: null

Looking at the note property, we can see that the default value of null gets interpreted as the string “null”.

Looking at categoryId, it seems that 0 is the closest integer to null they could think of.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:12 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
handrewscommented, Jan 20, 2020

If we need to further clarify the language in OAS 3.1, we should track this in #2099. Otherwise it sounds like maybe this can be closed?

0reactions
philsturgeoncommented, Feb 5, 2020

Dope! That PR is on the agenda so I’m gonna close this one on down.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to define a property that can be string or null in OpenAPI ...
The nullable keyword used in OAS 3.0.x (see below) does not exist in OAS 3.1, it was removed in favor of the 'null'...
Read more >
Migration Guide: SQL, Datasets and DataFrame - Apache Spark
Since Spark 3.3, Spark turns a non-nullable schema into nullable for API ... In Spark 3.0, negative scale of decimal is not allowed...
Read more >
Resolve nullable warnings | Microsoft Learn
Several compiler warnings indicate code that isn't null-safe. Learn how to address those warnings by making your code more resilient.
Read more >
How to manage nullable properties - Jane - Read the Docs
Most of the time, the schema you get from vendor have issues about nullability of their properties. Here Jane has an option called...
Read more >
Documentation - TypeScript 3.7
only checks for whether the value on the left of it is null or undefined - not any of the subsequent properties. 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