9

Say I have a Model:

"Pet":{
  "type": "object"
  "properties": {
    "name":{"type":"integer"},
    "age":{"type":"integer"}
  }
}

And another model:

"Human":{
  "type": "object"
  "properties": {
    "name":{"type":"integer"},
    "age":{"type":"integer"},
    "pets":{
      "type":"array"
      "items": {
        <This is where my question is>
      }
    }
  }
}

How can I reference the Pet model in my human model?

With swagger I was able to say:

"$ref": "#/definitions/Pet"

but API Gateway seems to not allow it.

2 Answers2

11

If you mean reference model outside swagger, you can do that by specifying the model with an absolute url like below

 {"type":"array","items":{"$ref":"https://apigateway.amazonaws.com/restapis/<rest_api_id>/models/Pet"}}

For swagger, this example from open api specification shows how to reference models within swagger - https://github.com/OAI/OpenAPI-Specification/blob/master/examples/v2.0/json/petstore.json

"Pets": {
  "type": "array",
  "items": {
    "$ref": "#/definitions/Pet"
  }

Note that api gateway does not support 'default' response, so if you are trying to import the above petstore.json example, you need to remove the "default" fields.

Abhigna Nagaraja
  • 1,874
  • 15
  • 17
  • 1
    This is the first post that actually notes that AWS is an external service while swagger routes are internal.. finally!!!! you got my upvote :) – ymz Oct 30 '17 at 16:28
  • The unsupported OpenAPI 3.0 features go past the `default` - https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-known-issues.html#api-gateway-known-issues-rest-apis – Yuri Apr 11 '19 at 11:18
  • Just for the sake of the link, the answer is what https://forums.aws.amazon.com/thread.jspa?messageID=697388 mentions. – zavr Apr 10 '20 at 17:45
2

If you just want single values (not arrays), this works:

..
"properties": {
            "id": {
                "$ref": "https://apigateway.amazonaws.com/restapis/abcd1234/models/UserId"
            },
..

(It would be nice if this could be a relative URL rather than absolute, but I haven't found any mention of how to do this yet)

Jim ReesPotter
  • 445
  • 1
  • 3
  • 10