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

Combined schema.json/#/definitions/foo not supported? #408

Open
stevage opened this issue Jun 2, 2015 · 5 comments · May be fixed by #733
Open

Combined schema.json/#/definitions/foo not supported? #408

stevage opened this issue Jun 2, 2015 · 5 comments · May be fixed by #733

Comments

@stevage
Copy link
Contributor

stevage commented Jun 2, 2015

I couldn't get references of this form to work:

{ "$ref": "schema.json/#/definitions/foo" }

whereas either #/definitions/foo or schema.json do work. My understanding is that these are allowed by the spec?

Without that support, it makes it hard to partially decompose a large file - either all the definitions have to be within it, or every definition has to be its own file, pretty much.

@saintsGrad15
Copy link

saintsGrad15 commented Apr 28, 2016

Has anybody resolved this issue? I think mine is similar.

I have a schema that links, via $ref, to an external schema. That schema has its own "definitions" section. Any reference, in the external schema, to its own "definitions" section fails. The only "definitions" section it can refer to successfully is the top level schema.

@limor1
Copy link

limor1 commented May 2, 2016

Same problem...

if the schema was loaded using:
jsoneditor = new JSONEditor($editor,{ schema: { $ref: "xx.json" } });

then any $ref inside xx.json is will be ignored

{
"$ref": "#/definitions/def1",
    "definitions": {
        "def1": {
            "names": {
                "type": "object",
                "properties": {
                    "port1": {
                        "type": "string",
                        "title": "Port1 Name"
                    },
                    "port2": {
                        "type": "string",
                        "title": "Port 2Name"
                    }
                }
            }
        },
        "def2": {
            "name": {
                "type": "string",
                "title": "Node instance name",
            }
        }
    }
}

@PilauP1
Copy link

PilauP1 commented May 4, 2016

Is there a solution for this yet? This seems to be the biggest issue I am experiencing with the tool

@limor1
Copy link

limor1 commented May 4, 2016

Looks like references to $ref: file.json do not get read recursively during the initial parsing.
I got it to work by "manually" reading the schema into local json vars without $ref to a URL
Then the recursive $refs within the same schema work ok.
callAjax can be replaced by .get() if you use jquery.

        function callAjax(url, callback){
            var xmlhttp;
            // compatible with IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
            xmlhttp.onreadystatechange = function(){
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
                    callback(xmlhttp.responseText);
                }
            }
            xmlhttp.open("GET", url, true);
            xmlhttp.send();
        }


        callAjax("/file/location/schema.json",
            function(e) {
                schema = JSON.parse(e);
                jsoneditor_reload(true);
            }
        );

        var jsoneditor_reload = function(keep_value) {
            var startval = (jsoneditor && keep_value)? jsoneditor.getValue() : window.startval;
            window.startval = undefined;

            if(jsoneditor) jsoneditor.destroy();
            jsoneditor = new JSONEditor($jsoneditor_element,{
                schema: schema
            });

            callAjax("/file/location/data.json",
                function(r) {
                    jsoneditor.setValue(JSON.parse(r));
                });


            window.jsoneditor = jsoneditor;

            // etc. etc..
        }

@submitteddenied
Copy link

Just submitted a pull request to solve this issue. While remote files referenced with $ref are fetched recursively, definitions in remote files are not parsed, so they are unavailable (even within those remote files). PR #733 loads them with a caveat, definitions with the same name will clobber each other.

eg:

//root.json
{ 
  type: "array",
  items: {
    oneOf: [
      { $ref: "foo.json" },
      { $ref: "bar.json" }
  }
}

//foo.json
{
  $ref: "#/definitions/item",
  definitions: {
    item: {
      { type: "string", enum: ["foo"] }
    }
  }
}

//bar.json
{
  $ref: "#/definitions/item",
  definitions: {
    item: {
      { type: "string", enum: ["bar"] }
    }
  }
}

In this example, JSONEditor will load both foo.json and bar.json "simultaneously" and it's undefined which of the #/definitions/item will be valid in the root schema.

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

Successfully merging a pull request may close this issue.

5 participants