xiaohutai / jsonapi

[Bolt Extension] JSON API for Bolt CMS
https://market.bolt.cm/view/bolt/jsonapi
MIT License
55 stars 18 forks source link

ContextErrorException #34

Closed Wartybliggens closed 7 years ago

Wartybliggens commented 7 years ago

This extension used to work but now I'm getting an exception that I cannot figure out. I did not do anything to the code, as it's a fresh install of Bolt 3.2.3, and this is the only extension installed. I'm running Bolt in a Docker container but I don't think that is related.

For any content type the error is the same. For example, if the url is ".../json/entries" the error shows:

Uncaught Exception: ContextErrorException ContextErrorException in SelectQueryHandler.php line 74: Warning: preg_split() expects parameter 2 to be string, object given

$metadata = $repo->getClassMetadata();
        $allowedParams = array_keys($metadata->getFieldMappings());
        $cleanParams = [];
        foreach ($queryParams as $fieldSelect => $valueSelect) {
            $stack = preg_split('/ *(\|\|\|) */', $fieldSelect);
            $valueStack = preg_split('/ *(\|\|\|) */', $valueSelect);

The offended line is the last one above, and the problem is that "$valueSelect" is coming in as a "Bolt\Extension\Bolt\JsonApi\Converter\Parameter\Type\Page Object" instead of as a string. I haven't a clue why this would start happening. Do you have any ideas?

Wartybliggens commented 7 years ago

There has been a lot of recent activity around SelectQueryHandler: https://github.com/bolt/bolt/commits/release/3.2/src/Storage/Query/Handler/SelectQueryHandler.php

Raistlfiren commented 7 years ago

Thanks @Wartybliggens, it seems as though the storage layer has changed somewhat since bolt version 3. The handler and directive used to be used when registered in the provider, now it doesn't seem to work with the latest update... I will try to ping rossriley and see what is going on. Thanks for the bug report.

GwendolenLynch commented 7 years ago

FWIW this problem might have been happening previously. Bolt 3.2 when debugging is enabled will show these types of errors as exceptions. Either way, I am having a dig right now to see.

Wartybliggens commented 7 years ago

Thanks guys. I am using Bolt specifically to be headless, so this is super-important for me anyway.

Stijn98s commented 7 years ago

I have the same thing when i use bolt 3.2.3. it works fine on 3.0.0. so if its that important you can go to that version of bolt

Wartybliggens commented 7 years ago

There have been some related fixes (in branch release/3.2), but now instead of an error the extension cannot find results for any content type, so the problem is not yet fixed. As suggested I may lock onto an earlier Bolt version for now, but I will watch for changes.

Wartybliggens commented 7 years ago

Thank you very much for fixing this!

A note: there are quite a number of PHP notices for this new version 3.0.7 of the extension with Bolt 3.2.3 in debug mode. Were your fixes paired to a future Bolt release?

Interestingly, the notices change depending on the content type. Here are the notices for Pages and Entries (where the second-to-last notice will repeat many times):

Notice: Undefined index: filter in /var/www/html/vendor/bolt/bolt/src/Storage/Query/ContentQueryParser.php on line 392

Notice: Undefined index: maxResults in /var/www/html/vendor/doctrine/dbal/lib/Doctrine/DBAL/Query/QueryBuilder.php on line 1095

Notice: Undefined index: firstResult in /var/www/html/vendor/doctrine/dbal/lib/Doctrine/DBAL/Query/QueryBuilder.php on line 1095

Notice: Undefined index: templatefields in /var/www/html/vendor/bolt/bolt/src/Storage/Field/Type/TemplateFieldsType.php on line 40

Notice: Undefined variable: repeatingFieldCollection in /var/www/html/extensions/vendor/bolt/jsonapi/src/Parser/Field/FieldFactory.php on line 104

Notice: Undefined index: data in /var/www/html/extensions/vendor/bolt/jsonapi/src/Parser/Field/FieldFactory.php on line 54

Notice: Undefined index: type in /var/www/html/extensions/vendor/bolt/jsonapi/src/Parser/Field/FieldFactory.php on line 54

In the case of my custom content type there are more of the same notices plus an additional one:

Notice: Undefined variable: attributes in /var/www/html/extensions/vendor/bolt/jsonapi/src/Parser/Field/Type/Taxonomy.php on line 19

FYI, the custom content type is "Jobs" and looks like this, in case you want to try reproing this:

jobs:
    name: Jobs
    singular_name: Job
    fields:
        title:
            type: text
            class: large
            group: content
        slug:
            type: slug
            uses: title
        teaser:
            type: markdown
            height: 100px
        body:
            type: markdown
            height: 300px
        requirements:
            type: markdown
            height: 300px
        geolocation:
            type: geolocation
            group: Media
    taxonomy: [ categories, tags ]
    show_on_dashboard: true
    viewless: true
    default_status: publish
    searchable: false
    sort: -datepublish
Raistlfiren commented 7 years ago

Yeah I noticed that as well with the recent update. I will try to polish off the release in the next few days when I get a chance. You can disable the "Notices" for right now using ~E_NOTICE. Thanks @Wartybliggens!

iamajoe commented 7 years ago

I'm also having the same issue. Accessing /api/posts give me the error:

ContextErrorException in ContentQueryParser.php line 392:
Notice: Undefined index: filter

This is my content type:

posts:
    name: Posts
    singular_name: Post
    slug: posts
    singular_slug: post
    fields:
        title:
            type: text
        url:
            type: text
        excerpt:
            type: text
            class: large
        thumb:
            type: image
            attrib: [title, alt]
            extensions: [jpg]
    show_on_dashboard: true
    viewless: true
    default_status: publish
    sort: -datepublish
    searchable: false
    icon_many: fa:address-book-o
    icon_one: fa:adress-card-o

@bobdenotter told me to:

set the error_level in config.yml to 8181 or 0 and it should “go away” until it’s fixed

and it works.

mamoot64 commented 7 years ago

Hi everyone,

I would like to reopen this issue.

On fresh install of Bolt 3.2.13, the extension (v 3.0.12) doesn't work.

With the tricks from @bobdenotter @Sendoushi , it's work

set the error_level in config.yml to 8181 or 0 and it should “go away” until it’s fixed

Nicolas

Raistlfiren commented 7 years ago

@acidline - The plugin doesn't work at all? Or is the error throwing error notices about undefined variables?

mamoot64 commented 7 years ago

Hi @Raistlfiren ,

Plugin works with the error_level to 8181 On notice type error, a exception is throw.

Raistlfiren commented 7 years ago

@acidline - The errors are coming from not the plugin itself, but because Doctrine and the Bolt Storage layer does not check to see if the values are defined. I would have to fix the errors from supporting libraries or include several isset statements in my code.

Raistlfiren commented 7 years ago

I just ran the latest version and encountered the same issues also. After setting the errorLevel to 8181 it fixed the problems.