Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Null type in JSON schema ignore when generating swagger. #594

Open
2 tasks done
dormesica opened this issue May 3, 2022 · 6 comments
Open
2 tasks done

Null type in JSON schema ignore when generating swagger. #594

dormesica opened this issue May 3, 2022 · 6 comments

Comments

@dormesica
Copy link

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

3.29.0

Plugin version

6.0.1

Node.js version

16.13.1

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

12.3.1

Description

I'm using Typebox to generate JSON schemas for an endpoint's response. In one endpoint some response properties may be null.

In Typebox we defined it as follow

Type.Union([Type.String(), Type.Null()]

Which seems to generate the correct JSON schema

{
  "anyOf": [
    { "type": "string" },
    { "type": "null" }
  ]
}

The generated swagger file does not indicate that field can be nullable (which cases some of our tests to fail).

Steps to Reproduce

Create a schema with a property with type

{
  "anyOf": [
    { "type": "string" },
    { "type": "null" }
  ]
}

Generate swagger file.

Expected Behavior

No response

@climba03003
Copy link
Member

It is expected. There is no type call null in swagger or openapi.
You should transform it on your own.

@dormesica
Copy link
Author

Type can be declared as Nullable as far as I know.
It it not supported?

@climba03003
Copy link
Member

Type can be declared as Nullable as far as I know. It it not supported?

"type": "null" is the syntax of JSON Schema. Not Swagger 2.0 or OpenAPI 3.
It is complicated for a library to determine how to transform it to proper one.

You should use nullable property for Swagger 2.0 or OpenAPI 3. You may also need to add keyword for ajv.

Type.Union([Type.String({ nullable: true }), Type.Null()]

{
  "anyOf": [
    { "type": "string", nullable: true },
    { "type": "null" }
  ]
}

@dormesica
Copy link
Author

I tried using the type Type.String({ nullable: true }) and Type.Union([Type.String({ nullable: true }), Type.Null()] but it didn't work.
The generated swagger still does not allow the property to be null.

@TimonVS
Copy link
Contributor

TimonVS commented May 31, 2022

OpenAPI v3.1 is compatible with JSON Schema, and introduced support for "null". See "Schema Object Changes " in https://www.openapis.org/blog/2021/02/16/migrating-from-openapi-3-0-to-3-1-0.

As for OpenAPI 3.0, TypeBox has some documentation for supporting null types: https://github.com/sinclairzx81/typebox#OpenAPI. I haven't verified if this works well with fastify-swagger though.

@pebo
Copy link

pebo commented Aug 26, 2022

Type can be declared as Nullable as far as I know. It it not supported?

"type": "null" is the syntax of JSON Schema. Not Swagger 2.0 or OpenAPI 3. It is complicated for a library to determine how to transform it to proper one.

You should use nullable property for Swagger 2.0 or OpenAPI 3. You may also need to add keyword for ajv.

Type.Union([Type.String({ nullable: true }), Type.Null()]

{
  "anyOf": [
    { "type": "string", nullable: true },
    { "type": "null" }
  ]
}

Using the following versions:

    "@fastify/swagger": "^7.4.1",
    "@fastify/type-provider-typebox": "^2.3.0",
    "@sinclair/typebox": "^0.24.28",
    "fastify": "^4.5.2"

Given a typebox definition like:

deleted_at: Type.Union([Type.Null(), Type.String({ format: 'date-time',  nullable: true })]),

The following (invalid) open api 3.0 spec is generated:

deleted_at:
    anyOf:
      - type: 'null'
      - format: date-time
        nullable: true
        type: string

Dropping the Union would remove the in 3.0 invalid type: 'null part but that breaks the type validation as the attribute may be null. Is there a workaround for this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants