alma-cdk / openapix

Combine the power of AWS CDK & OpenAPI YAML Schema Definitions
https://constructs.dev/packages/@alma-cdk/openapix/
94 stars 6 forks source link

fix: Write defaultCors to correct path, output with x-amazon-apigateway-integration key #27

Closed hutchy2570 closed 2 years ago

hutchy2570 commented 2 years ago

Fixes #26

This writes the defaultCors output to paths.${path}.options, with the integration key x-amazon-apigateway-integration

Here is the generated document from the hello-api example with this fix:

{
  "openapi": "3.0.3",
  "info": {
    "title": "Hello API",
    "description": "Defines an example “Hello World” API",
    "version": "0.0.1"
  },
  "paths": {
    "/": {
      "get": {
        "operationId": "sayHello",
        "summary": "Say Hello",
        "description": "Prints out a greeting",
        "parameters": [
          {
            "name": "name",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string",
              "default": "World"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Successful response",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/HelloResponse"
                }
              }
            }
          }
        },
        "x-amazon-apigateway-integration": {
          "httpMethod": "POST",
          "responses": {},
          "type": "AWS_PROXY",
          "uri": "arn:${Token[AWS.Partition.10]}:apigateway:${Token[AWS.Region.11]}:lambda:path/2015-03-31/functions/${Token[TOKEN.412]}/invocations"
        }
      },
      "options": {
        "x-amazon-apigateway-integration": {
          "httpMethod": "POST",
          "responses": {
            "default": {
              "statusCode": "204",
              "responseParameters": {
                "method.response.header.Access-Control-Allow-Methods": "*",
                "method.response.header.Access-Control-Allow-Headers": "*"
              },
              "responseTemplates": {
                "application/json": "$input.json(\"$\")\n#set($domains = [__DOMAINS__])\n#set($origin = $input.params(\"origin\"))\n#if($domains.size()==0)\n#set($context.responseOverride.header.Access-Control-Allow-Origin=\"*\")\n#elseif($domains.contains($origin))\n#set($context.responseOverride.header.Access-Control-Allow-Origin=\"$origin\")\n#end"
              }
            }
          },
          "type": "MOCK"
        },
        "type": "CORS"
      }
    }
  },
  "components": {
    "schemas": {
      "HelloResponse": {
        "description": "Response body",
        "type": "object",
        "properties": {
          "message": {
            "type": "string",
            "description": "Greeting",
            "example": "Hello World!"
          }
        }
      }
    }
  }
}
aripalo commented 2 years ago

Hi @hutchy2570 and thank you very much for this contribution! 🤩 And sorry for the delay in response (July & early August are common vacation times here in Finland so we're pretty much AFK that time 😅 ).

I'll take a look at your another issue #24 next week!