Hi all,
I can define a request model for REST API like this:
request:
schemas:
application/json:
name: “Info”
schema: ${file(models/rInfo.json)}
but I cannot define a response model similarly , only template definition is available.
Is that true , or I miss something ?
Thank you very much!
Okay, I found a way to do this. You can do this with extensions:
extensions:
ApiGatewayMethodMyMethodGet:
Properties:
MethodResponses:
-
StatusCode: '200'
ResponseModels:
'application/json': MyModel
You have to create your models under Resources
to reference them
resources:
Resources:
MyModel:
Type: 'AWS::ApiGateway::Model'
Properties:
Name: MyModels
RestApiId:
Ref: ApiGatewayRestApi
ContentType: application/json
Description: 'Response model for something'
Schema: ${file(src/schema/get_something.json)}
Bonus:
If you want to reference a model from another model the $ref
property has to be the full url. You can define your schema like this if you want to do this:
get_something.json:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "MyModels",
"type": "array",
"title": "MyModels",
"description": "A list of MyModels",
"items": {
"$ref": {
"Fn::Join": [
"",
[
"https://apigateway.amazonaws.com/restapis/",
{
"Ref": "ApiGatewayRestApi"
},
"/models/",
{
"Ref": "MyModel"
}
]
]
}
}
}
I hope this helps.