Class ApiDefinition
Represents an OpenAPI definition asset.
Namespace: Amazon.CDK.AWS.APIGateway
Assembly: Amazon.CDK.Lib.dll
Syntax (csharp)
public abstract class ApiDefinition : DeputyBase
Syntax (vb)
Public MustInherit Class ApiDefinition Inherits DeputyBase
Remarks
ExampleMetadata: infused
Examples
var myApiDefinition = ApiDefinition.FromAsset("path-to-file.json");
var specRestApi = new SpecRestApi(this, "my-specrest-api", new SpecRestApiProps {
Deploy = false,
ApiDefinition = myApiDefinition
});
// Use `stageName` to deploy to an existing stage
var deployment = new Deployment(this, "my-deployment", new DeploymentProps {
Api = specRestApi,
StageName = "dev",
RetainDeployments = true
});
// Trigger a new deployment on OpenAPI definition updates
deployment.AddToLogicalId(myApiDefinition);
Synopsis
Constructors
ApiDefinition() | Represents an OpenAPI definition asset. |
Methods
Bind(Construct) | Called when the specification is initialized to allow this object to bind to the stack, add resources and have fun. |
BindAfterCreate(Construct, IRestApi) | Called after the CFN RestApi resource has been created to allow the Api Definition to bind to it. |
FromAsset(string, IAssetOptions?) | Loads the API specification from a local disk asset. |
FromBucket(IBucket, string, string?) | Creates an API definition from a specification file in an S3 bucket. |
FromInline(object) | Create an API definition from an inline object. |
Constructors
ApiDefinition()
Represents an OpenAPI definition asset.
protected ApiDefinition()
Remarks
ExampleMetadata: infused
Examples
var myApiDefinition = ApiDefinition.FromAsset("path-to-file.json");
var specRestApi = new SpecRestApi(this, "my-specrest-api", new SpecRestApiProps {
Deploy = false,
ApiDefinition = myApiDefinition
});
// Use `stageName` to deploy to an existing stage
var deployment = new Deployment(this, "my-deployment", new DeploymentProps {
Api = specRestApi,
StageName = "dev",
RetainDeployments = true
});
// Trigger a new deployment on OpenAPI definition updates
deployment.AddToLogicalId(myApiDefinition);
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
Remarks
ExampleMetadata: infused
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
Remarks
ExampleMetadata: infused
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
Returns
Remarks
ExampleMetadata: infused
FromInline(object)
Create an API definition from an inline object.
public static InlineApiDefinition FromInline(object definition)
Parameters
- definition object
Returns
Remarks
The inline object must follow the schema of OpenAPI 2.0 or OpenAPI 3.0
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"
}
}
} }
});