Problem/Motivation

On configuring a REST view display, selecting a format different from json will result on correct formatting on the preview but json on the view results.
Note: this only happens on entity (not fields) based displays.

Proposed resolution

Remove the check on view->preview on the render method from core/modules/rest/src/Plugin/views/style/Serializer.phpand default to the displayHandler

Beta phase evaluation

Reference: https://www.drupal.org/core/beta-changes
Issue category Bug because the formatting of REST view displays don't use their format configuration properly.
Issue priority Normal because it is not widespread and is self-contained, and the fallback does work properly.
Prioritized changes The main goal of this issue is fixing a normal bug.

Comments

pcambra’s picture

Status: Active » Needs review
StatusFileSize
new889 bytes

Status: Needs review » Needs work

The last submitted patch, 1: 2396253-rest_views_default_format-1.patch, failed testing.

pcambra’s picture

Status: Needs work » Needs review
StatusFileSize
new1.63 KB
new1.49 KB

Here's another take.

I'm trying to provide a test with this but I'm struggling on how to force a preview in StyleSerializerTest.php.

pcambra’s picture

Issue summary: View changes
dawehner’s picture

Ah this makes sense!
Can we fix the test coverage?

pcambra’s picture

Can we fix the test coverage?

I've tried but I don't know how to fake the preview in views properly, some indications are appreciated :)

dawehner’s picture

Issue tags: +VDC, +Needs tests

.

klausi’s picture

StyleSerializerTest::testLivePreview() is already testing this, so you could add it there or do something similar?

klausi’s picture

Status: Needs review » Needs work
geertvd’s picture

This one is probably blocked until Live preview for REST display is fixed #2443119: Views preview not working for REST display.

geertvd’s picture

Status: Needs work » Needs review
Issue tags: +drupaldevdays
StatusFileSize
new2.26 KB
new3.54 KB

Added tests for this.

The last submitted patch, 11: 2396253-rest_views_default_format-11-test.patch, failed testing.

Anonymous’s picture

Issue summary: View changes
Status: Needs review » Needs work
Issue tags: -Needs tests

This looks good for me, and I think the test coverage is sufficient.

Added a beta evaluation to the summary.

I only have on minor nitpick. RTBC after that is fixed.

+++ b/core/modules/rest/src/Plugin/views/style/Serializer.php
@@ -125,10 +125,10 @@ public function render() {
+    // Get the content type configured in the display or fallback to the
+    // default.

The wrapping of this comment is wrong.

Anonymous’s picture

Status: Needs work » Reviewed & tested by the community

@geertvd pointed out to me that the wrapping of the comment mentioned in #13 actually is right, so I think this is good to go.

dawehner’s picture

+++ b/core/modules/rest/src/Plugin/views/style/Serializer.php
@@ -125,10 +125,10 @@ public function render() {
-    $content_type = $this->displayHandler->getContentType();
-    if (!empty($this->view->live_preview)) {
-      $content_type = $this->options['formats'] ? reset($this->options['formats']) : 'json';
-    }
+    // Get the content type configured in the display or fallback to the
+    // default.
+    $default_content_type = empty($this->view->live_preview) ? $this->displayHandler->getContentType() : 'json';
+    $content_type = $this->options['formats'] ? reset($this->options['formats']) : $default_content_type;
 
     return $this->serializer->serialize($rows, $content_type);

So what we do here, sounds a bit of a bug. Does that mean if we setup one of the formats, even if multiple, we never respect the requested format from the request, see RestExport::init(), that sounds wrong, to be honest

geertvd’s picture

Status: Reviewed & tested by the community » Needs review
Issue tags: +Needs tests
StatusFileSize
new864 bytes
new3.58 KB

Yes we where actually ignoring the requested format, this should fix that.
I guess we will need extra test coverage there.

geertvd’s picture

StatusFileSize
new1.42 KB
new3.1 KB
new4.43 KB

Added extra test coverage.

The last submitted patch, 16: 2396253-rest_views_default_format-16-complete.patch, failed testing.

The last submitted patch, 17: 2396253-rest_views_default_format-17-test.patch, failed testing.

geertvd’s picture

Ah the problem is the default request format in RestExport

  /**
   * Overrides the content type of the data response, if needed.
   *
   * @var string
   */
  protected $contentType = 'json';

Will check how to fix this later this weekend.

Status: Needs review » Needs work

The last submitted patch, 17: 2396253-rest_views_default_format-17-complete.patch, failed testing.

geertvd’s picture

StatusFileSize
new5.37 KB
new963 bytes

This should fix that test failure, it seems like there should be a better way to do this though.

geertvd’s picture

Status: Needs work » Needs review
geertvd’s picture

StatusFileSize
new1.17 KB
new5.86 KB

Bit better

The last submitted patch, 22: 2396253-rest_views_default_format-22-complete.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 24: 2396253-rest_views_default_format-24-complete.patch, failed testing.

geertvd’s picture

Status: Needs work » Needs review
StatusFileSize
new1.71 KB
new5.37 KB
dawehner’s picture

Issue tags: -Needs tests

This looks perfect now, thank you a lot!

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Setting to RTBC

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 27: 2396253-rest_views_default_format-27-complete.patch, failed testing.

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 27: 2396253-rest_views_default_format-27-complete.patch, failed testing.

geertvd’s picture

Status: Needs work » Needs review
StatusFileSize
new5.36 KB

Reroll

geertvd’s picture

Status: Needs review » Reviewed & tested by the community

Setting RTBC as per #29

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

This issue is a normal bug fix, and doesn't include any disruptive changes, so it is allowed per https://www.drupal.org/core/beta-changes. Committed 6e83e1f and pushed to 8.0.x. Thanks!

Thanks for adding the beta evaluation to the issue summary.

  • alexpott committed 6e83e1f on 8.0.x
    Issue #2396253 by geertvd, pcambra, dawehner, klausi, pjonckiere:...

Status: Fixed » Closed (fixed)

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