-
-
Notifications
You must be signed in to change notification settings - Fork 582
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
Keep object key order when resolving a schema #481
Comments
Hey -- Some random brainstorming: What I'd initially imagine is an argument for a deserializer, and that deserializer being injectable with your version of JSON loading. But, even better I suspect would be to have There are after all at least 3 distinct tasks here:
And yeah how many objects that is, and whether any or all of them is |
The other issue is way more general than the problem i am facing right now. Also i am using the resolver locally with |
It is, yes, but I don't want 2 (which undoubtedly eventually becomes 3 and
4 and ..) when a decently designed interface solves both issues and seems
likely to solve future ones.
If such a solution exists that'd be worth doing, which is why I say it
needs thinking through before just adding arguments willy nilly.
…On Wed, Oct 24, 2018, 09:29 marmeladema ***@***.***> wrote:
The other issue is way more general than the problem i am facing right
now. Also i am using the resolver locally with file:// scheme, no HTTP
server involved.
But as a first step, we could add a deserializer argument to RefResolver
and call it in resolve_remote. Default value would still be json.loads.
This way, you could solve the yaml problem and this issue at the same time.
The .json() method from the requests module will not be used anymore
though, as all deserialization will use the same code path (which i think
if safer anyway).
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#481 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAUIXi7J7cVqab3lW5zpqIilF4jfxYg2ks5uoGs2gaJpZM4X3hYQ>
.
|
And by the way, requests' json method cannot just be replaced with
json.loads unfortunately, which is almost the whole reason we use requests.
It implements what the json spec says is a correct unicode decode dance,
which json.loads does not.
…On Wed, Oct 24, 2018, 09:32 Julian Berman ***@***.***> wrote:
It is, yes, but I don't want 2 (which undoubtedly eventually becomes 3 and
4 and ..) when a decently designed interface solves both issues and seems
likely to solve future ones.
If such a solution exists that'd be worth doing, which is why I say it
needs thinking through before just adding arguments willy nilly.
On Wed, Oct 24, 2018, 09:29 marmeladema ***@***.***> wrote:
> The other issue is way more general than the problem i am facing right
> now. Also i am using the resolver locally with file:// scheme, no HTTP
> server involved.
> But as a first step, we could add a deserializer argument to RefResolver
> and call it in resolve_remote. Default value would still be json.loads.
> This way, you could solve the yaml problem and this issue at the same
> time.
> The .json() method from the requests module will not be used anymore
> though, as all deserialization will use the same code path (which i think
> if safer anyway).
>
> —
> You are receiving this because you commented.
> Reply to this email directly, view it on GitHub
> <#481 (comment)>,
> or mute the thread
> <https://github.com/notifications/unsubscribe-auth/AAUIXi7J7cVqab3lW5zpqIilF4jfxYg2ks5uoGs2gaJpZM4X3hYQ>
> .
>
|
Do you think a PR as a "work-in-progress" base of discussion could help here ? Not meant to be merged of course. Just so that people can comment, and we can go toward finding a solution.
We can still use the |
Yep definitely makes sense on PR to propose something!
…On Wed, Oct 24, 2018, 09:44 marmeladema ***@***.***> wrote:
It is, yes, but I don't want 2 (which undoubtedly eventually becomes 3 and
4 and ..) when a decently designed interface solves both issues and seems
likely to solve future ones.
Do you think a PR as a "work-in-progress" base of discussion could help
here ? Not meant to be merged of course. Just so that people can comment,
and we can go toward finding a solution.
And by the way, requests' json method cannot just be replaced with
json.loads unfortunately, which is almost the whole reason we use requests.
It implements what the json spec says is a correct unicode decode dance,
which json.loads does not.
We can still use the guess_json_utf function from the requests module to
handle this dance properly.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#481 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAUIXmTkjdWyvZEZpI1AfjMgITVsK4j9ks5uoG6ngaJpZM4X3hYQ>
.
|
I had a similar use-case; I wanted to add a timeout to the request. Based on RefResolver.resolve_remote, I replaced all calls to def make_validator(schema, ..., timeout=TIMEOUT_IN_SECONDS):
# ..., figure out base_uri etc
def get_with_timeout(uri):
return requests.get(uri, timeout=timeout).json()
resolver = RefResolver(
base_uri=base_uri,
referrer=schema,
handlers={"http": get_with_timeout, "https": get_with_timeout},
)
return Draft7Validator(schema, resolver=resolver) This seems to work fine, although if the API settles down, I would just inherit from Your problem is similar. Luckily, on recent requests version (don't know how recent), you can specify extra keyword arguments for from __future__ import absolute_import, print_function, unicode_literals
import requests
from collections import OrderedDict
url = "https://api.github.com/users/octocat/repos"
requests.get(url).json(object_pairs_hook=OrderedDict) So you could use also use a function and custom def get_with_order(uri):
return requests.get(uri).json(object_pairs_hook=OrderedDict) Sure, it isn't super clean or optimal, and personally, I'd dump legacy Python and move to Python 3, but maybe this unblocks you? (Also, if there's a reason not use do this, I'd like to know) For the broader discussion, being able to mess around with RefResolver would be super useful because it does a lot, but I can understand the reluctance of having to support old designs. |
Subclassing isn't a supported public API for any classes in jsonschema :), but otherwise this looks reasonable.
Definitely keen to hear if you have any use cases that wouldn't be solved by what's in python-jsonschema/referencing#4! |
It would not work for me as i don't not want to provide a handler for a specific protocol, i need ordering whatever the protocol. I can not even fake the |
Fair enough, was hoping one of those approaches helps, but guess it's a niche use-case. My understanding is JSON specifies objects are unordered. Anyway, I'll add my thoughts to python-jsonschema/referencing#4 , didn't mean to hijack the issue! |
09fd353f Merge pull request #481 from kylef/kylef/time 0ed2e79b Fix negative time test to only fail on a single rule 2edc74b1 Add valid time with different second fractions 7bde0bf7 Add valid time with leap second including offset ee83f464 Stricter time format constraints 5732904a Merge pull request #480 from json-schema-org/ether/better-test-names c2994271 better test names for schema-items + additionalItems 6bc53e60 Merge pull request #479 from json-schema-org/fix-non-id-in-enum-for-drafts-6-and-7 3f783d9c fixing draft 6 & 7 non-id tests 5768c68d Merge pull request #476 from json-schema-org/ether/readme-updates 0c8bfc06 add mention of JSON::Schema::Tiny e4c10c6b fix markdown for underscores in package names eeb4db18 mention draft2020-12 in readme dff69dcb Merge pull request #474 from marksparkza/unevaluatedItems-depends-on-contains 51b4977c Merge pull request #478 from sorinsarca/patch-1 dfcd4a19 fix bad comma 4cb100a5 Merge pull request #465 from json-schema-org/ether/more-naive-ref 31dc86bc add another test of naive $ref replacement f858c613 Merge pull request #477 from json-schema-org/ether/more-items-tests 4e266c34 test that array-items/prefixItems adjusts the starting position for schema-items/additionalItems b7fced33 Merge pull request #473 from json-schema-org/ether/more-default-tests eadb9be7 test that a missing property is not populated by the default in the actual instance data 839b95d8 Added opis/json-schema 7cf78800 Add missing comma 3390c871 Update tests/draft2020-12/unevaluatedItems.json d3b88001 Update tests/draft2020-12/unevaluatedItems.json 84e1d5a9 Add another test case for unevaluatedItems-contains interaction f400802c Add tests for unevaluatedItems interaction with contains git-subtree-dir: json git-subtree-split: 09fd353fc44ab22e7e8998d096b3d6d83287e5e6
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
We (the library) no longer support <3.6 so I think this is closeable. If that's not the case and there's still what to address here feel free to follow up. |
As explained in the title, when resolving a schema on Python version (< 3.6) where dict implementation does not keep key insertion ordering, the object key order can be lost. This can be especially problematic for the "properties" object that is usually found in schemas.
I would be glad to provide a PR to fix this. My approach would be to provide a json_kwargs as RefResolver.init argument and forward it to json.loads in resolve_remote, so that users that need this functionality (at least me) could pass the object_pairs_hook argument with an OrderedDict value.
Would you be ok with that ?
The text was updated successfully, but these errors were encountered: