cdimascio / express-openapi-validator

🦋 Auto-validates api requests, responses, and securities using ExpressJS and an OpenAPI 3.x specification

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Type error when passing an object to apiSpec (TypeScript)

germanescobar opened this issue · comments

Describe the bug

When the apiSpec property is an object, it shows type errors.

To Reproduce

Try to compile the following code with tsc (this is a valid spec):

const spec = {
  "openapi": "3.0.1",
  "info": {
    "title": "API Documentation",
    "version": "1.0.0"
  },
  "servers": [
    {
      "url": "/api",
      "description": "Local Server"
    }
  ],
  "components": {
    "schemas": {
      "ListApplicationsResponse": {
        "type": "string"
      }
    }
  },
  "paths": {},
  "tags": []
}

OpenApiValidator.middleware({
  apiSpec: spec,
  validateRequests: true
})

I have a repo ready here.

Actual behavior
It shows the following error:

Type '{ openapi: string; info: { title: string; version: string; }; servers: { url: string; description: string; }[]; components: { schemas: { ListApplicationsResponse: { type: string; }; }; }; paths: {}; tags: never[]; }' is not assignable to type 'string | Document'.
  Type '{ openapi: string; info: { title: string; version: string; }; servers: { url: string; description: string; }[]; components: { schemas: { ListApplicationsResponse: { type: string; }; }; }; paths: {}; tags: never[]; }' is not assignable to type 'Document'.
    The types of 'components.schemas' are incompatible between these types.
      Type '{ ListApplicationsResponse: { type: string; }; }' is not assignable to type '{ [key: string]: ReferenceObject | SchemaObject; }'.
        Property '"ListApplicationsResponse"' is incompatible with index signature.
          Type '{ type: string; }' is not assignable to type 'ReferenceObject | SchemaObject'.
            Type '{ type: string; }' is not assignable to type 'CompositionSchemaObject'.
              Types of property 'type' are incompatible.
                Type 'string' is not assignable to type 'undefined'.

Expected behavior
I was expecting this to work.

Examples and context

I believe the documenation is lacking as well on this, but I managed to Infer that we need to pass a String as the value to ApiSpec.
so ApiSpec: JSON.stringify(spec) should do the trick.

Don't know if this is the correct way, but it seems to work.