Skip to content

Swashbuckle.Swagger.Extra can be used for easy integration of Swagger’s Json file to AWS Web API Gateway

License

Notifications You must be signed in to change notification settings

VikasChaturvedi87/Swashbuckle.Swagger.Extra

Repository files navigation

Swagger.Plugin

Swagger.Plugin is a OperationFilter for Swagger. It can easily modify Swagger Json file to have x-amazon-apigateway-integration and your Swagger Json file is ready to import into AWS API Gateway.

Dependencies

How to use

Install from Nuget, use below command to get reference

Install-Package Swagger.Plugin -Version 1.0.2

Open SwaggerConfig.cs file in ~/App_Start Use below code to have x-amazon-apigateway-integration in swagger Json file.

	GlobalConfiguration.Configuration 
                .EnableSwagger(c =>
                    {
	                  c.OperationFilter<ApiGatewayIntegrationFilter>();  
                    });
  

Integration Settings

You can create object of ApiGatewayIntegrationSettings class to apply configure Request | Response headers, Method Request, Method Response, parameters and query strings.

You can also add extra response codes which are not mentioned on you webapi action methods.

	// For example you have to put 409 response for all you API you need not to 
	// modify you code jut use below setting and this will add into your swagger Json file
	var integrationSettings = new ApiGatewayIntegrationSettings();
	integrationSettings.AddResponse("409", new Swashbuckle.Swagger.Response());
	
	GlobalConfiguration.Configuration 
                .EnableSwagger(c =>
                    {
					  ApiGatewayIntegrationFilter.IntegrationSettings = integrationSettings;                    
	                  c.OperationFilter<ApiGatewayIntegrationFilter>();  
                    });

Your Swagger file will look like this.

"paths": {
	"/api/Account/UserInfo": {
		"get": {
			"tags": ["Account"],
			"operationId": "Account_GetUserInfo",
			"consumes": [],
			"produces": ["application/json",
			"text/json",
			"application/xml",
			"text/xml"],
			"parameters": [],
			"responses": {
				"200": {
					"description": "200",
					"schema": {
						"$ref": "#/definitions/UserInfoViewModel"
					},
					"headers": {
						
					}
				},
				"409": {
					"description": "409",
					"headers": {
						
					}
				}
			},
			"x-amazon-apigateway-integration": {
				"type": "http",
				"httpMethod": "GET",
				"uri": "http: //localhost/api/Account/UserInfo",
				"responses": {
					"200": {
						"statusCode": "200",
						"responseParameters": {
							
						}
					},
					"409": {
						"statusCode": "409",
						"responseParameters": {
							
						}
					},
					"default": {
						"statusCode": "200",
						"responseParameters": {
							
						}
					}
				},
				"requestParameters": {
					
				}
			}
		}
	}

Adding common parameters(Header,QueryString, etc.) by using below code.

ApiGatewayIntegrationFilter.IntegrationSettings.AddGlobalActionParameter(new Parameter() {
                            name = "x-api-your-custom-header",
                            @in = "header",
                            type = "string",
                            required = false
                        });
"x-amazon-apigateway-integration": {
	"type": "http",
	"httpMethod": "GET",
	"uri": "http://localhost/api/Account/UserInfo",
	"responses": {
		"200": {
			"statusCode": "200",
			"responseParameters": {
				
			}
		},
		"409": {
			"statusCode": "409",
			"responseParameters": {
				
			}
		},
		"default": {
			"statusCode": "200",
			"responseParameters": {
				
			}
		}
	},
	"requestParameters": {
		"integration.request.header.Authorization": "method.request.header.Authorization",
		"integration.request.header.x-api-your-custom-header": "method.request.header.x-api-your-custom-header"
	}
}

Access control settings can be enabled by below code.

ApiGatewayIntegrationFilter.IntegrationSettings
                            .AccessControlSettings()
                            .AllowHeadersWtihValue("Content-Type,X-Amz-Date,Authorization,x-api-custom,X-Api-Key")
                            .AllowMethodWtihValue("*")
                            .AllowOrigenWtihValue("*");

Now you swagger file x-amazon-apigateway-integration property should look like this.

"x-amazon-apigateway-integration": {
	"type": "http",
	"httpMethod": "GET",
	"uri": "http://localhost/api/Account/UserInfo",
	"responses": {
		"200": {
			"statusCode": "200",
			"responseParameters": {
				"method.response.header.Access-Control-Allow-Headers": "'Content-Type,X-Amz-Date,Authorization,x-api-custom,X-Api-Key'",
				"method.response.header.Access-Control-Allow-Methods": "'*'",
				"method.response.header.Access-Control-Allow-Origin": "'*'"
			}
		},
		"409": {
			"statusCode": "409",
			"responseParameters": {
				"method.response.header.Access-Control-Allow-Headers": "'Content-Type,X-Amz-Date,Authorization,x-api-custom,X-Api-Key'",
				"method.response.header.Access-Control-Allow-Methods": "'*'",
				"method.response.header.Access-Control-Allow-Origin": "'*'"
			}
		},
		"default": {
			"statusCode": "200",
			"responseParameters": {
				"method.response.header.Access-Control-Allow-Headers": "'Content-Type,X-Amz-Date,Authorization,x-api-custom,X-Api-Key'",
				"method.response.header.Access-Control-Allow-Methods": "'*'",
				"method.response.header.Access-Control-Allow-Origin": "'*'"
			}
		}
	},
	"requestParameters": {
		"integration.request.header.Authorization": "method.request.header.Authorization",
		"integration.request.header.x-api-your-custom-header": "method.request.header.x-api-your-custom-header"
	}
}
}

You can now save your swagger file and import it to AWS API Gateway.

About

Swashbuckle.Swagger.Extra can be used for easy integration of Swagger’s Json file to AWS Web API Gateway

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages