BUG: validate_colander_schema don't handle missing=drop#317
BUG: validate_colander_schema don't handle missing=drop#317surabujin wants to merge 1 commit intoCornices:masterfrom surabujin:fix-colander-schema
Conversation
|
Thanks for pointing that out. It misses tests to be merged, though. |
|
@surabujin - good catch! Can you also write some tests to cover this case? |
|
I have fixed existing test. Tomorrow I will add test for this particular case. |
|
Can anyone see my patch? If there are something wrong, I want to know it and fix it. |
|
@surabujin, I haven't had a chance to look over the substance of this patch, but there's a few things on the tests that don't look quite right... You've altered some schema that had |
|
"default" attribute in SchemaNode used only during .serialize() method. So using it here as "default" placeholder for .deserialize() is incorrect. Thats why I replace them with "missing", which one is used in .deserialize() method. It works before, only because "weird" scheme deserialise way: if attr.default != null:
deserialized = attr.deserialize(attr.serialize())it can/should be replaced with: if attr.missing is not required:
deserialized = attr.missing |
|
It looks like it's doing It seems like the whole if statement should just be |
|
Okay, this patch needs work... I just tried running the tests without the change in cornice/schemas.py and it runs without failure. You need to first write a test that demonstrates the issue with the existing code (a test that fails with the existing code but functions with the fix). Also, changes made to |
|
We don't use colander to serialize data, only to deserialize. According to colander documentation http://docs.pylonsproject.org/projects/colander/en/latest/api.html#schema-related :
So we should never use "default" attribute to SchemaNode objects. We should not make any decision based on value of "default" attribute. Current behaviour is leading to misunderstanding to people familiar with colander. PS I will add requested by you test. |
|
@surabujin - colander is used in this case primarily for validation. They're accomplishing that with To re-iterate, my points are:
|
|
Last time - current cornice behaviour is incorrect from colander point of view. We should not use foreign library in incorrect way, only because we like "default" attribute more than "missing". PS is validation != deserialisation? |
|
^_^ I'm not going to argue if cornice is using colander right or wrong. I'm just pointing out how it is doing it. And how it's doing validation is by calling |
|
Weird logic... Sounds like - better add one more spike, than fix root clause. |
|
@surabujin, perhaps you're confusion is with what "deserialize" does... Colander has no concept of JSON and it's serialized format is an internal-use format called "cstruct" which is a dict that contains only strings. "deserialize" is the process to translate the cstruct into a dict with more "normal" types (such as integers) or custom types and will throw exceptions if it encounters errors. So, the process of validation (as far as I understand it) is like this:
Any of those steps can throw an exception which indicates the validation failed. The colander steps will also make appropriate calls to validators and preparers. |
|
@tisdal look into the code, it acting in different way than you describe here. It you .serialize only if there is a not "null" default value. PS Looks like we need to limit maximum version of mock packages in dependencies, because py26 tests become broken. |
Function validate_colander_schema call attr.deserialize even if attr.missing == drop and data don't contain attr.name. This behaviour become a problem if attr have preparer, which one expect something except colander.null.
|
I concur with @tisdall here: if we want to fix how colander is used in cornice, we'll need to make a new non-compatible release of Cornice. This might be acceptable, but if possible we should avoid doing it. I propose to act in two steps:
How does that sound? |
|
I agree with @surabujin, the use of Colander library in Cornice was not sound, and was somehow confusing for people familiar with it. I think the PR #330 covers the issue you found here since the code fully now relies on Colander. It is probably the best candidate for the many fixes about schemas. But since @tisdall was discussing about |
|
We took a different approach, see https://github.com/Cornices/cornice/blob/master/docs/source/upgrading.rst#colander-validation |
Function validate_colander_schema call attr.deserialize even if
attr.missing == drop and data don't contain attr.name. This behaviour become
a problem if attr have preparer, which one expect something except
colander.null.