Table of Contents

Class ApiDefinition

Namespace
Amazon.CDK.AWS.APIGateway
Assembly
Amazon.CDK.AWS.APIGateway.dll

Represents an OpenAPI definition asset.

public abstract class ApiDefinition : DeputyBase
Inheritance
ApiDefinition
Derived

Examples

Integration integration;


            var api = new SpecRestApi(this, "books-api", new SpecRestApiProps {
                ApiDefinition = ApiDefinition.FromAsset("path-to-file.json")
            });

            var booksResource = api.Root.AddResource("books");
            booksResource.AddMethod("GET", integration);

Remarks

ExampleMetadata: infused

Constructors

ApiDefinition()

protected ApiDefinition()

Methods

Bind(Construct)

Called when the specification is initialized to allow this object to bind to the stack, add resources and have fun.

public abstract IApiDefinitionConfig Bind(Construct scope)

Parameters

scope Construct

The binding scope.

Returns

IApiDefinitionConfig

BindAfterCreate(Construct, IRestApi)

Called after the CFN RestApi resource has been created to allow the Api Definition to bind to it.

public virtual void BindAfterCreate(Construct scope, IRestApi restApi)

Parameters

scope Construct
restApi IRestApi

Remarks

Specifically it's required to allow assets to add metadata for tooling like SAM CLI to be able to find their origins.

FromAsset(string, IAssetOptions?)

Loads the API specification from a local disk asset.

public static AssetApiDefinition FromAsset(string file, IAssetOptions? options = null)

Parameters

file string
options IAssetOptions

Returns

AssetApiDefinition

FromBucket(IBucket, string, string?)

Creates an API definition from a specification file in an S3 bucket.

public static S3ApiDefinition FromBucket(IBucket bucket, string key, string? objectVersion = null)

Parameters

bucket IBucket
key string
objectVersion string

Returns

S3ApiDefinition

FromInline(object)

Create an API definition from an inline object.

public static InlineApiDefinition FromInline(object definition)

Parameters

definition object

Returns

InlineApiDefinition

Examples

ApiDefinition.FromInline(new Dictionary<string, object> {
                { "openapi", "3.0.2" },
                { "paths", new Struct {
                    /pets = new Struct {
                        Get = new Struct {
                            Responses = new Struct {
                                200 = new Struct {
                                    Content = new Struct {
                                        Application/json = new Struct {
                                            Schema = new Struct {
                                                $ref = "#/components/schemas/Empty"
                                            }
                                        }
                                    }
                                }
                            },
                            X-amazon-apigateway-integration = new Struct {
                                Responses = new Struct {
                                    Default = new Struct {
                                        StatusCode = "200"
                                    }
                                },
                                RequestTemplates = new Struct {
                                    Application/json = "{\"statusCode\": 200}"
                                },
                                PassthroughBehavior = "when_no_match",
                                Type = "mock"
                            }
                        }
                    }
                } },
                { "components", new Struct {
                    Schemas = new Struct {
                        Empty = new Struct {
                            Title = "Empty Schema",
                            Type = "object"
                        }
                    }
                } }
            });

Remarks

The inline object must follow the schema of OpenAPI 2.0 or OpenAPI 3.0