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

use JsonPointer for path handling #276

Merged
merged 2 commits into from
Aug 11, 2016
Merged

use JsonPointer for path handling #276

merged 2 commits into from
Aug 11, 2016

Conversation

steffkes
Copy link
Contributor

@steffkes steffkes commented Jun 3, 2016

crafting a new API, trying to follow the jsonapi-specs, i've realized that error-objects require a json pointer:

  • source: an object containing references to the source of the error, optionally including any of the following members:
    • pointer: a JSON Pointer [RFC6901] to the associated entity in the request document [e.g. /data for a primary data object, or /data/attributes/title for a specific attribute].

before making more changes, first thing to do was to get all tests working again, especially bringing back all the existing property paths since i don't intend to introduce a large BC which i probably still do? :/

having had a look at RFC6901 again, i'm curious why JsonPointer#getPropertyPathAsString does include a leading hashmark @jojo1981 - isn't that actually related to the filename rather than the path? checking other existing JsonPointer implementations in PHP, all use path expressions starting with a slash instead.

as a nice side-effect, using dots in keys wouldn't result in path that is rather hard to understand like tld.company.project[0].content (instead of /tld.company.project/0/content)

@mirfilip @bighappyface WDYT?

@steffkes
Copy link
Contributor Author

steffkes commented Jun 3, 2016

bummer, first commit and already broke the 5.3 build - didn't check the library requirements beforehand .. sorry for that, already pushed a fix using the old array syntax

@jojo1981
Copy link

jojo1981 commented Jun 3, 2016

@steffkes According the RFC6901 a # evaluate to the the whole document.

public function testJsonPointerWithPropertyPaths()
{
$initial = new JsonPointer('#/definitions/date');
$modified = $initial->withPropertyPaths(array('~definitions/general', '%custom%'));
Copy link

@jojo1981 jojo1981 Jun 3, 2016

Choose a reason for hiding this comment

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

To prove withPropertyPaths is immutable. The code needs to be changed here into:

$initial = new JsonPointer('#/definitions/date');

$this->assertEquals(array('definitions', 'date'), $initial->getPropertyPaths());
$this->assertEquals('#/definitions/date', $initial->getPropertyPathAsString());

$modified = $initial->withPropertyPaths(array('~definitions/general', '%custom%'));
$this->assertEquals(array('definitions', 'date'), $initial->getPropertyPaths());
$this->assertEquals('#/definitions/date', $initial->getPropertyPathAsString());

$this->assertEquals(array('~definitions/general', '%custom%'), $modified->getPropertyPaths());
$this->assertEquals('#/~0definitions~1general/%25custom%25', $modified->getPropertyPathAsString());

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jojo1981 i'm fine adding those two lines - would you mind explaining why you think they are necessary? i can't get my head around, what are those lines asserting that the current test doesn't? it's rather a duplication of the existing tests - which obviously doesn't hurt, but it still doesn't relate to the question if the method returns a new object or modified the existing

if we want to have additional checks, what about assertNotSame for $initial and $modified ?

Copy link

Choose a reason for hiding this comment

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

@steffkes It depends on which behavior should be tested. Asserting a new object is returned is 1 thing. asserting this has no side effect is another thing (so the original object has NOT be changed). I think both can be tested (so proved).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jojo1981 that still doesn't answer my question for more explanation about your intention. the second assert block for $initial already implies that it was okay in the first place - i don't see what kind of errors we could avoid by testing that (again). i'm not opposed to your suggestions, not at all, just want to understand if there is a real improvement that i'm just not seeing right now

@steffkes
Copy link
Contributor Author

steffkes commented Jun 9, 2016

i went ahead an included a pointer property in all error instances. since the jsonapi-specs said the pointer was supposed to look like /data/attributes/title i do call ltrim to get rid of the leading hashmark we do have - not entirely sure if this is the right thing to do here? probably have to re-read the specs (RFC as well as jsonapi) again to get a better idea how to handle it.

@steffkes
Copy link
Contributor Author

anyone like to have a second look? @bighappyface, probably @mirfilip or @jojo1981 (again, if you didn't already?)

the newly added PointerTest should make it clear how it's supposed to look and work. if there are any other places where we should add it, please let me know.

@bighappyface
Copy link
Collaborator

@steffkes I'll schedule some time to review this in the coming days. I'm super slammed at the moment but very grateful for your work.

@steffkes
Copy link
Contributor Author

no worries @bighappyface, no need to rush it - thanks for your feedback. let me know if you have questions and/or suggestions, i'll wait for it. spare time is sometimes rare, been there too (:

@bighappyface
Copy link
Collaborator

@steffkes would you please rebase this on master and squash the commits down to one?

@steffkes
Copy link
Contributor Author

@bighappyface i'm not sure that it went entirely as planned - didn't need rebasing/squashing that often in the past. if it doesn't look like it should - please do let me know. if you can i would appreciate it if you could tell me where i got it wrong.

i expected the other commits to disappear from this PR after doing all the steps i found described on How-to-Rebase-a-Pull-Request. at least this is what i looks like in my local git history ..

@steffkes
Copy link
Contributor Author

steffkes commented Jul 1, 2016

ah, looks like it's working - maybe some sort of cache on github that kept the older commits for the time being. the only commit i can see now is the latest that was created during the whole rebase/squash operation.

@bighappyface
Copy link
Collaborator

Squash looks good

@steffkes
Copy link
Contributor Author

gentle reminder, no rush here - if you need anything else from me, drop me a note @bighappyface

@bighappyface
Copy link
Collaborator

@steffkes my apologies for yet another rebase on master but some changes in another PR have put this into conflict. Would you mind rebasing on master once more? You shouldn't have to worry about squashing anymore.

@steffkes
Copy link
Contributor Author

steffkes commented Aug 2, 2016

@bighappyface i should have checked that before - thanks for the heads up. did another rebase - the diff looks good again. if you need anything else, let me know

@bighappyface
Copy link
Collaborator

@steffkes no worries.

Would you mind reviewing the test failures?

@steffkes
Copy link
Contributor Author

steffkes commented Aug 2, 2016

argh, should have done that before as well .. will do it now -- should be working again, will wait for the tests to be finished ...

@steffkes
Copy link
Contributor Author

steffkes commented Aug 2, 2016

working again @bighappyface

@bighappyface
Copy link
Collaborator

@jojo1981 @mirfilip @steffkes @FlorianSW would you mind taking a look at this PR?

I am thinking this would get bundled into a major version release to avoid any BC issues.

{
$validator = $this->getFactory()->createInstanceFor('enum');
$validator->check($value, $schema, $path, $i);

$this->addErrors($validator->getErrors());
}

protected function checkFormat($value, $schema = null, $path = null, $i = null)
/**
* Checks format of a element
Copy link
Contributor

Choose a reason for hiding this comment

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

an element

@steffkes
Copy link
Contributor Author

steffkes commented Aug 5, 2016

anyone? anything else? :)

@bighappyface
Copy link
Collaborator

+1

@bighappyface bighappyface merged commit 58b6e07 into jsonrainbow:master Aug 11, 2016
@thewilkybarkid thewilkybarkid mentioned this pull request Aug 26, 2016
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.

4 participants