简体   繁体   English

AWS CDK Api 网关模型参考依赖 - Model 参考必须采用规范形式

[英]AWS CDK Api Gateway Models Ref Dependency - Model reference must be in canonical form

I am trying to add multiple models in the same time using aws CDK when one of the models is referencing the other one.当其中一个模型引用另一个模型时,我正在尝试使用 aws CDK 同时添加多个模型。 Ex:前任:

  "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"
    }
  },

and

"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"
    }
  },

When i deploy this fails with当我部署失败时

Model reference must be in canonical form

From what i can see this fails because the GenderModel does not exists.据我所知,这失败了,因为GenderModel不存在。 If i first add the GenderModel in the stack and then i add the RequirementsModel and deploy again it works just fine because the GenderModel was previously created.如果我首先在堆栈中添加GenderModel ,然后添加RequirementsModel并再次部署,它就可以正常工作,因为GenderModel是先前创建的。 If i want to create both of the models in the same time it will fail.如果我想同时创建这两个模型,它将失败。 I tried to make sure the order of addModel call is correct but it seems it does not work.我试图确保addModel调用的顺序是正确的,但它似乎不起作用。

Solution Found找到解决方案

Seems like you have to add explicitly specify the dependency.似乎您必须添加明确指定的依赖项。

modelB.node.addDependency(modelA)

This will avoid the error and add the models in the correct order这将避免错误并以正确的顺序添加模型

The problem is https://apigateway.amazonaws.com/restapis/${Token[TOKEN.791]}/models/GenderModel , specifically the ${Token[TOKEN.791]} part.问题是https://apigateway.amazonaws.com/restapis/${Token[TOKEN.791]}/models/GenderModel ,特别是${Token[TOKEN.791]}部分。 When the API is not created, at CloudFormation synthetisation time, id is not known and placeholder value used - https://docs.aws.amazon.com/cdk/latest/guide/tokens.html当 API 未创建时,在 CloudFormation 合成时,id 未知并使用占位符值 - https://docs.aws.amazon.com/cdk/latest/guide/tokens.html

You can use the Ref intristic function to compose model by the reference您可以使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM