fastify / fastify-swagger

Swagger documentation generator for Fastify
MIT License
941 stars 209 forks source link

Type error on passing JSON schema to openapi.components.schema #705

Closed benevbright closed 1 year ago

benevbright commented 1 year ago

Prerequisites

Fastify version

-

Plugin version

No response

Node.js version

18.12.1

Operating system

Linux

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

13.0

Description

I get type error when passing a JSON schema to register option openapi.components.schema. (I create the json schema using Typebox)

fastify-swagger is using openapi-types for TS type. But we(fastify-swagger) don't have their change that they've made for OpenApi 3.1 https://github.com/kogosoftwarellc/open-api/pull/757 and now they have OpenAPIV3_1.Document.

So we would need to apply this to here https://github.com/fastify/fastify-swagger/blob/49f16423a5d7b0b315d8c145d8fbda9ef11f6b37/index.d.ts#L116 somehow, using OR or something.

Steps to Reproduce

Try this on TS playground

import { OpenAPIV3, OpenAPIV3_1 } from "openapi-types"

// error: Type '"null"' is not assignable to type 'NonArraySchemaObjectType | undefined'.(2322)
const User1: OpenAPIV3.SchemaObject = {
  type: "object",
  properties: {
    name: { type: "string" },
    city: {
      anyOf: [{ type: "null" }, { type: "string" }],
    },
  },
};

// ok
const User2: OpenAPIV3_1.SchemaObject = {
  type: "object",
  properties: {
    name: { type: "string" },
    city: {
      anyOf: [{ type: "null" }, { type: "string" }],
    },
  },
};

Also, it's reproducible when passing this object to openapi.components.schema option on fastify.register.

Expected Behavior

No response

benevbright commented 1 year ago

related to https://github.com/fastify/fastify-swagger/issues/499

mcollina commented 1 year ago

Thanks for reporting! Would you like to send a Pull Request to address this issue? Remember to add unit tests.

nidhi-kala commented 1 year ago

If no one has taken this yet, I'm happy to send a pull request for this.