3

I try to use swagger for describing a JSON API. So far it looks good, but I can't figure out how to use the anyOf structure for defining an array of different object types in a JSON answer.

The following JSON Schema is valid, and it should describe an array of Article and Video JSOn objects:

{
"Article":{
  "id":"Article",
  "required": ["title"],
  "properties":{
    "title":{
      "type":"string",
      "description": "Title of the article"
    }
  }
},

"Video":{
  "id":"Video",
  "required": ["title"],
  "properties":{
    "title":{
      "type":"string",
      "description": "Title of the video"
    }
  }
},

"News":{
  "id":"News",
  "required": ["instance_data"],
  "properties":{
    "instance_data":{
        "anyOf":[
           { "$ref": "Article" },
           { "$ref": "Video" } ],
      "description": "News instance data"
    }
  }
}
}

But in swagger the object type is always displayed as undefined instead of "Article or Video".

Is it possible at all in swagger to make this work?

TheEye
  • 9,280
  • 2
  • 42
  • 58
  • 1
    Swagger borrows a lot from JSON Schema, and many Swagger documents are valid JSON Schemas, but I think it's not completely compatible. Are you sure Swagger supports "anyOf"? – cloudfeet May 20 '14 at 16:49
  • No, that's why I ask ;-). But I think it's quite an essential feature to support, as it's common to have a list of elements of different types in an answer ... – TheEye May 21 '14 at 07:07
  • 1
    I agree. Why not raise an issue asking Swagger to support full JSON Schemas? Or even just use JSON Schema and [generate docs and client code](https://blog.heroku.com/archives/2014/5/20/heroku-http-api-toolchain) from that? – cloudfeet May 21 '14 at 14:28
  • Thanks for your comment, I'll have a look at that - I chose swagger first because I like the way they show the doc as web page. And I thought I might be missing something about how to use it and they might support it already. – TheEye May 22 '14 at 07:36

1 Answers1

2

Just to be clear: Swagger v2.0 does not support anyOf. Reed more here: https://github.com/swagger-api/swagger-spec/issues/57

Sergey Yarotskiy
  • 517
  • 4
  • 14