'How to use NSWAG to generate Options request with CORS header response for APIGateway
I am developing a C# REST API and I am using NSAG to generate OpenApi 3.0 doc which I can import to AWS APIGateway. Now to enable CORS, I need to add OPTIONS endpoint configuration as described in aws documentation here: https://docs.aws.amazon.com/apigateway/latest/developerguide/enable-cors-for-resource-using-swagger-importer-tool.html I am able to add the below configuration by hand into the generated specification and when I import into AWS APIGateway api, it works. But I am not sure how to automatically generate this configuration.
"options" : {
"produces" : [ "application/json" ],
"responses" : {
"200" : {
"description" : "200 response",
"headers" : {
"Access-Control-Allow-Origin" : {
"type" : "string"
},
"Access-Control-Allow-Methods" : {
"type" : "string"
},
"Access-Control-Allow-Headers" : {
"type" : "string"
}
}
}
},
"x-amazon-apigateway-integration" : {
"type" : "mock",
"responses" : {
"default" : {
"statusCode" : "200",
"responseParameters" : {
"method.response.header.Access-Control-Allow-Methods" : "'*'",
"method.response.header.Access-Control-Allow-Headers" : "'Content-Type,X-Amz-Date,Authorization'",
"method.response.header.Access-Control-Allow-Origin" : "'*'"
},
"responseTemplates" : {
"application/json" : "statusCode: 200"
}
}
},
"requestTemplates" : {
"application/json" : "{\"statusCode\": 200}"
},
"passthroughBehavior" : "when_no_match"
}
}
},
The stripped down version of current generated specification is here:
{
"x-generator": "NSwag v13.15.10.0 (NJsonSchema v10.6.10.0 (Newtonsoft.Json v13.0.0.0))",
"openapi": "3.0.0",
"info": {
"title": "My API",
"description": "My API",
"version": "1.0.0"
},
"servers": [
{
"url": "https://my.dummy.url"
}
],
"paths": {
"/Contacts": {
"get": {
"tags": [
"Contacts"
],
"description": "Gets list of Contacts the current user has access to.",
"operationId": "Contacts_GetContacts",
"parameters": [
{
"name": "skip",
"in": "query",
"required": true,
"schema": {
"type": "integer",
"format": "int32",
"default": 0
},
"x-position": 1
},
{
"name": "take",
"in": "query",
"required": true,
"schema": {
"type": "integer",
"format": "int32",
"default": 100
},
"x-position": 2
}
],
"responses": {
"200": {
"description": "",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ContactList"
}
}
}
}
},
"security": [
{
"jwt-rsa-custom-authorizer": []
}
],
"x-amazon-apigateway-integration": {
"connectionId": "[APIGATEWAY_VPCLINK_ID]",
"httpMethod": "GET",
"uri": "[MYAPI_HOST]/api/publicapi-Contacts",
"responses": {
"default": {
"statusCode": "200"
}
},
"passthroughBehavior": "when_no_match",
"connectionType": "VPC_LINK",
"type": "http_proxy",
"cacheKeyParameters": [
"method.request.querystring.skip",
"method.request.querystring.take"
]
}
}
},
},
"components": {
"schemas": {
"ContactList": {
"allOf": [
{
"$ref": "#/components/schemas/PaginatedListOfContact"
},
{
"type": "object",
"additionalProperties": false
}
]
},
"PaginatedListOfContact": {
"allOf": [
{
"$ref": "#/components/schemas/ListBaseOfContact"
},
{
"type": "object",
"additionalProperties": false,
"properties": {
"TotalRecords": {
"type": "integer",
"format": "int32"
},
"Skip": {
"type": "integer",
"format": "int32"
},
"Take": {
"type": "integer",
"format": "int32"
},
"RecordsReturned": {
"type": "integer",
"format": "int32"
}
}
}
]
},
"ListBaseOfContact": {
"type": "object",
"additionalProperties": false,
"properties": {
"Data": {
"type": "array",
"nullable": true,
"items": {
"$ref": "#/components/schemas/Contact"
}
}
}
},
"Contact": {
"type": "object",
"additionalProperties": false,
"properties": {
"ContactKey": {
"type": "string",
"nullable": true
},
"FullName": {
"type": "string",
"nullable": true
},
"WorkPhoneNumber": {
"type": "string",
"nullable": true
},
"MobilePhoneNumber": {
"type": "string",
"nullable": true
},
"EmailAddress": {
"type": "string",
"nullable": true
},
"CalendarUrl": {
"type": "string",
"nullable": true
},
"CompanyName": {
"type": "string",
"nullable": true
},
"CompanyLogoUrl": {
"type": "string",
"nullable": true
},
}
}
},
"securitySchemes": {
"jwt-rsa-custom-authorizer": {
"type": "apiKey",
"name": "Authorization",
"in": "header",
"x-amazon-apigateway-authtype": "custom",
"x-amazon-apigateway-authorizer": {
"authorizerUri": "[AUTHORIZER_URI]",
"authorizerCredentials": "[AUTHORIZER_CREDENTIALS]",
"authorizerResultTtlInSeconds": "[AUTHORIZER_RESULT_TTL_IN_SECONDS]",
"identityValidationExpression": "^Bearer [-0-9a-zA-z\\.]*$",
"type": "token"
}
}
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
