Giter VIP home page Giter VIP logo

Comments (8)

neomerx avatar neomerx commented on July 20, 2024

Can you please post some code to illustrate how to get the error? I do not quite get how you succeed with crashing it 😄 Thanks

from json-api.

efinder2 avatar efinder2 commented on July 20, 2024

I've modified the existing testIntegrationWithEncodingParameters. The crashed will be caused if the result of get Includes isn't "escaped" by iterableToArray.

Attention the assert in the withIncludedPaths prevents it with tests. It only throws when the assert is removed.:

 public function testCombination()
    {
        $profileUrl1 = 'http://example1.com/foo';
        $profileUrl2 = 'http://example2.com/boo';

        $queryParameters = [
            BaseQueryParser::PARAM_FIELDS  => [
                'comments' => Comment::LINK_AUTHOR.',     '.Comment::ATTRIBUTE_BODY.'      ',
                'people'   => Author::ATTRIBUTE_FIRST_NAME,
            ],
            BaseQueryParser::PARAM_SORT    => '-created,title,+updated',
            BaseQueryParser::PARAM_INCLUDE => Comment::LINK_AUTHOR.',   '.
                Comment::LINK_AUTHOR.'.'.Author::LINK_COMMENTS,
            BaseQueryParser::PARAM_PROFILE => urlencode(implode(' ', [$profileUrl1, $profileUrl2])),
        ];
        $parser = new BaseQueryParser($queryParameters);

        //
        // Now the main purpose of the test. Will it work with EncodingParameters?
        //

        // firstly setup some data
        $author = Author::instance(9, 'Dan', 'Gebhardt');
        $comments = [
            Comment::instance(5, 'First!', $author),
            Comment::instance(12, 'I like XML better', $author),
        ];
        $author->{Author::LINK_COMMENTS} = $comments;

        // and encode with params taken from the parser
        $actual = Encoder::instance(
            [
                Author::class  => AuthorSchema::class,
                Comment::class => CommentSchema::class,
            ]
        )
            ->withIncludedPaths($parser->getIncludes())
            ->encodeData($comments);
    }

from json-api.

neomerx avatar neomerx commented on July 20, 2024

Thanks. I'll have a look this weekend.

from json-api.

neomerx avatar neomerx commented on July 20, 2024

I am in doubt. The parser actually parses inputs paths and returns them 'parsed'. However, withIncludedPaths expects a list of dot-separated paths. And each of them is right and some sort of adapter is needed. Technically, the solution is simple and instead of

    ->withIncludedPaths($parser->getIncludes())

it is required to

    ->withIncludedPaths($this->iterableKeys($parser->getIncludes()))

where

    private function iterableKeys(iterable $iterable): iterable {
        foreach ($iterable as $key => $value) {
            yield $key;
        }
    }

What are you thoughts on this?

from json-api.

efinder2 avatar efinder2 commented on July 20, 2024

Thanks for your response. Now I'm confused. I don't understand why you put iterableToArray() as parameter in the test, but above you explain it should be iterableKeys?

Also isn't it the sence of a parser to prepare the parameter for later usage in the fetching and rendering of the output? Where do someone need additional data of the getInclude method?

In general it would be helpful to have more documentation. PhpDoc and wiki entries.
Is it ok for you, if I write some PhpDoc?
How to help you with the wiki? It couldn't be cloned

from json-api.

neomerx avatar neomerx commented on July 20, 2024

Sorry for confusion

I needed paths like post.comments.author to be split into ['post', 'comments', 'author'] so I could build database/ORM quires and load those relationships. Thus, the parser seemed to be a perfect candidate to do such parsing and have the parsed result as an array/Generator. However, I bumped the same problem as you, the Encoder wanted paths dot-separated because it needed to know which paths to follow and in what order (1 post, 2 post.comments and 3 post.comments.author in this case). So I decided to return both forms.

return [
    'post.comments.author' => ['post', 'comments', 'author'],
];

Though in a form of a Generator. Sorry, for being a bit complicated.

That is why taking keys works in this case. Technically, they are the same values as in the parser input though they are validated and guaranteed to have expected format, no trailing spaces and etc.

AFAIK, the Wiki can be edited by any logged-in Github user. Git repo is also available https://github.com/neomerx/json-api.wiki.git If you have any issues with the edit please post changes here and I will move them.

from json-api.

neomerx avatar neomerx commented on July 20, 2024

Thanks. I think it would be better to move the compatibility note to https://github.com/neomerx/json-api/wiki/Parsing-API-Parameters#query-parameters and rather than referring to 'tests' and provide a usage sample.

from json-api.

neomerx avatar neomerx commented on July 20, 2024

Released in 4.0.1

from json-api.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.