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 allOf construct doesn't work

See original GitHub issue

Describe the bug I’m trying to describe a nullable ref and spectral is yelling at my examples being invalid

To Reproduce

Given this OpenAPI document:

openapi: 3.0.0
info:
  title: repro
  version: '1.0'
servers:
  - url: 'http://localhost:3000'
paths:
  '/users/{userId}':
    parameters:
      - schema:
          type: integer
        name: userId
        in: path
        required: true
        description: Id of an existing user.
    get:
      summary: Get User Info by User ID
      tags: []
      responses:
        '200':
          description: User Found
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/User'
              examples:
                Get User Alice Smith:
                  value:
                    nullable_ref: null
        '404':
          description: User Not Found
      operationId: get-users-userId
      description: Retrieve the information of the user with the matching user ID.
components:
  schemas:
    Foo:
      type: object
      properties:
        bar:
          type: string
      required:
        - bar
    User:
      title: User
      type: object
      description: ''
      properties:
        nullable_ref:
          nullable: true
          allOf:
            - $ref: "#/components/schemas/Foo"
      required:
        - nullable_ref

Run this command:

npx @stoplight/spectral lint -q repro.yaml

Received:

29:35    error  oas3-valid-oas-content-example  `nullable_ref` property type should be object                            paths./users/{userId}.get.responses[200].content.application/json.examples['Get User Alice Smith'].value.nullable_ref

In the 6.0.0 alpha, I get an error:

Error: schema is invalid: data/properties/nullable_ref/type must be equal to one of the allowed values, data/properties/nullable_ref/type/0 must be equal to one of the allowed values, data/properties/nullable_ref/type must match a schema in anyOf
    at Ajv.validateSchema (/Users/philipbjorge/.npm/_npx/97956/lib/node_modules/@stoplight/spectral/node_modules/ajv/dist/core.js:251:23)
    at Ajv._addSchema (/Users/philipbjorge/.npm/_npx/97956/lib/node_modules/@stoplight/spectral/node_modules/ajv/dist/core.js:443:18)
    at Ajv.compile (/Users/philipbjorge/.npm/_npx/97956/lib/node_modules/@stoplight/spectral/node_modules/ajv/dist/core.js:145:26)
    at Object.exports.schema (/Users/philipbjorge/.npm/_npx/97956/lib/node_modules/@stoplight/spectral/dist/functions/schema/schema.js:37:33)
    at Object.o (eval at exports.evaluateExport (/Users/philipbjorge/.npm/_npx/97956/lib/node_modules/@stoplight/spectral/dist/ruleset/utils/evaluators.js:89:80), <anonymous>:1:1817)
    at Object.oasExample (eval at exports.evaluateExport (/Users/philipbjorge/.npm/_npx/97956/lib/node_modules/@stoplight/spectral/dist/ruleset/utils/evaluators.js:89:80), <anonymous>:1:3524)
    at Object.exports.lintNode (/Users/philipbjorge/.npm/_npx/97956/lib/node_modules/@stoplight/spectral/dist/runner/lintNode.js:29:33)
    at callback (/Users/philipbjorge/.npm/_npx/97956/lib/node_modules/@stoplight/spectral/dist/runner/runner.js:38:32)
    at JSONPath._handleCallback (/Users/philipbjorge/.npm/_npx/97956/lib/node_modules/@stoplight/spectral/node_modules/jsonpath-plus/dist/index-node-cjs.js:306:5)
    at JSONPath._trace (/Users/philipbjorge/.npm/_npx/97956/lib/node_modules/@stoplight/spectral/node_modules/jsonpath-plus/dist/index-node-cjs.js:337:10)

Expected behavior I expected this example to pass validation – I thought the nullable: true, allOf approach was the appropriate way to create a nullable reference in OAS 3

Screenshots If applicable, add screenshots to help explain your problem.

Environment (remove any that are not applicable):

  • Library version: 5.9.1

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
philipbjorgecommented, May 12, 2021

@meme – Not that it helps the primary issue, but I can share that the - type: "null" construct will only be valid with a OAS 3.1 doc

0reactions
checkin247commented, Sep 5, 2022

updating to openapi 3.1.0 (from 3.0.0) fixed the issue for me. Thanks to : https://github.com/stoplightio/spectral/issues/1603#issuecomment-840048026

The OpenApi 3.0.0 states: “Note that there is no null type; instead, the nullable attribute is used as a modifier of the base type.” Reference

Read more comments on GitHub >

github_iconTop Results From Across the Web

openapi - How to specify a property can be null or a reference ...
As a workaround I ended up defining a must-be-null ref called null_type that I can use in an anyOf construct. Like so: allOf:...
Read more >
Schema generation rules · GitBook - Goswagger.Io
anyOf , oneOf and not constructs are not supported (this is for OpenAPI 3); the null type is not supported (the nullable keyword...
Read more >
OpenAPI Specification - Version 3.0.3 - Swagger
The OpenAPI Specification allows combining and extending model definitions using the allOf property of JSON Schema, in effect offering model composition. allOf ......
Read more >
Documentation - TypeScript 2.0
In strict null checking mode the compiler requires every reference to a local variable of a type that doesn't include undefined to be...
Read more >
Solved: Pattern vs. nullable - SmartBear Community
Will "nullable" contradict the pattern, if it does not include null value as well? Without allOf, how the pattern regex and the sub...
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