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

v5.0.1 with new ajv: short-hand syntax with nullable does not work anymore

Open aivanov-noveo opened this issue 2 years ago • 6 comments

Describe the bug Prior to v 5.x I could combine a ref component with nullable: true in anyOf to achieve describing a nullable prop. Now I got the error.

To Reproduce Combine a referenced component with nullable: true:

      someField:
        anyOf:
          - $ref: '#/components/schemas/field-type'
          - nullable: true

Actual behavior An exception instead of assuming a nullable component

"nullable" cannot be used without "type"

(I suppose at least it should be a validation error from the validator).

Expected behavior A nullable prop

Examples and context Not working example:

      someField:
        anyOf:
          - $ref: '#/components/schemas/field-type'
          - nullable: true

Workaround:

      someField:
        anyOf:
          - $ref: '#/components/schemas/field-type'
          - type: object
            nullable: true

aivanov-noveo avatar Jan 12 '23 07:01 aivanov-noveo

We ran into this problem to, so we reverted to v4.x.x for now. Would be nice if this could be solved in v5 though

Tlepel avatar Feb 27 '23 10:02 Tlepel

I am trying to upgrade to 5.x and am running into this issue as well. It's particularly frustrating because it is not specific about where the error occurred. We have a large file with many nullable: true and it is not immediately apparent what is causing the issue.

Jackman3005 avatar Apr 13 '23 04:04 Jackman3005

@cdimascio Any plans on fixing this issue any time soon?

aivanov-noveo avatar Apr 18 '23 10:04 aivanov-noveo

@aivanov-noveo can you provide a minimal version of the problematic spec that demonstrates the issue. Thus will help to reproduce and resolve

cdimascio avatar Apr 18 '23 11:04 cdimascio

@cdimascio here it is:

openapi: '3.0.0'
info:
  version: 1.0.0
  title: test
servers:
  - url: /
security: []
paths:
  /test:
    post:
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/UserLink'
      responses:
        '200':
          description: OK
components:
  schemas:
    User:
      type: object
      properties:
        id:
          type: integer
    UserLink:
      type: object
      properties:
        user:
          anyOf:
            - $ref: '#/components/schemas/User'
            - nullable: true

The middleware was registered like this:

import { middleware as oaMiddleware } from 'express-openapi-validator';
const apiTestSpec = join(__dirname, './api-test.yml');
const middleware = oaMiddleware({
  apiSpec: apiTestSpec,
  validateApiSpec: true,
});

aivanov-noveo avatar May 15 '23 10:05 aivanov-noveo

I am seeing this too--any update here?

gwprice115 avatar Jan 29 '24 21:01 gwprice115