RicoSuter/NJsonSchema

Recursive refs on JSON Schema from JSON example

Open

#1,252 opened on Sep 22, 2020

View on GitHub
 (0 comments) (0 reactions) (0 assignees)C# (550 forks)github user discovery
help wantedtype: bugtype: enhancement

Repository metrics

Stars
 (1,578 stars)
PR merge metrics
 (No merged PRs in 30d)

Description

Hi! The JSON

{
    "Publications": [{
            "Author": {
                "Name": "Joaquin Phoenix",
                "FullInfo": {
                    "Name": "Joaquin Phoenix"
                }
            },
            "Collaborator": {
                "Name": "Robert DeNiro",
                "FullInfo": {
                    "Name": "Robert DeNiro"
                }
            }
        }
    ]
}

when run by the code var schema = JsonSchema.FromSampleJson(data); var json = schema.ToJson();

generates schema with self-referencial refs:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "Publications": {
      "type": "array",
      "items": {
        "$ref": "#/definitions/Publication"
      }
    }
  },
  "definitions": {
    "Author": {
      "type": "object",
      "properties": {
        "Name": {
          "type": "string"
        },
        "FullInfo": {
          "$ref": "#/definitions/Author"
        }
      }
    },
    "Publication": {
      "type": "object",
      "properties": {
        "Author": {
          "$ref": "#/definitions/Author"
        },
        "Collaborator": {
          "$ref": "#/definitions/Author"
        }
      }
    }
  }
}

Contributor guide