From 5faed6b43416b601326fa12e1c515f62c30ed200 Mon Sep 17 00:00:00 2001 From: Robert Wallach Date: Mon, 12 Feb 2018 14:56:06 -0600 Subject: [PATCH] Update object-inheritance.md --- articles/modeling/object-inheritance.md | 37 +++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/articles/modeling/object-inheritance.md b/articles/modeling/object-inheritance.md index 8b13789..2428baf 100644 --- a/articles/modeling/object-inheritance.md +++ b/articles/modeling/object-inheritance.md @@ -1 +1,38 @@ +# Object Inheritance + +## What +- A **model** contains common resuable information that can be referenced in your endpoint definitions or other models in your API design. +- When a model derives its properties from another model, the event is called **inheritance**. +- The model which contains the common set of properties and fields becomes a parent to other models, and it is called the **base type**. +- The model which inherits the common set of properties and fields is known as the **derived type**. +- If a base type inherits its properties from another model, the derived type automatically inherits the same properties indicating that inheritance is **transitive**. +- OpenAPI Specification v2 uses the **allOf** syntax to declare inheritance. +- **allOf** obtains a collection of object definitions validated independently but, collectively make up a single object. + +## Why +- Inheritance makes your API design more compact. It helps avoid duplication of common properties and fields. + +## Best Practices + + +> Avoid using contradictory declarations such as declaring properties with the samer name but dissimilar data type in your base model and derived model. + +### Example + +``` +{ + Vehicle: + type: object + properties: + brand: + type: string + Sedan: + allOf: # (This keyword combines the Vehicle model and the Sedan model) + $ref: '#/definitions/Vehicle' + type: object + properties: + isNew: + type: boolean +} +```