laminas-api-tools / api-tools-hal

Laminas Module providing Hypermedia Application Language assets and rendering
https://api-tools.getlaminas.org/documentation
BSD 3-Clause "New" or "Revised" License
6 stars 12 forks source link

Api problem message as json #15

Open weierophinney opened 4 years ago

weierophinney commented 4 years ago

When I make request on non existing resource page /users?page=1234567 I receive:

{
  "type": "http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html",
  "title": "Conflict",
  "status": 409,
  "detail": "Invalid page provided"
}

As you see it a api problem. However in Content-Type you can find application/json instead of application/problem+json


Originally posted by @snapshotpl at https://github.com/zfcampus/zf-hal/issues/121

weierophinney commented 4 years ago

I can confirm this...


Originally posted by @Wilt at https://github.com/zfcampus/zf-hal/issues/121#issuecomment-148744285

weierophinney commented 4 years ago

A pull request for this issue can be found here: https://github.com/zfcampus/zf-hal/pull/127


Originally posted by @Wilt at https://github.com/zfcampus/zf-hal/issues/121#issuecomment-148749857

weierophinney commented 4 years ago

Maybe good idea to add a test?


Originally posted by @Wilt at https://github.com/zfcampus/zf-hal/issues/121#issuecomment-148750023

weierophinney commented 4 years ago

Another solution would be to include a

    /**
     * Does the payload represent a ApiProblem?
     *
     * @return bool
     */
    public function isApiProblem()
    {
        $payload = $this->getPayload();
        return ($payload instanceof ApiProblem);
    }

And use this method inside injectResponse method:

    if ($model instanceof HalJsonModel){
        if($model->isApiProblem){
            $contentType = 'application/problem+json'
        }elseif($model->isCollection() || $model->isEntity()){
            $contentType = 'application/hal+json';
        }
    }

Originally posted by @Wilt at https://github.com/zfcampus/zf-hal/issues/121#issuecomment-148750614