brandur / json_schema

A JSON Schema V4 and Hyperschema V4 parser and validator.
MIT License
230 stars 45 forks source link

Couldn't resolve URI - json_schema-0.20.6/lib/json_schema/reference_expander.rb:43:in `expand!' #117

Open jerko-culina opened 5 years ago

jerko-culina commented 5 years ago

Hi, sorry if this is not the right place for this error bellow. Ruby 2.5.5 Rails 2.5.3 json_schema-0.20.6

I got next error. When I try to match occupancy.json, everything works fine. When I try to match occupancies.json I got this error.

Schema files are bellow. Can't figure why it's happening!!

Error:

JsonSchema::AggregateError: #/properties/data/items: Couldn't resolve URI: file:///occupancy.json. #: Couldn't resolve references: file:///occupancy.json#/definitions/occupancy.
from /Users/pc/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/json_schema-0.20.6/lib/json_schema/reference_expander.rb:43:in `expand!'

Schema files: occupancies.json

{
  "properties": {
    "data": {
      "type": "array",
      "items": { "$ref": "file:/occupancy.json#/definitions/occupancy" }
    },
    "meta": {
      "type": "object",
      "properties":{
        "current_page": { "type": "number" },
        "total_pages": { "type": "number" },
        "total_count": { "type": "number" },
        "max_page_size": { "type": "number" }
      }
    }
  }
}

occupancy.json

{
  "$id": "file:/occupancy.json#",
  "definitions": {
    "occupancy": {
      "type": "object",
      "properties": {
        "id": { "type": "string" },
        "type": { "type": "string" },
        "attributes": {
          "type": "object",
          "properties": {
            "type": { "type": "string" },
            "updated_at": {
              "type": "string",
              "format": "date-time"
            }
          }
        }
      }
    }
  },
  "properties": {
    "data": { "$ref": "#/definitions/occupancy" }
  }
}
kytrinyx commented 5 years ago

I think I've run into something similar, and I didn't have the time to debug it properly at the time, so I made a workaround that used a fake URL for the $id. Something like this:

"$id": "https://schemas.yourdomain.com/occupancy.json",

And then reference it in items:

"items": { "$ref": "https://schemas.yourdomain.com/occupancy.json#/definitions/occupancy" }

Let me know if that works—if it doesn't I'll try to make a failing test so we can debug.