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

Type error when passing an object to apiSpec (TypeScript) #861

Open
germanescobar opened this issue Sep 25, 2023 · 1 comment
Open

Type error when passing an object to apiSpec (TypeScript) #861

germanescobar opened this issue Sep 25, 2023 · 1 comment

Comments

@germanescobar
Copy link

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

@Cisvhenriksen
Copy link

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.

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

2 participants