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

REST JSONP response serialization #10943

Closed
progmult opened this issue Feb 23, 2016 · 4 comments
Closed

REST JSONP response serialization #10943

progmult opened this issue Feb 23, 2016 · 4 comments

Comments

@progmult
Copy link

I use JSONP as a response format when creating REST API:

public function actionIndex()
{
            \Yii::$app->response->format = \yii\web\Response::FORMAT_JSONP;
            $callback = \Yii::$app->request->queryParams['callback'];

            $searchModel = new RaspisanSearch();
            $data = $searchModel->search(\Yii::$app->request->queryParams);

            return ['data' => $data, 'callback' => $callback];
}

But actual response is serialized ActiveProvider:

callbackfunctionname({"query":{"sql":null,"on":null,"joinWith":null,"select":null,"selectOption":null,"distinct":null,"from":null,"groupBy":null,"join":null,"having":null,"union":null,"params":[],"where":null,"limit":null,"offset":null,"orderBy":null,"indexBy":null,"modelClass":"app\\models\\Raspisan","with":null,"asArray":null,"multiple":null,"primaryModel":null,"link":null,"via":null,"inverseOf":null},"key":null,"db":null,"id":null});

A problem orrurs in yii\rest\Serializer, which serialize() method don't understand ['data' => $data, 'callback' => $callback] format and return data without serialization.

public function serialize($data)
    {
        if ($data instanceof Model && $data->hasErrors()) {
            return $this->serializeModelErrors($data);
        } elseif ($data instanceof Arrayable) {
            return $this->serializeModel($data);
        } elseif ($data instanceof DataProviderInterface) {
            return $this->serializeDataProvider($data);
        } else {
            return $data;
        }
    }
@progmult
Copy link
Author

As a solution I add new property in \yii\base\Response

/**
 * @var array of parameters. This parameters provided to response $format
 */
public $formatParams;

and then change yii\web\JsonResponseFormatter

protected function formatJsonp($response)
{
    $response->getHeaders()->set('Content-Type', 'application/javascript; charset=UTF-8');
    if ($response->data !== null && isset($response->formatParams['jsonpCallback'])) {
        $response->content = sprintf('%s(%s);', $response->formatParams['jsonpCallback'], Json::htmlEncode($response->data));
    } elseif ($response->data !== null) {
        $response->content = '';
        Yii::warning("The 'jsonp' response requires that jsonpCallback parameter to be set", __METHOD__);
    }
}

Fin

public function actionIndex()
{       
    \Yii::$app->response->format = \yii\web\Response::FORMAT_JSONP;
    \Yii::$app->response->formatParams['jsonpCallback'] = \Yii::$app->request->queryParams['callback'];

    $searchModel = new RaspisanSearch();
    return $searchModel->search(array(\Yii::$app->request->queryParams));
}

@mdmunir
Copy link
Contributor

mdmunir commented May 9, 2016

👍
prever directly use Yii::$app->request->get('callback') in yii\web\JsonResponseFormatter.
But it going break bc.

if (($callback = Yii::$app->request->get('callback')) !== null) {
    $response->content = sprintf('%s(%s);', $callback, Json::htmlEncode($response->data));
} elseif ($response->data !== null) {
    $response->content = '';
    Yii::warning("The 'jsonp' response requires that jsonpCallback parameter to be set", __METHOD__);
}

@mdmunir
Copy link
Contributor

mdmunir commented May 10, 2016

Also prefer to split jsonformater and jsonpformater into two classes. Both of class are totally diferent.
I can submit PR for this

@yii-bot
Copy link

yii-bot commented Apr 5, 2018

Issue moved to yiisoft/yii-api#8

@yii-bot yii-bot closed this as completed Apr 5, 2018
@cebe cebe removed this from the 2.1.0 milestone Apr 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants