Swagger 2.0: 'Definitions'(Or Model in 1.2) inheritance

6,195 views
Skip to first unread message

mission liao

unread,
Oct 27, 2014, 4:07:18 AM10/27/14
to swagger-sw...@googlegroups.com
Hi,

   Just want to confirm that if model inheritance is still undefined in swagger 2.0.
TODO: Add explanation about composition and inheritance in the new spec.

   We have 'discriminator' but have nothing compared to 'subTypes'.

Ron

unread,
Oct 27, 2014, 5:25:07 AM10/27/14
to swagger-sw...@googlegroups.com
It is defined, I just need to expand on the documentation.

For a quick overview - there are two.. let's say "types" of inheritance with Swagger 2.0.

The first is composition using the "allOf" keyword - you can say that a model contains the properties of other models (more than one).

The second is polymorphism where the "discriminator" can be used on top of the "allOf" property.
Just like in 1.2, the "discriminator" must be defined at the top level model and it must be required.

Unlike 1.2, there's no "subTypes". Any model that includes the super mode in the "allOf" definition will be considered a sub type of it.
So the super model is unaware of its children, but now the child is aware of its parent.

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

mission liao

unread,
Oct 27, 2014, 7:29:06 AM10/27/14
to swagger-sw...@googlegroups.com
Hi Ron,

   I would like to compose some example to make sure what I understood is spec compliant.
   For 'composition':
{
“User”: {
properties: {
“id”: { “type”: “string” },
“email”: { “type”: “string” }
}
},
“UserCredential”: {
properties: {
“password”: { “type”: “string” }
},
allOf : [
{ “$ref”: “#/definitions/User” }
]
}
}

 For inheritance:
{
“Pet” : {
properties: {
“pet_type”: { “type”: “string” }
},
discriminator: “pet_type”,
required: [“pet_type”]
},
“Dog”: {
allOf: [
{ “$ref”: “#/definitions/Pet” }
]
}
When an operation accepting Pet as a parameter, we can pass a Dog(with pet_type=Dog) instead.
Am I on the right track? Thanks for any advice.
To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggersocket+unsub...@googlegroups.com.

Ron

unread,
Oct 27, 2014, 7:35:37 AM10/27/14
to swagger-sw...@googlegroups.com
Close, but not quite.

Ignoring the name of the sample - https://github.com/swagger-api/swagger-spec/blob/master/fixtures/v2.0/json/models/modelWithComposition.json - serves as a great sample of showing inheritance.
Composition is pretty much the same, you'd just throw out the "discriminator" field.

Hope this helps.

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

mission liao

unread,
Oct 27, 2014, 8:03:34 AM10/27/14
to swagger-sw...@googlegroups.com
Thanks, that's clear.
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.

Abhijeet Seal

unread,
Jul 25, 2016, 1:17:21 PM7/25/16
to Swagger
Hi Ron,


The slc loopback:swagger codegen will generate base of UserCredential as PersistedModel, but I want to inherit the properties of loopback inbuilt User Model
Foe example
“UserCredential”: {
properties: {
“password”: { “type”: “string” }
},
allOf : [
{ “$ref”: “#/definitions/User” }
]
}
}
Here instead of “#/definitions/User” I want to use loopback inbuilt model 'User'. In this case how can we handle this scenario?
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.

Ron Ratovsky

unread,
Jul 25, 2016, 2:25:19 PM7/25/16
to swagger-sw...@googlegroups.com

I really have no knowledge of loopback.

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.

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

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

Reply all
Reply to author
Forward
0 new messages