yndlingsfar / serverless-openapi-integration-helper

The plugin separates x-amazon-apigateway extension syntax from your openapi3 files
MIT License
6 stars 5 forks source link

CORS header fixes #21

Closed henhal closed 1 year ago

henhal commented 1 year ago

Two problems were addressed:

  1. When adding cors headers to each operation, non-method objects under each path item were mistakenly also modified. For example in a document like this:
    paths:
      /pets:
        parameters:
          ...
        get:
          ...

Here, any content under the parameters object should not be modified - only HTTP method object such as get, post, put etc.

  1. Referenced responses were modified inline. This caused the resulting document to be invalid. Example:
    components:
      responses:
        MyErrorResponse:
          content:
            ...
    paths:
      /pets:
        get:
          ...
          responses:
            400:
              $ref: '#/components/responses/MyErrorResponse'

Here, the object under the '400' key must not be modified, since a $ref must not be combined with other attributes. Instead, each response object found under components/responses are modified with CORS content as well.

henhal commented 1 year ago

Partially fixes #20

yndlingsfar commented 1 year ago

Thank you so much for your great input. Unfortunately, after merging your change in main, I noticed that there is still an error message. Could you take a look at this please? So I haven't created a new version for NPM yet. I have attached my example project and the error message. I am so grateful for your support, after changing jobs and no longer actively using the module myself every day, I simply don't have the time for further development.

Bildschirm­foto 2023-04-08 um 10 19 40

example.zip

henhal commented 1 year ago

Hi @yndlingsfar! Thanks for merging my PRs! Sorry for missing one null check, I raised #25 to fix this. Your example project (which lacked components/responses in the spec) completed successfully for me with this fix applied onto serverless-openapi-integration-helper#master. Also, when I tried adding a response to the API it successfully injected CORS headers to it:

  responses:
    TestResponse:
      content:
        application/json:
          schema:
            type: object
      headers:
        Access-Control-Allow-Origin:
          style: simple
          explode: false
          schema:
            type: string
        Access-Control-Allow-Methods:
          style: simple
          explode: false
          schema:
            type: string
        Access-Control-Allow-Headers:
          style: simple
          explode: false
          schema:
            type: string