Skip to content

How is asserting None handled #603

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

Closed
humbienri opened this issue Sep 13, 2019 · 2 comments
Closed

How is asserting None handled #603

humbienri opened this issue Sep 13, 2019 · 2 comments

Comments

@humbienri
Copy link

humbienri commented Sep 13, 2019

Given this requests response.json() (note the None instead of null):

    "version": "2.3.4",
    "result": "Multi-Status",
    "data": [
        {
            "derId": 583,
            "warnings": [],
            "status": "success",
            "error": None
        }
    ]
}

why is it that this schema isn't sufficient:

    "$schema": "http://json-schema.org/draft-04/schema#",
    "title": "",
    "description": "JSON schema for the HTTP REST API response.",
    "type": "object",
    "properties": {
        "version": {"type": "string"},
        "result": {"type": "string"},
        "data": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "derId": {"type": "integer"},
                    "warnings": {"type": "array"},
                    "status": {"type": "string"},
                    "error": {"type": ["null","string"]}
                },
                "required": ["derId", "status", "warnings", "error"]
            }
        }
    },
    "required": ["data", "result", "version"]
}

Even if I remove the requirement for the error property, jsonschema.validate() chokes on the presence of None.

I realize that None is a python thing and not valid json, as I understand it, but it is the calling of the .json() api on the API's response that introduces the None.

Thank you in advance.

@Julian
Copy link
Member

Julian commented Sep 13, 2019

Hi, please include a full reproducer (the code you're running + its traceback).

Note though that, presuming you're talking about requests, that requests.Response.json does not return JSON, it returns Python objects, and that jsonschema.validate is expecting exactly that.

@humbienri
Copy link
Author

humbienri commented Sep 16, 2019

Hello,
Sorry Julian, I did not include my actual code b/c it would get a bit tricky since my code is spread out across multiple modules and classes. I guess I could doctor up an actual example to trigger this rather than modifying my code for illustrative purposes.

But I do think you've hit the nail on the head. Yes I am using python's requests but seems like you are saying that *.json() does not output valid json?

One thing I did notice is that no matter how much I fiddled with my code I kept seeing an error regarding Assert None and instructed to use Assert is None. Your questions prompted me to try and reduce this down to just the commandline and try to reproduce this from there. So, import jsonschema and such and I was not able to reproduce the issue I was encountering. Can't believe I spend days on this and it was totally my fault.

I think somewhere along the way I forgot that the validate() returns None on a successful validation? So my assert valid_per_schema(blah dih dah) was essentially boiling down to assert None and it was pytest that was whining at me!!

Classic case of "we see what we want to see". Sheesh!

Thanks again @Julian! Sorry for the hassle.

Julian added a commit that referenced this issue Nov 28, 2022
78c888273 Merge pull request #618 from json-schema-org/gregsdennis/contentschema-should-fail-content
5cbc53bc0 Merge pull request #613 from santhosh-tekuri/rjp-multidigit
bb000ce98 Merge pull request #620 from json-schema-org/ether/remove-unused-remotes
c4c490f1d Merge pull request #617 from json-schema-org/gregsdennis/dynamicAnchor-inside-propertyDependencies
0189831a9 remove schemas that are never referenced
cf1b94249 Merge pull request #610 from handrews/rm-remotes
d6490e817 move anchors into defs
2f9d117c0 Merge pull request #616 from json-schema-org/gregsdennis/propertyDependencies-and-unevaluated
4e5649cd0 move tests to draft-next
a41f2f6c4 added type:object to contentSchema schemas
2f50e7864 add tests for $dynamicAnchor in multiple branches of propertyDependencies
4794a1991 add tests for unevaluatedProperties seeing inside propertyDependencies
27cc299f3 Add RJP test 'multi-digit integer prefix'
716b95d94 Merge pull request #612 from santhosh-tekuri/rjp-positive
320c804d1 Add RJP test 'explicit positive prefix'
c8f210c39 Merge pull request #611 from santhosh-tekuri/time-alpha
3faeb222d add time test 'contains letters'
642441f2d Merge pull request #603 from santhosh-tekuri/uuid-nonstr
94d5043c7 add non-string uri tests
0c81374a2 Remove unneeded remotes
97a3e2156 Merge pull request #608 from json-schema-org/json-everything-uses-the-test-suite
bdaf7e8be added json-everything to 'who uses' section of readme; removed manatee.json (deprecated)
f00ec1008 Merge pull request #606 from santhosh-tekuri/duration-nounit
134480721 Merge pull request #607 from santhosh-tekuri/time-offsetprefix
dd4538eee Test time format 'offset not starting with plus or minus'
80fe2db15 test duration format 'element without unit'
38ea15116 Merge pull request #604 from santhosh-tekuri/time-offset
613ec170e second fraction, not offset
ee4bd4eb6 Add time format test with second fraction, no offset
86c2517cd Merge pull request #605 from santhosh-tekuri/rjp-empty
cfe80006a Add relative-json-pointer test with empty string
31796b3b8 add non-string uuid tests

git-subtree-dir: json
git-subtree-split: 78c8882732bcdc2dad81cd7ce1e3f9bca6fb7a9d
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

No branches or pull requests

2 participants