How to model an array of different types of object

8,503 views
Skip to first unread message

Jeff Bannister

unread,
Nov 28, 2014, 11:33:18 AM11/28/14
to swagger-sw...@googlegroups.com
Hi,

I have a use case where I want to describe using Swagger 2.0 a callback API. This API will POST an array of event objects to the registered callback URLs. Each one of these objects will only have a type name in common e.g. typeA, typeB etc.

How would I define this in Swagger?

I've tried the following….

  Events:
    type: array
    minitems: 1
    maxitems: 100
    items:
      allOf:
        - $ref: OfferEvent
        - $ref: ReferAFriendEvent

For the example above the Swagger editor just displays an empty array.

Also I feel like a should be using anyOf not allOf (part of the JSON 4 schema - http://json-schema.org/latest/json-schema-validation.html#anchor82). Is there any reason why Swagger doesn't support anyOf - I am supposed to use the discriminator field instead?

Regards,

Jeff.

Ron

unread,
Nov 28, 2014, 11:37:48 AM11/28/14
to swagger-sw...@googlegroups.com
Swagger doesn't support non-deterministic APIs, which is why 'anyOf' is not supported.

However, as long as you have one field that's common between the models, you can use model inheritance.

Define the top model with the type discriminator and the sub models would inherit from it using `allOf`

You can look at this example - https://github.com/swagger-api/swagger-spec/blob/master/fixtures/v2.0/json/models/modelWithComposition.json.

Hope this helps.

--
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.

Jeff Bannister

unread,
Nov 28, 2014, 12:26:28 PM11/28/14
to swagger-sw...@googlegroups.com
Thanks Ron for the prompt reply...

Based on the link and your description I ended up with....

  Events:
    type
: array
    minitems
: 1
    maxitems
: 100
    items
:

      $ref
: Event
     
 
Event:
    discriminator
: eventname
    properties
:
      eventname
:
        type
: string
    required
:
     
- eventname    
       
 
ReferAFriendEvent:
    allOf
:
     
- $ref: Event
    properties
:
      accountId
:
        type
: integer
      campaignName
:
        type
: string

 
OfferEvent:
    allOf
:
     
- $ref: Event
    properties
:
      accountId
:
        type
: integer
      offerName
:
        type
: string

However the model is not displayed correctly in the swagger editor. For example if I expand the ReferAFriendEvent, the eventname property which should have been inherited from the parent object 'Event' is missing. Also not sure whether I have to abandon trying to model a POST request that can send an array of 1..* Event A, 1..* Event B as this would be be non-deterministic.


On Friday, 28 November 2014 16:37:48 UTC, Ron R wrote:
Swagger doesn't support non-deterministic APIs, which is why 'anyOf' is not supported.

However, as long as you have one field that's common between the models, you can use model inheritance.

Define the top model with the type discriminator and the sub models would inherit from it using `allOf`

You can look at this example - https://github.com/swagger-api/swagger-spec/blob/master/fixtures/v2.0/json/models/modelWithComposition.json.

Hope this helps.
On 28 November 2014 at 18:33, Jeff Bannister wrote:
Hi,

I have a use case where I want to describe using Swagger 2.0 a callback API. This API will POST an array of event objects to the registered callback URLs. Each one of these objects will only have a type name in common e.g. typeA, typeB etc.

How would I define this in Swagger?

I've tried the following….

  Events:
    type: array
    minitems: 1
    maxitems: 100
    items:
      allOf:
        - $ref: OfferEvent
        - $ref: ReferAFriendEvent

For the example above the Swagger editor just displays an empty array.

Also I feel like a should be using anyOf not allOf (part of the JSON 4 schema - http://json-schema.org/latest/json-schema-validation.html#anchor82). Is there any reason why Swagger doesn't support anyOf - I am supposed to use the discriminator field instead?

Regards,

Jeff.

--
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-swaggersocket+unsub...@googlegroups.com.

Ron

unread,
Nov 28, 2014, 12:41:45 PM11/28/14
to swagger-sw...@googlegroups.com
It's quite possible we haven't implemented support for inheritance on the editor yet, so feel free to open an issue about it.

If you need to describe the ability to send an array of models, but they all have to be the same, then yes, it can be an issue.

I admit that with regards to an API-design, I'm not sure why you're looking to impose it, but that's not really any of my business :)

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

Ted Epstein

unread,
Nov 29, 2014, 6:44:32 AM11/29/14
to swagger-sw...@googlegroups.com
Jeff,

Instead of modeling events as a type hierarchy, you could model collection<event> as the the type hierarchy.  So your abstract supertype would be EventCollection, with an eventType discriminator.  And your subtypes would be ReferAFriendEventCollection, OfferEventCollection, etc.  

You might even be able to do this in combination with the event hierarchy you already modeled, make EventCollection a container of abstract Events, and model the event subtype constraint into your collection subtypes.  I don't know JSON Schema well enough to say whether and how you could do this, but in any case you may not need that level of specificity in your model.

Cheers,

Ted Epstein |   |      skype:ted.epstein

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

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages