[英]AWS CDK Api Gateway Models Ref Dependency - Model reference must be in canonical form
當其中一個模型引用另一個模型時,我正在嘗試使用 aws CDK 同時添加多個模型。 前任:
"Gender": {
"contentType": "application/json",
"modelName": "GenderModel",
"schema": {
"type": "string",
"enum": [
"Not Specified",
"Male",
"Female",
"Non-Binary"
],
"schema": "http://json-schema.org/draft-04/schema#",
"title": "GenderModel"
}
},
和
"Requirements": {
"contentType": "application/json",
"modelName": "RequirementsModel",
"schema": {
"type": "object",
"properties": {
"gender": {
"ref": "https://apigateway.amazonaws.com/restapis/${Token[TOKEN.791]}/models/GenderModel"
}
},
"required": [
"gender",
],
"additionalProperties": false,
"schema": "http://json-schema.org/draft-04/schema#",
"title": "RequirementsModel"
}
},
當我部署失敗時
Model reference must be in canonical form
據我所知,這失敗了,因為GenderModel
不存在。 如果我首先在堆棧中添加GenderModel
,然后添加RequirementsModel
並再次部署,它就可以正常工作,因為GenderModel
是先前創建的。 如果我想同時創建這兩個模型,它將失敗。 我試圖確保addModel
調用的順序是正確的,但它似乎不起作用。
找到解決方案
似乎您必須添加明確指定的依賴項。
modelB.node.addDependency(modelA)
這將避免錯誤並以正確的順序添加模型
問題是https://apigateway.amazonaws.com/restapis/${Token[TOKEN.791]}/models/GenderModel
,特別是${Token[TOKEN.791]}
部分。 當 API 未創建時,在 CloudFormation 合成時,id 未知並使用占位符值 - https://docs.aws.amazon.com/cdk/latest/guide/tokens.html
您可以使用Ref
intristic function 通過引用組成 model
const getModelRef = (api: RestApi, model: Model): string =>
Fn.join(
'',
['https://apigateway.amazonaws.com/restapis/',
api.restApiId,
'/models/',
model.modelId]);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.