kogosoftwarellc / open-api

A Monorepo of various packages to power OpenAPI in node

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

jiuyun01 opened this issue · comments

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;

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"
      }
    },

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.

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

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 = {