Do Swagger models support oneOf?

748 views
Skip to first unread message

David Lee

unread,
Mar 28, 2013, 1:13:20 PM3/28/13
to wordn...@googlegroups.com
I couldn't find a dedicated Swagger ML, so I hope it's okay to post here. Everyone else seems to :-)

I have a situation where the type of a field in my Swagger model may very depending on the message. Based on the examples at json-schema.org, I think it should look something like this (where the type of Obj.varies could be one of Foo, Bar or Bam):

    "models": {
        "Foo": { "id": "Foo", "properties": { ... } },
        "Bar": { "id": "Bar", "properties": { ... } },
        "Bam": { "id": "Bam", "properties": { ... } },
        "Obj": { "id": "Obj",
            "properties": {
                ...
                "varies": {
                    "type": "object",
                    "oneOf": [
       { "$ref": "Foo" },
       { "$ref": "Bar" },
       { "$ref": "Bam" }
                    ]
                }
            }
        }
    }

Of course, this isn't working. Is this sort of thing supported by Swagger's models? If not, is there something similar, like inheritance or discriminated unions?

Thanks!
-- 
David M. Lee
Digium, Inc. | Software Developer
445 Jan Davis Drive NW - Huntsville, AL 35806 - USA
Check us out at:  www.digium.com  & www.asterisk.org

Tony Tam

unread,
Mar 28, 2013, 3:20:42 PM3/28/13
to wordn...@googlegroups.com
Hi David, currently swagger expects the response model to be consistent.  Most folks are working inside that contract by modeling the response model as a superset of the fields of possible response variants.  Is that an option for you?

David Lee

unread,
Mar 28, 2013, 3:48:24 PM3/28/13
to wordn...@googlegroups.com
Not really; in this case it's a catch-all 'event', which could vary greatly. There would be lots of shared fields, and lots of unique fields. That would make documenting which fields you should expect with each event a bit overwhelming.

The best alternative that I've come up with is to have each type listed as a different field, documenting that exactly one will be provided. Thoughts on this approach?

"Obj": { "id": "Obj",
    "properties": {
        "foo": { "type": "Foo" },
        "bar": { "type": "Bar" },
        "bam": { "type": "Bam" }
    }
}

--
dave

Tony Tam

unread,
Mar 28, 2013, 3:57:36 PM3/28/13
to wordn...@googlegroups.com
got it.  Yes, that approach works just fine, and will work in codegen, etc.
Reply all
Reply to author
Forward
0 new messages