kogosoftwarellc / open-api

A Monorepo of various packages to power OpenAPI in node
MIT License
895 stars 237 forks source link

Type 'string' is not assignable to type 'NonArraySchemaObjectType | undefined'. #856

Closed jiuyun01 closed 1 year ago

jiuyun01 commented 1 year ago

with the following apiDoc for OAS 3.

const apiDoc = {
  openapi: "3.0.2",
  info: {
    title: "Test API",
    version: "1.0.0"
  },
  servers: [
    {
      url: "/v1",
      description: "local testing v1"
    }
  ],
  paths: {},
  components: {
    schemas: {
      Todo: {
        title: "Todo",
        type: "object",
        properties: {
          id: {
            type: "number",
            description: "Todo ID",
            example: "123"
          },
          message: {
            type: "string"
          }
        },
        required: [
          "id",
          "message"
        ]
      }
    }
  }
}

export default apiDoc;
jiuyun01 commented 1 year ago

able to fix it by changing from:

type NonArraySchemaObjectType = 'boolean' | 'object' | 'number' | 'string' | 'integer';

to

type NonArraySchemaObjectType = boolean | object | number | string | integer;

But we also run into another similar issue:

            Type '{ type: string; scheme: string; bearerFormat: string; }' is not assignable to type 'HttpSecurityScheme'.
              Types of property 'type' are incompatible.
                Type 'string' is not assignable to type '"http"'.

if we try to add securitySchemes to components:

    securitySchemes: {
      ClientAuthorizer: {
        type: "http",
        scheme: "bearer",
        bearerFormat: "JWT"
      },
      ClientAccountOwnerAuthorizer: {
        type: "http",
        scheme: "bearer",
        bearerFormat: "JWT"
      }
    },
jiuyun01 commented 1 year ago

Swagger 2.0 version has type defined to be: type?: string | string[];

While openapi 3.0 one has type defined to be:

type NonArraySchemaObjectType = 'boolean' | 'object' | 'number' | 'string' | 'integer';
type?: NonArraySchemaObjectType;

That's why json version works for Swagger but not for Openapi one.

jiuyun01 commented 1 year ago
import apiDoc from "./api/api-doc";
...

async function initializeAPIs() {
  await initialize({
    apiDoc: apiDoc, //not working for openapi 3.0, but works for swagger 2.0
    //apiDoc: resolve(__dirname, "./api/api-doc.yml"), //working for api-doc.yml
jiuyun01 commented 1 year ago

got it fixed in a simple way. We just explicitly declare it with the right type, instead of relying on compiling time type inference. Instead of

const apiDoc = {

we should do:

import { OpenAPIV3 } from 'openapi-types'

const apiDoc: OpenAPIV3.Document = {