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

3.1 - Jsonview auto serialize #6502

Merged
merged 8 commits into from
May 9, 2015
Merged

3.1 - Jsonview auto serialize #6502

merged 8 commits into from
May 9, 2015

Conversation

ADmad
Copy link
Member

@ADmad ADmad commented May 6, 2015

Refs #6500

@ADmad ADmad added the view label May 6, 2015
@ADmad ADmad added this to the 3.1.0 milestone May 6, 2015
@ADmad ADmad changed the title Jsonview auto serialize 3.1 - Jsonview auto serialize May 6, 2015
* views to provide layout-like functionality.
* If you need the regular view template processing you can set `_serialize` to
* false which will turn off automatic serialization and instead view template
* and layotu will be used.
Copy link
Contributor

Choose a reason for hiding this comment

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

nitpick typo in layout

Copy link
Member Author

Choose a reason for hiding this comment

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

Not a nitpick, this ends up in API docs :)

@ceeram
Copy link
Contributor

ceeram commented May 6, 2015

lgtm

@ceeram
Copy link
Contributor

ceeram commented May 6, 2015

Shouldnt XmlView behave similar?

@markstory
Copy link
Member

XmlView should behave in a similar way. Having them be different will be not good. Also this change could be interpreted as an API breaking change. If an app was relying on using view files, it would now default to serializing which could be unexpected. What was wrong with the serialize => true approach?

@ADmad
Copy link
Member Author

ADmad commented May 7, 2015

What was wrong with the serialize => true approach?

It would still require adding $this->set('_serialize', true) in all actions which isn't DRY.

ADmad added 2 commits May 7, 2015 11:34
Doing so will serialize all available view variables.
Set '_serialize' to false to turn off serialization.
@ADmad ADmad force-pushed the jsonview-auto-serialize branch from 5a27b48 to 9893894 Compare May 7, 2015 06:11
* When the view is rendered, the `$posts` view variable will be serialized
* into JSON.
* By default all view variables will be serialized to JSON. So for above example
* the `$posts` view variable will be serialized into JSON.
Copy link
Member

Choose a reason for hiding this comment

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

Is this still true?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes it is.

@josegonzalez
Copy link
Member

Seems like an API break, and perhaps a serious one for certain users. As such, I'm 👎 .

Do we want to move to breaking the API in minor releases?

@patrickconroy
Copy link
Contributor

Would a controller property work? So, setting serialize in your AppController to true would auto-serialize the json view? And it'd default to false?

Right now all my json stuff has that serialize view property which is cumbersome.

@dereuromark
Copy link
Member

@patrickconroy You can set $this->set('_serialize', true) in your AppController. This way it would not hurt if some controllers don't use it, but it would apply to all by default that use JsonView.
That is pretty DRY in my book - and it is fully BC.

So that's what we should do here IMO.
I think less magic and explicitly setting it to true is reasonable.

@ADmad
Copy link
Member Author

ADmad commented May 7, 2015

I'll switch back to not serializing by default to avoid BC issues. Instead can document the fact that one can set $this->set('_serialize', true) in AppController::beforeRender() to get serializing as default behavior.

@markstory
Copy link
Member

Instead can document the fact that one can set $this->set('_serialize', true) in AppController::beforeRender() to get serializing as default behavior.

This sounds like a much safer plan. We could even make this part of the AppController in the project skeleton.

@ADmad
Copy link
Member Author

ADmad commented May 7, 2015

Done.

@dereuromark
Copy link
Member

This would probably have fit into 3.0 now as well. But with 3.1 it can be communicated better.
Also we should make XmlView allow true here.

@ADmad
Copy link
Member Author

ADmad commented May 7, 2015

This would probably have fit into 3.0 now as well.

No it won't as this change causes this test to fail.

@dereuromark
Copy link
Member

Doesn't make much sense, as you said :) But all right, 3.1 is fine.

ADmad added 2 commits May 8, 2015 12:03
It contains common code used by view which return serialized data.
@ADmad
Copy link
Member Author

ADmad commented May 8, 2015

  • Implemented support for _serialize true for XmlView too.
  • Pulled out duplicate code from JsonView and XmlView to SerializedView

}
parent::loadHelpers();
}
public $_responseType = 'xml';
Copy link
Contributor

Choose a reason for hiding this comment

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

why underscore public attribute?

Copy link
Member Author

Choose a reason for hiding this comment

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

Mistake in all 3 classes. Should be protected.

$response->type('json');
}
}
public $_responseType = 'json';
Copy link
Contributor

Choose a reason for hiding this comment

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

and here

@markstory
Copy link
Member

Looks good to me. This looks far safer than the original approach.

ADmad added a commit that referenced this pull request May 9, 2015
3.1 - Add support for `_serialize` = `true` for JsonView and XmlView
@ADmad ADmad merged commit 2b6821a into 3.1 May 9, 2015
@ADmad ADmad deleted the jsonview-auto-serialize branch May 9, 2015 04:00
@patrickconroy
Copy link
Contributor

Hey guys. With the way _serialize=true is working it doesn't seem possible to have it work when you have a single var.

$status = 1;
$this->set(compact('status') + ['_serialize' => true]);

If you do the above right now, you get this response, which isn't valid JSON:

1

However, if you have more than one var, it works as expected:

$status = 1;
$a = 'b';
$this->set(compact('status', 'a') + ['_serialize' => true]);

Returns:

{
    "status": 1,
    "a": "b"
}

The unexpectedness of that makes me think this is a bug, but it's possible that's as intended.

@markstory
Copy link
Member

@patrickconroy Could you open a new issue with that?

@patrickconroy
Copy link
Contributor

Sure np.

@markstory
Copy link
Member

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants