Exemple #1
0
    def _fetch(self, url):
        s = requests.Session()
        s.mount("file:", FileAdapter())
        # make sure file url is absolute
        u = urlparse(url)
        if u.scheme == "file":
            u = u._replace(path=str(Path(u.path).absolute()))
            url = u.geturl()

        try:
            doc = s.get(url)
        except Exception as e:
            raise ValueError("unable to fetch '{}', due to '{}'".format(
                url, str(e)))

        src = doc.text
        try:
            return loads(src, origin=url)
        except:
            try:
                return loads(src, parser=yaml)
            except Exception as e:
                print(src)
                raise ValueError("unable to parse '{}', due to '{}'".format(
                    url, str(e)))
Exemple #2
0
def test_swagger_structure():
    yaml_src = """
openapi: 3.0.0
info:
  description: "deposits endpoint"
  version: "1.0.0"
  title: "vouchers"
paths:
  /make/deposit:
    post:
      summary: "make a deposit"
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: file:tests/schemas/money.json
      responses:
        '200':
          description: success
          content:
            application/json:
              schema:
                type: boolean
        '400':
          description: failure
          content:
            application/json:
              schema:
                type: string
"""
    schema = loads(yaml_src, parser=yaml)
    print(schema.to_dict())
Exemple #3
0
def test_array_items_byref():
    src = """
{
  "type" : "object",
  "properties" : {
    "humanity" : {
      "type" : "array",
      "items" : {
        "$ref" : "#/definitions/human"
      }
    }
  },
  "definitions" : {
    "human" : {
      "type" : "object",
      "properties" : {
        "name" : {
          "type" : "string"
        }
      }
    }
  }
}
"""
    schema = loads(src)
    humanity = schema.property("humanity")
    assert isinstance(humanity.items, Reference)
    assert isinstance(humanity.items.resolve(), ObjectSchema)
    assert isinstance(humanity.items.resolve().property("name"), StringSchema)
def test_combination_with_schema_dumping():
    src = """
{
  "type": "object",
  "properties": {
    "x" : {
      "type" : "string"
    },
    "y" : {
      "type" : "string"
    }
  },
  "oneOf": [
    {
      "required": [
        "x"
      ]
    },
    {
      "required": [
        "y"
      ]
    }
  ]
}
"""
    schema = loads(src)
    d = schema.to_dict()
    s = json.dumps(d)
Exemple #5
0
def test_external_references(asset):
    json_src = """{
    "type": "object",
    "properties" : {
      "home" : {
        "$ref" : "file:%%%%"
      },
      "business" : {
        "$ref" : "https://localhost/schemas/unknown.json"
      }
    }
  }
  """.replace("%%%%", asset("guid.json"))

    schema = loads(json_src)

    assert isinstance(schema, ObjectSchema)
    home = schema.property("home", return_definition=False)
    assert home.is_ref()
    assert isinstance(home._definition, Reference)
    assert isinstance(schema.property("home"), StringSchema)

    try:
        schema.property("business").resolve()
        assert False
    except ValueError:
        pass
def test_reference_to_definition_selections():
    json_src = """{
    "type": "object",
    "properties" : {
      "home" : {
        "type" : "object",
        "properties" : {
          "address" : {
            "$ref" : "#/definitions/address"
          }
        }
      }
    },
    "definitions" : {
      "address" : {
        "type" : "object",
        "properties" : {
          "street" : {
            "type" : "string"
          }
        }
      }
    }
  }
  """

    schema = loads(json_src)

    assert isinstance(schema, ObjectSchema)
    assert isinstance(schema.select("home").definition, ObjectSchema)
    assert isinstance(schema.select("home.address").definition, ObjectSchema)
    assert isinstance(
        schema.select("home.address.street").definition, StringSchema)
Exemple #7
0
def test_swagger_component_definitions():
    src = """
type: object
properties:
  something:
    $ref: "#/components/schemas/sometype"
  someotherthing:
    $ref: "#/components/schemas/someothertype"
components:
  schemas:
    sometype:
      type: string
    someothertype:
      type: object
      properties:
        foo:
          type: string
"""

    schema = loads(src, parser=yaml)

    assert isinstance(schema.property("something"), StringSchema)
    assert isinstance(schema.property("someotherthing"), ObjectSchema)
    assert isinstance(
        schema.select("someotherthing.foo").definition, StringSchema)
def test_nested_selections():
    json_src = """{
    "type": "object",
    "properties" : {
      "home" : {
        "type" : "object",
        "properties" : {
          "address" : {
            "type" : "object",
            "properties" : {
              "street" : {
                "type" : "string"
              }
            }
          }
        }
      }
    }
  }
  """

    schema = loads(json_src)

    assert isinstance(schema, ObjectSchema)
    assert isinstance(schema.select("home").definition, ObjectSchema)
    assert isinstance(schema.select("home.address").definition, ObjectSchema)
    assert isinstance(
        schema.select("home.address.street").definition, StringSchema)
    assert schema.select("home.address.phone") is None
def test_anyof_refs():
    json_src = """{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "$id": "test",
    "title": "a title",
    "description": "a description",
    "version": "123",
    "type": "object",
    "properties" : {
      "home" : {
        "anyOf" : [
          { "$ref" : "#/definitions/address" },
          { "$ref" : "#/definitions/id"      }
        ]
      },
      "business" : {
        "anyOf" : [
          { "$ref" : "#/definitions/address" },
          { "$ref" : "#/definitions/id"      }
        ]
      }
    },
    "definitions" : {
      "id" : {
        "type" : "string"
      },
      "address" : {
        "type" : "object",
        "properties" : {
          "url": {
            "type": "string",
            "format": "uri-reference"
          }
        },
        "additionalProperties" : false,
        "required" : [
          "url"
        ]
      }
    }
  }
  """

    schema = loads(json_src)

    home = schema.select("home.url").definition
    business = schema.select("business.url").definition

    assert isinstance(home, StringSchema)
    assert isinstance(business, StringSchema)

    # home = string <- property <- object <- definition
    assert isinstance(home.parent.parent.parent, Definition)
    assert home.parent.parent.parent.name == "address"
    assert isinstance(business.parent.parent.parent, Definition)
    assert business.parent.parent.parent.name == "address"

    assert home is business
Exemple #10
0
def test_untyped_object():
  src = """
{
  "type": "object",
  "properties": {
    "properties": {
      "type": "object",
      "additionalProperties": { "$ref": "#" },
    },
    "something" : {
      "properties" : {
        "a" : {
          "type" : "object"
        },
        "properties" : {
          "$ref" : "#"
        }
      }
    },
    "somethingelse" : {
      "properties" : {
        "properties" : {
          "type" : "string"
        }
      }
    },
    "type": {
      "anyOf": [
        { "type" : "string" },
        {
          "type": "array",
          "items": { "type" : "string" }
        }
      ]
    }
  },
  "format": { "type": "string" },
  "definitions" : {
    "properties" : {
      "enum" : [
        "a",
        "b"
      ]
    }
  }
}
"""
  schema = loads(src)
  assert isinstance(schema.property("properties"), ObjectSchema)
  assert isinstance(schema.property("type"), AnyOf)
  assert isinstance(schema.property("something"), ObjectSchema)
  assert isinstance(schema.select("something.properties"), Property)
  assert isinstance(schema.select("somethingelse.properties"), Property)
  assert isinstance(schema.definition("properties"), Enum)
def test_external_reference_with_fragment(asset):
    json_src = """{
    "type": "object",
    "properties" : {
      "foreign" : {
        "$ref" : "file:%%%%"
      }
    }
  }
  """.replace("%%%%", asset("money.json"))

    schema = loads(json_src)

    assert (isinstance(schema.select("foreign.currency").definition, Enum))
def test_string_schema():
    json_src = """{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "$id": "test",
    "title": "a title",
    "description": "a description",
    "version": "123",
    "type": "string",
    "format": "uri-reference"
  }
  """

    schema = loads(json_src)

    assert isinstance(schema, StringSchema)
Exemple #13
0
def test_origin_of_external_reference(asset):
    json_src = """{
    "type": "object",
    "properties" : {
      "foreign" : {
        "$ref" : "file:%%%%"
      }
    }
  }
  """.replace("%%%%", asset("money.json#/properties/currency"))

    schema = loads(json_src)

    assert isinstance(schema.property("foreign"), Enum)
    assert isinstance(schema.property("foreign"), Enum)
    assert schema.property("foreign").origin.endswith("currencies.json")
Exemple #14
0
def test_swagger_select_components():
    src = """
components:
  schemas:
    sometype:
      type: string
    someothertype:
      type: object
      properties:
        foo:
          type: string
"""

    schema = loads(src, parser=yaml)
    assert isinstance(schema.definition("sometype"), StringSchema)
    assert isinstance(
        schema.select("components.schemas.sometype").definition, StringSchema)
def test_anyof():
    json_src = """{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "$id": "test",
    "title": "a title",
    "description": "a description",
    "version": "123",
    "type": "object",
    "properties" : {
      "home" : {
        "anyOf" : [
          { "$ref" : "#/definitions/address" },
          { "$ref" : "#/definitions/id"      }
        ]
      }
    },
    "definitions" : {
      "id" : {
        "type" : "string"
      },
      "address" : {
        "type" : "object",
        "properties" : {
          "url": {
            "type": "string",
            "format": "uri-reference"
          }
        },
        "additionalProperties" : false,
        "required" : [
          "url"
        ]
      } 
    }
  }
  """

    schema = loads(json_src)

    assert isinstance(schema, ObjectSchema)
    assert len(schema.properties) == 1
    assert schema.properties[0].name == "home"
    assert isinstance(schema.properties[0].definition, AnyOf)
    assert len(schema.properties[0].definition.options) == 2
    assert isinstance(schema.properties[0].definition.options[0], Reference)
    assert isinstance(schema.properties[0].definition.options[1], Reference)
def test_dependencies_in_tuple(asset):
    src = """
{
  "type" : "array",
  "items" : [
    {
      "type" : "string"
    },
    {
      "$ref" : "file:%%%%"
    }
  ]
}
""".replace("%%%%", asset("money.json"))

    schema = loads(src)
    assert len(schema.dependencies()) == 1
Exemple #17
0
def test_external_reference_with_fragment(asset):
    json_src = """{
    "type": "object",
    "properties" : {
      "foreign" : {
        "$ref" : "file:%%%%"
      }
    }
  }
  """.replace("%%%%", asset("money.json#/properties/currency"))

    schema = loads(json_src)

    assert isinstance(schema, ObjectSchema)
    foreign = schema.property("foreign", return_definition=False)
    assert foreign.is_ref()
    assert isinstance(foreign._definition, Reference)
    assert isinstance(schema.property("foreign"), Enum)
def test_array_tuple_support():
    src = """
{
  "type" : "array",
  "items" : [
    {
      "type" : "string"
    },
    {
      "type" : "integer"
    }
  ]
}
"""

    schema = loads(src)
    d = schema.to_dict()
    s = json.dumps(d)
def test_dependencies_within_references(asset):
    src = """
{
  "type": "object",
  "properties" : {
    "a" : {
      "$ref" : "#/definitions/aType"
    }
  },
  "definitions" : {
    "aType" : {
      "$ref" : "file:%%%%"
    }
  }
}
""".replace("%%%%", asset("money.json"))

    schema = loads(src)
    assert len(schema.dependencies()) == 1
Exemple #20
0
def test_simple_local_ref_to_definition():
    json_src = """{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "$id": "test",
    "title": "a title",
    "description": "a description",
    "version": "123",
    "type": "object",
    "properties" : {
      "home" : {
        "anyOf" : [
          { "$ref" : "#/definitions/address" },
          { "$ref" : "#/definitions/id"      }
        ]
      }
    },
    "definitions" : {
      "id" : {
        "type" : "string"
      },
      "address" : {
        "type" : "object",
        "properties" : {
          "url": {
            "type": "string",
            "format": "uri-reference"
          }
        },
        "additionalProperties" : false,
        "required" : [
          "url"
        ]
      } 
    }
  }
  """

    schema = loads(json_src)

    assert isinstance(schema.property("home").options[1], Reference)
    resolved = schema.property("home").options[1].resolve()
    assert isinstance(resolved, StringSchema)
    assert resolved is schema.definition("id")
Exemple #21
0
def test_selecting_into_any_of():
    src = """
{
  "type" : "object",
  "anyOf" : [
    { "$ref" : "#/definitions/x" },
    { "$ref" : "#/definitions/y" },
    { "$ref" : "#/definitions/z" }
  ],
  "definitions" : {
    "x" : {
      "type" : "object",
      "properties" : {
        "a": {
          "type" : "string"
        }
      }
    },
    "y" : {
      "type" : "object",
      "properties" : {
        "b": {
          "type" : "string"
        }
      }
    },
    "z" : {
      "type" : "object",
      "properties" : {
        "c": {
          "type" : "string"
        }
      }
    }
  }
}
"""

    schema = loads(src)
    assert isinstance(schema.select("a"), Property)
    assert schema.select("a").name == "a"
    assert isinstance(schema.select("c"), Property)
    assert schema.select("c").name == "c"
def test_dependencies_in_combination(asset):
    src = """
{
  "type" : "object",
  "allOf" : [
    { "$ref" : "#/definitions/x" },
    { "$ref" : "#/definitions/y" },
    { "$ref" : "#/definitions/z" }
  ],
  "definitions" : {
    "x" : {
      "type" : "object",
      "properties" : {
        "a": {
          "type" : "string"
        }
      }
    },
    "y" : {
      "type" : "object",
      "properties" : {
        "b": {
          "type" : "string"
        }
      }
    },
    "z" : {
      "type" : "object",
      "properties" : {
        "c": {
          "$ref" : "file:%%%%"
        }
      }
    }
  }
}
""".replace("%%%%", asset("money.json"))

    schema = loads(src)
    assert len(schema.dependencies()) == 1
def test_simple_selections():
    json_src = """{
    "type": "object",
    "properties" : {
      "home" : {
        "type" : "string"
      },
      "business" : {
        "type" : "string"
      }
    }
  }
  """

    schema = loads(json_src)

    assert isinstance(schema, ObjectSchema)
    home = schema.select("home")
    assert isinstance(home, Property)
    assert isinstance(home.definition, StringSchema)

    assert isinstance(schema.select("business").definition, StringSchema)
def test_object_schema():
    json_src = """{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "$id": "test",
    "title": "a title",
    "description": "a description",
    "version": "123",
    "type": "object",
    "properties": {
      "url": {
        "type": "string",
        "format": "uri-reference"
      }
    }
  }
  """

    schema = loads(json_src)

    assert isinstance(schema, ObjectSchema)
    assert len(schema.properties) == 1
    assert schema.properties[0].name == "url"
    assert isinstance(schema.properties[0].definition, StringSchema)
Exemple #25
0
def test_avoiding_recursing():
    src = """
{
  "type" : "object",
  "properties" : {
    "person" : {
      "$ref" : "#/definitions/human"
    }
  },
  "definitions" : {
    "human" : {
      "type" : "object",
      "properties" : {
        "parent" : {
          "$ref" : "#/definitions/human"
        }
      }
    }
  }
}
"""
    schema = loads(src)
    dependencies = schema.dependencies()
    assert True
Exemple #26
0
def test_nested_selections():
    src = """{
    "type": "object",
    "properties" : {
      "home" : {
        "type" : "object",
        "properties" : {
          "address" : {
            "type" : "object",
            "properties" : {
              "street" : {
                "type" : "string"
              }
            }
          }
        }
      }
    }
  }
  """

    schema = loads(src)
    assert schema.select("home")._location.line == 4
    assert schema.select("home.address")._location.line == 7
def test_all_of_properties():
    src = """
type: object
properties:
allof:
  - $ref: "#/definitions/something"
  - $ref: "#/definitions/somethingelse"
definitions:
  something:
    type: object
    properties:
      x:
        type: string
  somethingelse:
    type: object
    properties:
      y:
        type: integer        
"""
    schema = loads(src, parser=yaml)
    assert isinstance(schema.property("x"), StringSchema)
    assert isinstance(schema.property("y"), IntegerSchema)
    assert isinstance(schema.select("x"), Property)
    assert isinstance(schema.select("x").definition, StringSchema)
def test_accessing_array_tuple():
    src = """
{
  "type" : "object",
  "properties" : {
    "list" : {
      "type" : "array",
      "items" : [
        {
          "type" : "string"
        },
        {
          "type" : "object",
          "properties" : {
            "value" : {
              "type" : "integer"
            }
          }
        }
      ]
    }
  }
}
"""

    schema = loads(src)
    assert isinstance(schema.property("list"), TupleSchema)
    assert isinstance(schema.property("list")[0].definition, StringSchema)
    assert isinstance(
        schema.property("list")[1].definition.property("value"), IntegerSchema)

    assert isinstance(
        schema.property("list").item(1).property("value"), IntegerSchema)

    assert isinstance(schema.select("list.0").definition, StringSchema)
    assert isinstance(schema.select("list.1.value").definition, IntegerSchema)
def test_end_of_trace_is_top_level_schema(asset):
    src = """
{
  "type" : "object",
  "properties" : {
    "level" : {
      "type": "object",
      "properties" : {
        "id" : {
          "$ref" : "file:%%%%"
        }
      }
    }
  }
}
""".replace("%%%%", asset("guid.json"))

    schema = loads(src)
    trace = schema.trace("level.id")
    assert len(trace) == 2
    assert trace[0]._location.line == 5
    assert trace[1]._location.line == 8
    assert trace[1]._origin is None
    assert trace[1].definition._origin.endswith("tests/schemas/guid.json")
def test_overlapping_paths():
    src = """
{
  "type": "object",
  "properties": {
    "collection": {
      "type": "array",
      "items": {
        "oneOf" : [
          {
            "type" : "object",
            "properties" : {
              "product" : {
                "tag" : 1,
                "type" : "object",
                "properties" : {
                  "name" : {
                    "type" : "string"
                  }
                }
              }
            }
          },
          {
            "type" : "object",
            "properties" : {
              "product" : {
                "tag" : 2,
                "type" : "object",
                "properties" : {
                  "label" : {
                    "type" : "string"
                  }
                }
              }
            }
          },
          {
            "type" : "object",
            "properties" : {
              "product" : {
                "tag" : 3,
                "type" : "object",
                "properties" : {
                  "label" : {
                    "type" : "object",
                    "properties" : {
                      "nl" : {
                        "type" : "string"
                      }
                    }
                  }
                }
              }
            }
          }
        ]
      }
    }
  }
}
"""

    schema = loads(src)
    trace = schema.trace("collection.product.label")
    assert len(trace) == 3
    assert trace[1].definition.tag.value == 2

    trace = schema.trace("collection.product.label.nl")
    assert len(trace) == 4
    assert trace[1].definition.tag.value == 3