Jérôme
Jérôme

Reputation: 14714

Multiple inheritance/composition in OpenAPI / JSON Schema

In OpenAPI, inheritance is achieved with allof. For instance, in this example:

  "definitions": {
    "Pet": {
      "type": "object",
      "allOf": [
        {
          "$ref": "#/definitions/NewPet"   # <--- here
        },
        [...]
      ]
    },
    "NewPet": {
      "type": "object",
      "properties": {
        "name": {
          "type": "string"
        },
      }
    },

I didn't find in the spec anything about multiple inheritance. For instance, if Pet inherits from both NewPet and OldPet.

In Python, I would write

class Pet(NewPet, OldPet):
    ...

and the Method Resolution Order is deterministic about which parent class should be checked first for methods/attributes, so I can tell if Pet.age will be NewPet.age or OldPet.age.

So what if I let Pet inherit from both NewPet and OldPet, where name property is defined in both schemas, with a different value in each? What will be Pet.name?

  "definitions": {
    "Pet": {
      "type": "object",
      "allOf": [
        {
          "$ref": "#/definitions/NewPet"   # <--- multiple inheritance
          "$ref": "#/definitions/OldPet"
        },
        [...]
      ]
    },
    "NewPet": {
      "type": "object",
      "properties": {
        "name": {
          "type": "string"
        },
      }
    },
    "OldPet": {
      "type": "object",
      "properties": {
        "name": {
          "type": "integer"    # <-- name redefined here, different type
        },
      }
    },

Will OldPet take precedence? NewPet? It is undefined/invalid? Is it application defined?

I tried this in swagger-editor. Apparently, the schema with two refs is valid, but swagger-editor does not resolve the properties. If just displays allof with the two references.

Edit: According to this tutorial, having several refs in the same allof is valid. But nothing is said about the case where both schemas have a different attribute with the same name.

Upvotes: 3

Views: 5694

Answers (2)

user2321274
user2321274

Reputation: 1

One thing to consider is what inheritance means. In Eclipse Modeling Framework trying to create a class that extends 2 classes with the same attribute is an error. Never the less I consider that multiple inheritance.

This is called the Diamond Problem. See https://en.wikipedia.org/wiki/Multiple_inheritance

Upvotes: -2

Jason Desrosiers
Jason Desrosiers

Reputation: 24489

JSON Schema doesn't support inheritance. The behavior of allOf can sometimes look like inheritance, but if you think about it that way you will end up getting confused.

The allOf keyword means just what it says: all of the schemas must validate. Nothing gets overridden or takes precedence over anything else. Everything must validate.

In your example, a JSON value must validate against both "NewPet" and "OldPet" in full. Because there is no JSON value that can validate as both a string and an integer, validation of the "name" property will always fail to validate against either "NewPet" or "OldPet" (or both). Therefore the "Pet" schema will never validate against any given JSON value.

Upvotes: 5

Related Questions