Steps to reproduce:
- Drupal 8.5.1 + php 7.1
- Install drupal with standard profile
- Install jsonapi + schemata_json_schema modules
- access to /jsonapi/node/page (OK)
- create a page node
- access to /jsonapi/node/page (KO)

The website encountered an unexpected error. Please try again later.
AssertionError: A JSON API response failed validation (see the logs for details). Please report this in the issue queue on drupal.org in assert() (line 182 of modules/contrib/jsonapi/src/EventSubscriber/ResourceResponseSubscriber.php).
assert(, 'A JSON API response failed validation (see the logs for details). Please report this in the issue queue on drupal.org') (Line: 182)
Drupal\jsonapi\EventSubscriber\ResourceResponseSubscriber->doValidateResponse(Object, Object) (Line: 172)
Drupal\jsonapi\EventSubscriber\ResourceResponseSubscriber->onResponse(Object, 'kernel.response', Object)
call_user_func(Array, Object, 'kernel.response', Object) (Line: 111)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.response', Object) (Line: 191)
Symfony\Component\HttpKernel\HttpKernel->filterResponse(Object, Object, 1) (Line: 173)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 68)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 99)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 78)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 40)
Drupal\jsonapi\StackMiddleware\FormatSetter->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 50)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 664)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

Comments

akalam created an issue. See original summary.

akalam’s picture

Issue summary: View changes
e0ipso’s picture

Status: Active » Postponed (maintainer needs more info)

@akalam can you share the validation error present in your logs?

alesbencina’s picture

Might be a problem related to schemata module. Which version are you using and schema?

akalam’s picture

Status: Postponed (maintainer needs more info) » Active

@e0ipso
Here the validation messages present in the error log.

Response failed validation. Response: {"type":"node--page","id":"dbfdfa59-46b8-4bf8-9cbb-81ccaaae5d66","attributes":{"nid":1,"uuid":"dbfdfa59-46b8-4bf8-9cbb-81ccaaae5d66","vid":1,"langcode":"en","revision_timestamp":1522830053,"revision_log":null,"status":true,"title":"Praesent sapien massa, convallis a pellentesque nec","created":1522830032,"changed":1522830053,"promote":false,"sticky":false,"default_langcode":true,"revision_translation_affected":true,"path":{"alias":null,"pid":null,"langcode":"en"},"body":{"value":"\u003Cp\u003EPraesent sapien massa, convallis a pellentesque nec, egestas non nisi. Vestibulum ac diam sit amet quam vehicula elementum sed sit amet dui. Proin eget tortor risus. Nulla porttitor accumsan tincidunt. Proin eget tortor risus. Praesent sapien massa, convallis a pellentesque nec, egestas non nisi. Praesent sapien massa, convallis a pellentesque nec, egestas non nisi. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Donec velit neque, auctor sit amet aliquam vel, ullamcorper sit amet ligula. Pellentesque in ipsum id orci porta dapibus. Vestibulum ac diam sit amet quam vehicula elementum sed sit amet dui.\u003C\/p\u003E\r\n","format":"basic_html","processed":"\u003Cp\u003EPraesent sapien massa, convallis a pellentesque nec, egestas non nisi. Vestibulum ac diam sit amet quam vehicula elementum sed sit amet dui. Proin eget tortor risus. Nulla porttitor accumsan tincidunt. Proin eget tortor risus. Praesent sapien massa, convallis a pellentesque nec, egestas non nisi. Praesent sapien massa, convallis a pellentesque nec, egestas non nisi. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Donec velit neque, auctor sit amet aliquam vel, ullamcorper sit amet ligula. Pellentesque in ipsum id orci porta dapibus. Vestibulum ac diam sit amet quam vehicula elementum sed sit amet dui.\u003C\/p\u003E","summary":""}},"relationships":{"type":{"data":{"type":"node_type--node_type","id":"08849151-1e4c-4644-b323-89ed49de4e42"},"links":{"self":"http:\/\/local.solvay.com\/jsonapi\/node\/page\/dbfdfa59-46b8-4bf8-9cbb-81ccaaae5d66\/relationships\/type","related":"http:\/\/local.solvay.com\/jsonapi\/node\/page\/dbfdfa59-46b8-4bf8-9cbb-81ccaaae5d66\/type"}},"revision_uid":{"data":{"type":"user--user","id":"ef92e61b-6f61-4b36-be3c-5046a641247e"},"links":{"self":"http:\/\/local.solvay.com\/jsonapi\/node\/page\/dbfdfa59-46b8-4bf8-9cbb-81ccaaae5d66\/relationships\/revision_uid","related":"http:\/\/local.solvay.com\/jsonapi\/node\/page\/dbfdfa59-46b8-4bf8-9cbb-81ccaaae5d66\/revision_uid"}},"uid":{"data":{"type":"user--user","id":"ef92e61b-6f61-4b36-be3c-5046a641247e"},"links":{"self":"http:\/\/local.solvay.com\/jsonapi\/node\/page\/dbfdfa59-46b8-4bf8-9cbb-81ccaaae5d66\/relationships\/uid","related":"http:\/\/local.solvay.com\/jsonapi\/node\/page\/dbfdfa59-46b8-4bf8-9cbb-81ccaaae5d66\/uid"}}},"links":{"self":"http:\/\/local.solvay.com\/jsonapi\/node\/page\/dbfdfa59-46b8-4bf8-9cbb-81ccaaae5d66"}} Errors: [{"property":"attributes.langcode","pointer":"\/attributes\/langcode","message":"String value found, but an object is required","constraint":"type","context":1},{"property":"attributes.revision_timestamp","pointer":"\/attributes\/revision_timestamp","message":"Invalid time 1522830053, expected integer of milliseconds since Epoch","constraint":"format","context":1,"format":"utc-millisec"},{"property":"attributes.revision_log","pointer":"\/attributes\/revision_log","message":"NULL value found, but a string is required","constraint":"type","context":1},{"property":"attributes.created","pointer":"\/attributes\/created","message":"Invalid time 1522830032, expected integer of milliseconds since Epoch","constraint":"format","context":1,"format":"utc-millisec"},{"property":"attributes.changed","pointer":"\/attributes\/changed","message":"Invalid time 1522830053, expected integer of milliseconds since Epoch","constraint":"format","context":1,"format":"utc-millisec"},{"property":"attributes.path.alias","pointer":"\/attributes\/path\/alias","message":"NULL value found, but a string is required","constraint":"type","context":1},{"property":"attributes.path.pid","pointer":"\/attributes\/path\/pid","message":"NULL value found, but an integer is required","constraint":"type","context":1}]

For more legible messages, I paste the unserialized error messages:

Array
(
    [0] => stdClass Object
        (
            [property] => attributes.langcode
            [pointer] => /attributes/langcode
            [message] => String value found, but an object is required
            [constraint] => type
            [context] => 1
        )

    [1] => stdClass Object
        (
            [property] => attributes.revision_timestamp
            [pointer] => /attributes/revision_timestamp
            [message] => Invalid time 1522830053, expected integer of milliseconds since Epoch
            [constraint] => format
            [context] => 1
            [format] => utc-millisec
        )

    [2] => stdClass Object
        (
            [property] => attributes.revision_log
            [pointer] => /attributes/revision_log
            [message] => NULL value found, but a string is required
            [constraint] => type
            [context] => 1
        )

    [3] => stdClass Object
        (
            [property] => attributes.created
            [pointer] => /attributes/created
            [message] => Invalid time 1522830032, expected integer of milliseconds since Epoch
            [constraint] => format
            [context] => 1
            [format] => utc-millisec
        )

    [4] => stdClass Object
        (
            [property] => attributes.changed
            [pointer] => /attributes/changed
            [message] => Invalid time 1522830053, expected integer of milliseconds since Epoch
            [constraint] => format
            [context] => 1
            [format] => utc-millisec
        )

    [5] => stdClass Object
        (
            [property] => attributes.path.alias
            [pointer] => /attributes/path/alias
            [message] => NULL value found, but a string is required
            [constraint] => type
            [context] => 1
        )

    [6] => stdClass Object
        (
            [property] => attributes.path.pid
            [pointer] => /attributes/path/pid
            [message] => NULL value found, but an integer is required
            [constraint] => type
            [context] => 1
        )

)

@alesbencina I'm ussing schemata last release 1.0-alpha3

wim leers’s picture

Title: "JSON API response failed validation" error message on node endpoints » Schemata + JSON API: "JSON API response failed validation" error message on node endpoints

Schemata just had a new release. That could be it.

            [message] => Invalid time 1522830032, expected integer of milliseconds since Epoch

It's not milliseconds, but seconds, so this is wrong in the schema.

            [message] => NULL value found, but a string is required

This is optional, so NULL is valid.

            [message] => NULL value found, but an integer is required

Same here.

Pinged the schemata maintainer: https://twitter.com/wimleers/status/981550577535651840.

logickal’s picture

Just to chime in here, we are also seeing this after upgrading jsonapi. I agree that the issue looks like the schema that Schemata generates is wrong based on entity and fields as configured, but not sure that it's just their latest version, as I tested against their commits from October and still failed. We are going to try to take a look at the schema generation and type_mappers and see if we can find some clues there.

wim leers’s picture

Interesting! We're very confident we didn't change JSON API response documents thanks to our test coverage — git d 8.x-1.14 8.x-1.15 -- tests/src/Functional would have to show significant changes if we'd broken BC in JSON API.

It'd be wonderful if you could dig into this and determine the root cause! 👍

brockfanning’s picture

I'm new to this suite of modules, but a couple thoughts based on what I've found so far:

  1. These errors only appear if ASSERT_ACTIVE is set to TRUE, like if settings.php has assert_options(ASSERT_ACTIVE, TRUE);
  2. I've opened an issue and submitted a first-draft patch for the "NULL value found" errors in the Schemata issue queue: #2958251: Allow null value for non-required fields (json schema)
wim leers’s picture

@brockfanning: Welcome, and thank you for opening #2958251: Allow null value for non-required fields (json schema). I can confirm that your statement about ASSERT_ACTIVE is correct :)

Grayside’s picture

Thanks for pinging me Wim.

Schemata was overdue for a release for some time, is there confirmation this was working with Schemata alpha2?

e0ipso’s picture

wim leers’s picture

Schemata was overdue for a release for some time, is there confirmation this was working with Schemata alpha2?

Can somebody who's affected by this please test that? Thanks!

brockfanning’s picture

I just tested with Schemata 1.0-alpha2, JSON API 1.14, and JSON API Extras 2.0-rc1 - and I'm seeing similar errors as above - such as the langcode ("object is required"), created/changed (milliseconds vs seconds), and the fields not accepting NULL values.

I also tested with Schemata 1.0-alpha3, JSON API 1.15, and JSON API Extras 2.0-rc1 - and I get the same results.

One possible reason for differing experiences might be that the appearance of errors is affected by the "fields" query parameter. For example, if my GET request includes &fields[my_content_type]=title,changed then I will see the validation error about milliseconds for 'changed', but if my GET request only includes &fields[my_content_type]=title then I won't see that validation error.

A side note: This effect of the "fields" query parameter can also cause issues when required fields are not included in the parameter value. For example, if my content type has a required field called "foo", and my GET request includes &fields[my_content_type]=title,foo then I won't see any validation errors. But if my GET request only includes &fields[my_content_type]=title then I will see a validation error about foo being required.

wim leers’s picture

brockfanning’s picture

Project: JSON:API » JSON:API Extras
Version: 8.x-1.15 » 8.x-2.x-dev

Hopefully I haven't misinterpreted something in my testing, but I think this problem stems from dependencies of jsonapi_extras:2.0.0-rc1. When I revert to jsonapi_extras:1.0.0-rc7 the validation errors go away. I can clearly see that when I do a `composer install` after upgrading to jsonapi_extras:2.0.0-rc1, the justinrainbow/json-schema package is installed, and when I revert back to 1.0.0-rc7, the justinrainbow/json-schema package is removed. I believe this is caused by e0ipso/shaper, which has a dependency on json-schema:^5.2 (https://github.com/e0ipso/shaper/blob/master/composer.json#L20).

I'll go out on a limb and move this to the jsonapi_extras 2.0 queue, but feel free to move it back if I'm off base.

wim leers’s picture

@brockfanning: Thanks so much for digging into this!

s_leu’s picture

Had the same problem. Atfer updating the following modules to these versions, it was gone:

        "drupal/jsonapi": "^1.23",
        "drupal/jsonapi_extras": "^2.5",
        "drupal/schemata": "1.x-dev"
logickal’s picture

I think this is resolved with the latest 1.x of jsonapi and 2.x of jsonapi_extras as noted above by @s_leu. We have re-enabled schemata and openapi modules and are no longer seeing validation errors.

e0ipso’s picture

Status: Active » Fixed

Fantastic! Thanks for the feedback @s_leu and @logickal!

I'll close this issue then.

e0ipso’s picture

wim leers’s picture

🎉

e0ipso’s picture

FWIW I suspect that the fix was a patch to Schemata. I already released that patch in tagged version, so you don't have to rely in `1.x-dev`.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.