swagger-api / swagger-editor

Swagger Editor
https://editor.swagger.io
Apache License 2.0
8.87k stars 2.24k forks source link

Unable to enable CORS in server generated nodejs from Swagger-Editor #2689

Open tgt87 opened 3 years ago

tgt87 commented 3 years ago

Q&A (please complete the following information)

Content & configuration

OpenAPI definition:

openapi: 3.0.1
info:
  title: Test APIs
  version: 1.0.0
servers:
- url: http://localhost:8080/api/v1
tags:
- name: Company

paths:
  /companies:
    get:
      tags:
      - Company
      summary: Get all companies
      description: Returns all companies
      operationId: getCompanies
      responses:
        200:
          description: success
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Company'

components:
  schemas:
    Company:
      type: object
      required:
      - name
      properties:
        id:
          type: integer
          readOnly: true
        name:
          type: string 
        isDeleted: 
          type: boolean

Describe the bug you're encountering

Hi, I generated the server code (nodejs-server) using the above yaml from Swagger Editor and was encountering CORS errors when I tried to call the api from an angular client. image

I've installed cors package (https://www.npmjs.com/package/cors) and added the following in the index.js file and still encountering the same error. image

I've also tried using other methods (as shown below) but still getting the same error: app.use(function (req, res, next) { res.setHeader('Access-Control-Allow-Origin', '*'); res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE'); res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type'); res.setHeader('Access-Control-Allow-Credentials', true); next(); });

Any idea on how to resolve this?

tgt87 commented 3 years ago

Found a workaround in https://github.com/bug-hunters/oas3-tools/issues/19

inoyakaigor commented 6 months ago

The issue is still relevant