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

$ref as object #36

Closed
jschoch opened this issue Jun 19, 2017 · 4 comments
Closed

$ref as object #36

jschoch opened this issue Jun 19, 2017 · 4 comments
Labels

Comments

@jschoch
Copy link

jschoch commented Jun 19, 2017

AFAIK this should work but perhaps it only works with the libraries I have been using.

y.json

{
    "$schema": "http://json-schema.org/draft-04/schema",
    "title": "Circle",
    "id": "http://example.com/y.json",
    "description": "Schema for a circle shape",
    "type": "object",
    "properties": {
        "x": {
            "$ref": "http://example.com/x.json"
        }
    },
    "required": ["x"]
}

x.json

{
    "$schema": "http://json-schema.org/draft-04/schema",
    "type": "object",
    "title": "x",
    "id": "http://example.com/x.json",
    "description": "Schema for common types",
    "properties": {
        "color": {
            "id": "#color",
            "type": "string",
            "enum": [ "red", "yellow", "green", "blue" ]
        },
        "point": {
            "id": "#point",
            "type": "object",
            "properties": {
                "x": {
                    "type": "number"
                },
                "y": {
                    "type": "number"
                }
            },
            "required": [ "x", "y" ]
        }
    }
}
js2e y.json
** (UndefinedFunctionError) function nil.id/0 is undefined or private
    nil.id()
    (js2e) lib/printers/preamble_printer.ex:112: JS2E.Printers.PreamblePrinter.resolve_dependency/4
    (elixir) lib/enum.ex:1755: Enum."-reduce/3-lists^foldl/2-0-"/3
    (js2e) lib/printers/preamble_printer.ex:57: JS2E.Printers.PreamblePrinter.create_imports/3
    (js2e) lib/printers/preamble_printer.ex:35: JS2E.Printers.PreamblePrinter.print_preamble/3
    (js2e) lib/printer.ex:51: JS2E.Printer.print_schema/3
    (js2e) lib/printer.ex:33: anonymous fn/5 in JS2E.Printer.print_schemas/2
    (stdlib) lists.erl:1263: :lists.foldl/3
@dragonwasrobot
Copy link
Owner

There might be a bug when referencing the root of another schema, I'll look into it.
In general, I try to follow the spec here for resolving references: http://json-schema.org/latest/json-schema-core.html#id-keyword.
As a quick work-around you can try wrapping the x in a "definitions" property (see the spec for example).

Note: I'm also working on improved error reporting.

@dragonwasrobot
Copy link
Owner

Found and fixed the bug, will push a solution soon.

If you want to fix this now for yourself you can replace the line
https://github.com/dragonwasrobot/json-schema-to-elm/blob/master/lib/printer.ex#L194
with

if to_string(identifier) == schema_id do
  schema_type_dict["#"]
else
  schema_type_dict[to_string(identifier)]
end

@dragonwasrobot
Copy link
Owner

Closing issue as f238a54 fixes it.

@jschoch
Copy link
Author

jschoch commented Jun 20, 2017

i updated and perhaps i'm doing something wrong.

Also did the wild guess that "id" resolves somehow and is how "http://example.com/x.json" gets from a non existent URI to the file with that "id". Is that correct? I've seen other libraries resort to having a module apply tuple for resolving file refs.

../../js2e y2.json

20:15:15.465 [debug] Arguments: ["y2.json"]

20:15:15.474 [debug] Files: ["y2.json"]

20:15:15.496 [debug] Parsing type with name: #, path: , and value: %{"$schema" => "http://json-schema.org/draft-04/schema#", "description" => "Schema for a circle shape", "id" => "http://example.com/y2.json", "properties" => %{"x" => %{"$ref" => "http://example.com/x.json"}}, "required" => ["x"], "title" => "Circle", "type" => "object"}

20:15:15.500 [debug] node_parser: &JS2E.Parsers.ObjectParser.parse/5

20:15:15.501 [debug] Parsing '["#"]' as ObjectType

20:15:15.504 [debug] Parsing type with name: x, path: #, and value: %{"$ref" => "http://example.com/x.json"}

20:15:15.504 [debug] node_parser: &JS2E.Parsers.TypeReferenceParser.parse/5

20:15:15.505 [debug] parsing '["#", "x"]' as TypeReference

20:15:15.509 [debug] Parsed type reference: %JS2E.Types.TypeReference{name: "x", path: %URI{authority: "example.com", fragment: nil, host: "example.com", path: "/x.json", port: 80, query: nil, scheme: "http", userinfo: nil}}

20:15:15.510 [debug] Descendants types dict: %{"#/x" => %JS2E.Types.TypeReference{name: "x", path: %URI{authority: "example.com", fragment: nil, host: "example.com", path: "/x.json", port: 80, query: nil, scheme: "http", userinfo: nil}}}

20:15:15.510 [debug] Property ref dict: %{"x" => ["#", "x"]}

20:15:15.511 [debug] Parsed object type: %JS2E.Types.ObjectType{name: "#", path: ["#"], properties: %{"x" => ["#", "x"]}, required: ["x"]}
** (UndefinedFunctionError) function nil.id/0 is undefined or private
    nil.id()
    (js2e) lib/printers/preamble_printer.ex:112: JS2E.Printers.PreamblePrinter.resolve_dependency/4
    (elixir) lib/enum.ex:1755: Enum."-reduce/3-lists^foldl/2-0-"/3
    (js2e) lib/printers/preamble_printer.ex:57: JS2E.Printers.PreamblePrinter.create_imports/3
    (js2e) lib/printers/preamble_printer.ex:35: JS2E.Printers.PreamblePrinter.print_preamble/3
    (js2e) lib/printer.ex:51: JS2E.Printer.print_schema/3
    (js2e) lib/printer.ex:33: anonymous fn/5 in JS2E.Printer.print_schemas/2
    (stdlib) lists.erl:1263: :lists.foldl/3

schoch@xeon:~/dev/json-schema-to-elm/examples/example-input-json-schemas$ cat y2.json

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "title": "Circle",
    "id": "http://example.com/y2.json",
    "description": "Schema for a circle shape",
    "type": "object",
    "properties": {
        "x": {
            "$ref": "http://example.com/x.json"
        }
    },
    "required": ["x"]
}

schoch@xeon:~/dev/json-schema-to-elm/examples/example-input-json-schemas$ cat x.json

{
    "$schema": "http://json-schema.org/draft-04/schema",
    "type": "object",
    "title": "x",
    "id": "http://example.com/x.json",
    "description": "Schema for common types",
    "properties": {
        "color": {
            "id": "#color",
            "type": "string",
            "enum": [ "red", "yellow", "green", "blue" ]
        },
        "point": {
            "id": "#point",
            "type": "object",
            "properties": {
                "x": {
                    "type": "number"
                },
                "y": {
                    "type": "number"
                }
            },
            "required": [ "x", "y" ]
        }
    }
}```

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

No branches or pull requests

2 participants