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

References through other references being produced #24

Closed
thewilkybarkid opened this issue Aug 2, 2016 · 1 comment
Closed

References through other references being produced #24

thewilkybarkid opened this issue Aug 2, 2016 · 1 comment

Comments

@thewilkybarkid
Copy link

Sorry if this is a bit confusing to understand, but I have a complicated schema with many (circular) references, and I'm using the bundle() method then passing the results to z-schema which can't understand it.

My main schema (minus a lot of irrelevant detail) is:

model.yaml

$schema: https://json-schema.org/draft-04/schema#
type: object
title: Model
properties:
    body:
        type: array
        items:
            $ref: section.yaml
        minItems: 1
    body2:
        type: object
        properties:
            content:
                type: array
                items:
                    $ref: any.yaml
                minItems: 1

Then the following are referenced:

any.yaml

oneOf:
  - $ref: list.yaml
  - $ref: section.yaml
  - $ref: text.yaml

list.yaml

$schema: https://json-schema.org/draft-04/schema#
type: object
title: List
properties:
    items:
        type: array
        items:
            oneOf:
              - $ref: list.yaml
              - $ref: text.yaml
        minItems: 1
required:
  - items

section.yaml

$schema: https://json-schema.org/draft-04/schema#
type: object
title: Section
properties:
    content:
        type: array
        items:
            $ref: any.yaml
        minItems: 1
required:
  - content

text.yaml

$schema: https://json-schema.org/draft-04/schema#
type: object
title: Text
properties:
    text:
        type: string
required:
  - text

The bundle() method of this library returns:

{
    "$schema": "https://json-schema.org/draft-04/schema#",
    "type": "object",
    "title": "Model",
    "properties": {
        "body": {
            "type": "array",
            "items": {
                "$schema": "https://json-schema.org/draft-04/schema#",
                "type": "object",
                "title": "Section",
                "properties": {
                    "content": {
                        "type": "array",
                        "items": {
                            "$ref": "#/properties/body2/properties/content/items"
                        },
                        "minItems": 1
                    }
                },
                "required": [
                    "content"
                ]
            },
            "minItems": 1
        },
        "body2": {
            "type": "object",
            "properties": {
                "content": {
                    "type": "array",
                    "items": {
                        "oneOf": [
                            {
                                "$schema": "https://json-schema.org/draft-04/schema#",
                                "type": "object",
                                "title": "List",
                                "properties": {
                                    "items": {
                                        "type": "array",
                                        "items": {
                                            "oneOf": [
                                                {
                                                    "$ref": "#/properties/body/items/properties/content/items/oneOf/0"
                                                },
                                                {
                                                    "$ref": "#/properties/body/items/properties/content/items/oneOf/2"
                                                }
                                            ]
                                        },
                                        "minItems": 1
                                    }
                                },
                                "required": [
                                    "items"
                                ]
                            },
                            {
                                "$ref": "#/properties/body/items"
                            },
                            {
                                "$schema": "https://json-schema.org/draft-04/schema#",
                                "type": "object",
                                "title": "Text",
                                "properties": {
                                    "text": {
                                        "type": "string"
                                    }
                                },
                                "required": [
                                    "text"
                                ]
                            }
                        ]
                    },
                    "minItems": 1
                }
            }
        }
    }
}

When using z-schema to parse (any) JSON it errors with:

[
    {
        "code": "UNRESOLVABLE_REFERENCE",
        "params": [
            "#/properties/body/items/properties/content/items/oneOf/0"
        ],
        "message": "Reference could not be resolved: #/properties/body/items/properties/content/items/oneOf/0",
        "path": "#/properties/body2/properties/content/items/oneOf/0/properties/items/items/oneOf/0"
    },
    {
        "code": "UNRESOLVABLE_REFERENCE",
        "params": [
            "#/properties/body/items/properties/content/items/oneOf/2"
        ],
        "message": "Reference could not be resolved: #/properties/body/items/properties/content/items/oneOf/2",
        "path": "#/properties/body2/properties/content/items/oneOf/0/properties/items/items/oneOf/1"
    }
]

The (first) problem is that the reference at #/properties/body2/properties/content/items/oneOf/0/properties/items/items/oneOf/0 ("$ref": "#/properties/body/items/properties/content/items/oneOf/0") goes through another $ref. According to https://github.com/json-schema/json-schema/wiki/$ref-traps#references-through-other-references this should still be resolvable (so the bug could be in z-schema not understanding it), but the bundle() method could have produced "$ref": "#/properties/body2/properties/content/items/oneOf/0" which would avoid the problem.

I hit this problem yesterday and reordering some properties (hence reordering some $refs) fixed it a couple of times, but I can't find a way around the latest instance.

@thewilkybarkid
Copy link
Author

I have a workaround, which is to run it through bundle() a second time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants