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

Add coverage for JSON Schema V4 #89

Merged
merged 1 commit into from Apr 5, 2019
Merged

Add coverage for JSON Schema V4 #89

merged 1 commit into from Apr 5, 2019

Conversation

seanpdoyle
Copy link
Collaborator

@seanpdoyle seanpdoyle commented Aug 31, 2018

Closes #88.
Closes #90.

Add tests from Issue #90

When the "id" key is specified as a "number" in both attribute and
property definitions, then specified as a "string" value in the
payload, the matcher will raise and the test will pass.

After rebasing off master after 3b66b4d was merged, the
tests that were once failing
are now passing.

spec/json_matchers/match_json_schema_spec.rb Show resolved Hide resolved
spec/json_matchers/match_json_schema_spec.rb Show resolved Hide resolved
spec/json_matchers/match_json_schema_spec.rb Outdated Show resolved Hide resolved
spec/json_matchers/match_json_schema_spec.rb Show resolved Hide resolved
spec/json_matchers/match_json_schema_spec.rb Show resolved Hide resolved
spec/json_matchers/match_json_schema_spec.rb Show resolved Hide resolved
spec/json_matchers/match_json_schema_spec.rb Show resolved Hide resolved
spec/json_matchers/match_json_schema_spec.rb Show resolved Hide resolved
spec/json_matchers/match_json_schema_spec.rb Show resolved Hide resolved
spec/json_matchers/match_json_schema_spec.rb Show resolved Hide resolved
"attributes"
],
"properties": {
"id": { "type": "number" },
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@seanpdoyle The type of id should be "string". The test fails if we change the type from number to string.

@@ -59,6 +59,147 @@
expect(json).to match_json_schema("api/v1/schema")
end

it "supports invalidating the referenced schema when using local references" do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/LineLength: Line is too long. [81/80]


json = build(:response, {
"data": [{
"id": 1,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Convert this id attribute as a string, i.e. "id": "1". Keep the id inside the attributes as a number. The test will fail now.

Copy link
Collaborator Author

@seanpdoyle seanpdoyle Nov 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha! I've pushed up d2027df. Thanks for clarifying @musaffa !

@musaffa
Copy link

musaffa commented Nov 9, 2018

This test will pass:

it "Fails properly in local refs in referenced schema" do
  create(:schema, name: "post", json: {
    "$schema": "https://json-schema.org/draft-04/schema#",
    "id": "file:/post.json#",
    "definitions": {
      "attributes": {
        "type": "object",
        "required": [
          "id",
          "name",
        ],
        "properties": {
          "id": { "type": "string" },
          "name": { "type": "string" }
        }
      }
    },
    "type": "object",
    "required": [
      "id",
      "type",
      "attributes"
    ],
    "properties": {
      "id": { "type": "string" },
      "type": { "type": "string" },
      "attributes": {
        # "$ref": "#/definitions/attributes"
        "type": "object",
        "required": [
          "id",
          "name",
        ],
        "properties": {
          "id": { "type": "string" },
          "name": { "type": "string" }
        }
      }
    }
  })
  posts_index = create(:schema, name: "posts/index", json: {
    "$schema": "https://json-schema.org/draft-04/schema#",
    "id": "file:/posts/index.json#",
    "type": "object",
    "required": [
      "data"
    ],
    "definitions": {
      "posts": {
        "type": "array",
        "items": {
          "$ref": "file:/post.json#"
        }
      }
    },
    "properties": {
      "data": {
        "$ref": "#/definitions/posts"
      }
    }
  })

  json = build(:response, {
    "data": [{
      "id": "1",
      "type": "Post",
      "attributes": {
        "id": 1,
        "name": "The Post's Name"
      }
    }]
  })

  expect(json).not_to match_json_schema(posts_index)
end

But this test will fail:

it "Fails properly in local refs in referenced schema" do
  create(:schema, name: "post", json: {
    "$schema": "https://json-schema.org/draft-04/schema#",
    "id": "file:/post.json#",
    "definitions": {
      "attributes": {
        "type": "object",
        "required": [
          "id",
          "name",
        ],
        "properties": {
          "id": { "type": "string" },
          "name": { "type": "string" }
        }
      }
    },
    "type": "object",
    "required": [
      "id",
      "type",
      "attributes"
    ],
    "properties": {
      "id": { "type": "string" },
      "type": { "type": "string" },
      "attributes": {
        "$ref": "#/definitions/attributes"
        # "type": "object",
        # "required": [
        #   "id",
        #   "name",
        # ],
        # "properties": {
        #   "id": { "type": "string" },
        #   "name": { "type": "string" }
        # }
      }
    }
  })
  posts_index = create(:schema, name: "posts/index", json: {
    "$schema": "https://json-schema.org/draft-04/schema#",
    "id": "file:/posts/index.json#",
    "type": "object",
    "required": [
      "data"
    ],
    "definitions": {
      "posts": {
        "type": "array",
        "items": {
          "$ref": "file:/post.json#"
        }
      }
    },
    "properties": {
      "data": {
        "$ref": "#/definitions/posts"
      }
    }
  })

  json = build(:response, {
    "data": [{
      "id": "1",
      "type": "Post",
      "attributes": {
        "id": 1,
        "name": "The Post's Name"
      }
    }]
  })

  expect(json).not_to match_json_schema(posts_index)
end

Check out the difference in the commented out codes in properties.attributes section.

@Jesterovskiy
Copy link

@seanpdoyle any updates for this PR?

@seanpdoyle
Copy link
Collaborator Author

After rebasing off master after 3b66b4d was merged, the tests that were once failing are now passing.

Thank you all for your contributions, and thank you @musaffa for clarifying the details around your failing test!

Closes [#88].
Closes [#90].

Add tests from Issue #90

When the `"id"` key is specified as a `"number"` in both attribute and
property definitions, then specified as a `"string"` value in the
payload, the matcher will raise and the test will pass.

After rebasing off `master` after [`3b66b4d`][3b66b4d] was merged, [the
tests that were once failing][tests] are now passing.

[#88]: #88
[#90]: #90
[3b66b4d]: 3b66b4d
[tests]: #89 (comment)
@seanpdoyle seanpdoyle merged commit 4edaef8 into master Apr 5, 2019
@seanpdoyle seanpdoyle deleted the issue-88 branch April 5, 2019 16:44
seanpdoyle added a commit that referenced this pull request Apr 5, 2019
This commit adds MiniTest coverage for tests that correspond to those
introduced in [#89] to increase parity between the two suites.

[#89]: #89
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 this pull request may close these issues.

None yet

4 participants