Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for oneOf inheritance #23

Merged
merged 7 commits into from
Jan 15, 2024
Merged

Add support for oneOf inheritance #23

merged 7 commits into from
Jan 15, 2024

Conversation

glopesdev
Copy link
Member

@glopesdev glopesdev commented Jan 15, 2024

This PR adds preliminary support for validation and code generation of oneOf inheritance patterns where discriminated unions are used to define polymorphic properties or arrays. In this case, it is common to use a oneOf restriction to specify the exact set of allowed types.

NJsonSchema does not directly support this yet (see RicoSuter/NJsonSchema#13), but the approach detailed here works for some of the most common cases. Supporting this pattern will allow code completion of polymorphic types (with discriminator validation) for manually edited YAML / JSON files.

An example of a supported schema is below:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "title": "Container",
  "properties": {
    "Animals": {
      "type": "array",
      "items": { "$ref": "#/definitions/AnimalTypes" }
    }
  },
  "definitions": {
    "Dog": {
      "type": "object",
      "properties": {
        "kind": {
          "enum": [ "Dog" ]
        },
        "Bar": {
          "type": [
            "null",
            "string"
          ]
        }
      },
      "required": [ "kind", "Bar" ],
      "allOf": [
        {
          "$ref": "#/definitions/Animal"
        }
      ]
    },
    "Cat": {
      "type": "object",
      "properties": {
        "kind": {
          "enum": [ "Cat" ]
        },
        "Baz": {
          "type": [
            "null",
            "string"
          ]
        }
      },
      "required": [ "kind", "Baz" ],
      "allOf": [
        {
          "$ref": "#/definitions/Animal"
        }
      ]
    },
    "Animal": {
      "type": "object",
      "discriminator": "kind",
      "x-abstract": true,
      "required": [
        "kind"
      ],
      "properties": {
        "Foo": {
          "type": [
            "null",
            "string"
          ]
        },
        "kind": {
          "type": "string"
        }
      }
    },
    "AnimalTypes": {
      "oneOf": [
        {
          "$ref": "#/definitions/Dog"
        },
        {
          "$ref": "#/definitions/Cat"
        },
        {
          "type": "null"
        }
      ]
    }
  }
}

@glopesdev glopesdev added the feature New planned feature label Jan 15, 2024
@glopesdev glopesdev merged commit 3021350 into main Jan 15, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New planned feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant