How to handle choice in Swagger 2.0

1,592 views
Skip to first unread message

Nandigam Anil

unread,
Oct 29, 2015, 2:33:28 AM10/29/15
to Swagger
"NetworkResource": {
            "description": "SIM, mobile number, or IMEI used by the customer to gain access to services.",
            "type": "object",
            "properties": {
                "imei": {
                    "maxLength": 15,
                    "minLength": 15,
                    "type": "string",
                    "pattern": "[0-9]{15}"
                },
                "sim": {
                    "$ref": "#/definitions/SIM"
                },
                "mobileNumber": {
                    "$ref": "#/definitions/MobileNumber"
                },
                "resourceStatus": {
                    "type": "array",
                    "items": {
                        "$ref": "#/definitions/ResourceStatus"
                    }
                }
            }
        }
In the above Object I need to handle choice bit ween "mobileNumber" , "sim".
How we will represent in swagger 2.0. Can you please help on this.

Ron

unread,
Dec 7, 2015, 3:12:55 PM12/7/15
to Swagger
Hi there,

Apologies for the delayed reply.

If you're looking to describe that you can have one or the other but not both, that's something that can't be done. You also can't specify that at least one of the two needs to be available (a conditional requirement).
You can use inheritance to say that one model has a sim and the other a mobile number, but in order to do that you need an additional field to differentiate the two objects which you do not seem to have.

Guillermo Martínez del Camino

unread,
Nov 10, 2016, 5:15:11 AM11/10/16
to Swagger
I am rescuing this topic as I believe adding choice would be great for the swagger spec. We have got, for instance, an IP that queries billing events. Such events could be calls, SMSs, data traffic, or digital assets such as a movie download. They have a common part (the ID, time, etc.) and a specific part depending on the event type. We are having trouble documenting it as Swagger today cannot cope with the requirement. My proposal would be to add it this way:

"properties": [{

    ...

    "element": {
        "type": "choice",
        "choices": [{
            "$ref": "#/definitions/possibleType1"
        },
        {
            "$ref": "#/definitions/possibleType2"
        },
        {
            "type": "string"
        }]
    }

...

}]

Same way an array has its "items" field or a string has its "enum" or format field, choice would have an array of possible choices which would contain either a type or a reference.

Ron Ratovsky

unread,
Nov 10, 2016, 11:26:47 AM11/10/16
to swagger-sw...@googlegroups.com

The next version of the spec (not released yet) already has support for this.

--
You received this message because you are subscribed to the Google Groups "Swagger" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Message has been deleted

Guillermo Martínez del Camino

unread,
Nov 11, 2016, 8:01:06 AM11/11/16
to Swagger
That's great news!

rkushnir

unread,
Nov 29, 2016, 6:56:22 AM11/29/16
to Swagger
Hi, when should the next version be released? Do you know already?

Thanks

To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggersocket+unsub...@googlegroups.com.

tony tam

unread,
Nov 29, 2016, 12:02:14 PM11/29/16
to swagger-sw...@googlegroups.com
Sometime in the new year.  Please follow https://twitter.com/OpenApiSpec for notifications on it.

To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.

Pedro Maia

unread,
Mar 26, 2018, 9:54:34 AM3/26/18
to Swagger
Reply all
Reply to author
Forward
0 new messages