aws-cdk-lib.aws_apigateway.Model

class Model (construct)

LanguageType name
.NETAmazon.CDK.AWS.APIGateway.Model
Gogithub.com/aws/aws-cdk-go/awscdk/v2/awsapigateway#Model
Javasoftware.amazon.awscdk.services.apigateway.Model
Pythonaws_cdk.aws_apigateway.Model
TypeScript (source)aws-cdk-lib » aws_apigateway » Model

Implements IConstruct, IDependable, IResource, IModel

Example

declare const api: apigateway.RestApi;

// We define the JSON Schema for the transformed valid response
const responseModel = api.addModel('ResponseModel', {
  contentType: 'application/json',
  modelName: 'ResponseModel',
  schema: {
    schema: apigateway.JsonSchemaVersion.DRAFT4,
    title: 'pollResponse',
    type: apigateway.JsonSchemaType.OBJECT,
    properties: {
      state: { type: apigateway.JsonSchemaType.STRING },
      greeting: { type: apigateway.JsonSchemaType.STRING }
    }
  }
});

// We define the JSON Schema for the transformed error response
const errorResponseModel = api.addModel('ErrorResponseModel', {
  contentType: 'application/json',
  modelName: 'ErrorResponseModel',
  schema: {
    schema: apigateway.JsonSchemaVersion.DRAFT4,
    title: 'errorResponse',
    type: apigateway.JsonSchemaType.OBJECT,
    properties: {
      state: { type: apigateway.JsonSchemaType.STRING },
      message: { type: apigateway.JsonSchemaType.STRING }
    }
  }
});

Initializer

new Model(scope: Construct, id: string, props: ModelProps)

Parameters

  • scope Construct
  • id string
  • props ModelProps

Construct Props

NameTypeDescription
restApiIRestApiThe rest API that this model is part of.
schemaJsonSchemaThe schema to use to transform data to one or more output formats.
contentType?stringThe content type for the model.
description?stringA description that identifies this model.
modelName?stringA name for the model.

restApi

Type: IRestApi

The rest API that this model is part of.

The reason we need the RestApi object itself and not just the ID is because the model is being tracked by the top-level RestApi object for the purpose of calculating it's hash to determine the ID of the deployment. This allows us to automatically update the deployment when the model of the REST API changes.


schema

Type: JsonSchema

The schema to use to transform data to one or more output formats.

Specify null ({}) if you don't want to specify a schema.


contentType?

Type: string (optional, default: 'application/json')

The content type for the model.

You can also force a content type in the request or response model mapping.


description?

Type: string (optional, default: None)

A description that identifies this model.


modelName?

Type: string (optional, default: If you don't specify a name, AWS CloudFormation generates a unique physical ID and uses that ID for the model name. For more information, see Name Type.)

A name for the model.

Important If you specify a name, you cannot perform updates that require replacement of this resource. You can perform updates that require no or some interruption. If you must replace the resource, specify a new name.

Properties

NameTypeDescription
envResourceEnvironmentThe environment this resource belongs to.
modelIdstringReturns the model name, such as 'myModel'.
nodeNodeThe tree node.
stackStackThe stack in which this resource is defined.
static EMPTY_MODELIModelRepresents a reference to a REST API's Empty model, which is available as part of the model collection by default.
static ERROR_MODELIModelRepresents a reference to a REST API's Error model, which is available as part of the model collection by default.

env

Type: ResourceEnvironment

The environment this resource belongs to.

For resources that are created and managed by the CDK (generally, those created by creating new class instances like Role, Bucket, etc.), this is always the same as the environment of the stack they belong to; however, for imported resources (those obtained from static methods like fromRoleArn, fromBucketName, etc.), that might be different than the stack they were imported into.


modelId

Type: string

Returns the model name, such as 'myModel'.


node

Type: Node

The tree node.


stack

Type: Stack

The stack in which this resource is defined.


static EMPTY_MODEL

Type: IModel

Represents a reference to a REST API's Empty model, which is available as part of the model collection by default.

This can be used for mapping JSON responses from an integration to what is returned to a client, where strong typing is not required. In the absence of any defined model, the Empty model will be used to return the response payload unmapped.

Definition { "$schema" : "http://json-schema.org/draft-04/schema#", "title" : "Empty Schema", "type" : "object" }

See also: https://docs.amazonaws.cn/en_us/apigateway/latest/developerguide/models-mappings.html#models-mappings-models


static ERROR_MODEL

Type: IModel

Represents a reference to a REST API's Error model, which is available as part of the model collection by default.

This can be used for mapping error JSON responses from an integration to a client, where a simple generic message field is sufficient to map and return an error payload.

Definition { "$schema" : "http://json-schema.org/draft-04/schema#", "title" : "Error Schema", "type" : "object", "properties" : { "message" : { "type" : "string" } } }

Methods

NameDescription
applyRemovalPolicy(policy)Apply the given removal policy to this resource.
toString()Returns a string representation of this construct.
static fromModelName(scope, id, modelName)

applyRemovalPolicy(policy)

public applyRemovalPolicy(policy: RemovalPolicy): void

Parameters

  • policy RemovalPolicy

Apply the given removal policy to this resource.

The Removal Policy controls what happens to this resource when it stops being managed by CloudFormation, either because you've removed it from the CDK application or because you've made a change that requires the resource to be replaced.

The resource can be deleted (RemovalPolicy.DESTROY), or left in your AWS account for data recovery and cleanup later (RemovalPolicy.RETAIN).


toString()

public toString(): string

Returns

  • string

Returns a string representation of this construct.


static fromModelName(scope, id, modelName)

public static fromModelName(scope: Construct, id: string, modelName: string): IModel

Parameters

  • scope Construct
  • id string
  • modelName string

Returns

  • IModel