3

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.

2
  • 1
    What you describe is not inheritance. It breaks LSP ( en.wikipedia.org/wiki/Liskov_substitution_principle ). Inheritance is sometimes called "is a" relation. Your "NewPet" is not "OldPet". Even in languages that support the concept of "shadowing", the old member does not go away and can be accessed. You can get the same shadowing behavior by renaming NewPet.name to NewPet.new_name. Commented Jan 16, 2019 at 18:28
  • @Ark-kun Thanks for the heads-up. Commented Jan 21, 2019 at 14:38

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. This answer makes it all clear. The consequence of this, I guess, is that OpenAPI shouldn't rely solely one allOf to manage inheritance, because there are corner cases, like the one I exposed, where Schemas inherit from each other but allOf can't be used. In this case, an explicit field like x-inherits or x-childOf would come in handy.
-2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.