Skip to content

Not able to induce anyOf behavior with add_schema() #85

Description

@jeyendranbalakrishnan

I want to generate a schema with two sets of objects. combined with anyOf

import json 
from genson import SchemaBuilder

d11 = dict(a=1,b=2)
d12 = dict(a=3,b=4)
d21 = dict(c=2.0)
d22 = dict(c=3.0)

b1=SchemaBuilder()
b1.add_object(d11)
b1.add_object(d12)
s1 = b1.to_schema()

b2=SchemaBuilder()
b2.add_object(d21)
b2.add_object(d22)
s2 = b2.to_schema()

b=SchemaBuilder()
b.add_schema(s1)
b.add_schema(s2)

print(json.dumps(b.to_schema()))

gives

{
  "$schema": "http://json-schema.org/schema#",
  "type": "object",
  "properties": {
    "a": {
      "type": "integer"
    },
    "b": {
      "type": "integer"
    },
    "c": {
      "type": "number"
    }
  }
}

whereas I want it to generate:

  "$schema": "http://json-schema.org/schema#",
  "anyOf": [
    {
      "type": "array",
      "properties": {
        "a": {
          "type": "integer"
        },
        "b": {
          "type": "integer"
        },
       "required": ["a", "b"]
      }
    },
    {
        "type": "object",
        "properties": {
          "c": {
            "type": "number"
          },
         "required": ["c"]
        }
    }
  ]
}

How can I achieve this?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions