cdimascio / express-openapi-validator

🦋 Auto-validates api requests, responses, and securities using ExpressJS and an OpenAPI 3.x specification
MIT License
919 stars 210 forks source link

Spec generated by tsoa giving error #681

Open AmitThakkar opened 2 years ago

AmitThakkar commented 2 years ago

Describe the bug We are using tsoa for generating OpenAPI specs(https://tsoa-community.github.io/docs/generating.html#using-cli) and it is working fine. But when I try to use express-openapi-validator I get below error:

ERROR in apps/api/src/main.ts:40:5
TS2322: Type '{ components: { examples: {}; headers: {}; parameters: {}; requestBodies: {}; responses: {}; schemas: { UserDto: { properties: { id: { type: string; format: string; }; name: { type: string; }; email: { ...; }; address: { ...; }; isActive: { ...; }; createdAt: { ...; }; }; required: string[]...' is not assignable to type 'string | Document'.
  Type '{ components: { examples: {}; headers: {}; parameters: {}; requestBodies: {}; responses: {}; schemas: { UserDto: { properties: { id: { type: string; format: string; }; name: { type: string; }; email: { ...; }; address: { ...; }; isActive: { ...; }; createdAt: { ...; }; }; required: string[]...' is not assignable to type 'Document'.
    Types of property 'paths' are incompatible.
      Type '{ "/users": { post: { operationId: string; responses: { "200": { description: string; content: { "application/json": { schema: { $ref: string; }; }; }; }; }; security: never[]; parameters: never[]; requestBody: { required: boolean; content: { ...; }; }; }; get: { ...; }; }; "/users/{id}": { ...; }; "/users/...' is not assignable to type 'PathsObject'.
        Property '"/users"' is incompatible with index signature.
          Type '{ post: { operationId: string; responses: { "200": { description: string; content: { "application/json": { schema: { $ref: string; }; }; }; }; }; security: never[]; parameters: never[]; requestBody: { required: boolean; content: { ...; }; }; }; get: { ...; }; }' is not assignable to type 'PathItemObject'.
            The types of 'get.responses' are incompatible between these types.
              Type '{ "200": { description: string; content: { "application/json": { schema: { items: { $ref: string; }; type: string; }; }; }; }; }' is not assignable to type 'ResponsesObject'.
                Property '"200"' is incompatible with index signature.
                  Type '{ description: string; content: { "application/json": { schema: { items: { $ref: string; }; type: string; }; }; }; }' is not assignable to type 'ReferenceObject | ResponseObject'.
                    Type '{ description: string; content: { "application/json": { schema: { items: { $ref: string; }; type: string; }; }; }; }' is not assignable to type 'ResponseObject'.
                      Types of property 'content' are incompatible.
                        Type '{ "application/json": { schema: { items: { $ref: string; }; type: string; }; }; }' is not assignable to type '{ [media: string]: MediaTypeObject; }'.
                          Property '"application/json"' is incompatible with index signature.
                            Type '{ schema: { items: { $ref: string; }; type: string; }; }' is not assignable to type 'MediaTypeObject'.
                              Types of property 'schema' are incompatible.
                                Type '{ items: { $ref: string; }; type: string; }' is not assignable to type 'ReferenceObject | SchemaObject | undefined'.
                                  Type '{ items: { $ref: string; }; type: string; }' is not assignable to type 'ArraySchemaObject'.
                                    Types of property 'type' are incompatible.
                                      Type 'string' is not assignable to type '"array"'.
    38 | app.use(
    39 |   OpenApiValidator.middleware({
  > 40 |     apiSpec: swaggerJson,
       |     ^^^^^^^
    41 |     validateResponses: true,
    42 |   })
    43 | );

I checked swagger.json(generated by tsoa) that also seems fine:

...
                            "get": {
                "operationId": "FindAll",
                "responses": {
                    "200": {
                        "description": "Ok",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "items": {
                                        "$ref": "#/components/schemas/UserDto"
                                    },
                                    "type": "array". <===  This line giving me error
                                }
                            }
                        }
                    }
                },
                "security": [],
                "parameters": []
            }
...
cdimascio commented 2 years ago

try casting the value as any as a workaround the issue. let me know if it works properly. assuming so, we'll have to look at the typescript types

apiSpec: <any> swaggerJson,