Giter Site home page Giter Site logo

lode / jsonapi Goto Github PK

View Code? Open in Web Editor NEW
49.0 3.0 9.0 693 KB

Human-friendly library to implement JSON:API without needing to know the specification.

Home Page: https://packagist.org/packages/alsvanzelf/jsonapi

License: MIT License

PHP 100.00%
jsonapi json-api json api-server php

jsonapi's People

Contributors

jwissels avatar kat3su avatar lode avatar pierozi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

jsonapi's Issues

check key primary_links['self']['href']

I noticed following on line 281 in resource.php:

$base_url = (isset($this->primary_links['self']['href'])) ? $this->primary_links['self']['href'] : $this->primary_links['self'];

$this->primary_links['self']['href'] never seems to exist (at least not href), can't find where ['href'] should be set, but that put aside :)
$this->primary_links['self'] is a string, and ...['href']seems to substring that, resulting in "/", and thus true, and thus using "/" as $base_url.

When I add an additional check to see if primary_links['self'] is an array, it seems to work as intended (line 281+303):

// $base_url = (isset($this->primary_links['self']['href'])) ? $this->primary_links['self']['href'] : $this->primary_links['self'];
$base_url = (is_array($this->primary_links['self']) && isset($this->primary_links['self']['href']))
	? $this->primary_links['self']['href']
	: $this->primary_links['self'];

Allow include in included and data resourses with same id

Say, I have resource structure like this:

{
    "data": [
        {
            "type": "news",
            "id": "item1",
            "attributes": {
                "title": "item1"
            },
            "relationships": {
                "also": {
                    "data": [{
                            "type": "news",
                            "id": "item2"
                        }]
                }
            }
        },
        {
            "type": "news",
            "id": "item2",
            "attributes": {
                "title": "item2"
            },
            "relationships": {
                "also": {
                    "data": [{
                            "type": "news",
                            "id": "item3"
                        }]
                }
            }
        }
    ]
}

And I want to get resources like this:

{
    "data": [
        {
            "type": "news",
            "id": "item1",
            "attributes": {
                "title": "item1"
            },
            "relationships": {
                "also": {
                    "data": [{
                            "type": "news",
                            "id": "item2"
                        }]
                }
            }
        },
        {
            "type": "news",
            "id": "item2",
            "attributes": {
                "title": "item2"
            },
            "relationships": {
                "also": {
                    "data": [{
                            "type": "news",
                            "id": "item3"
                        }]
                }
            }
        }
    ],
    "included": [
        {
            "type": "other_companies",
            "id": "item2",
            "attributes": {
                "title": "item2",
                "link": "link",
                "code": "item2",
                ...
            }
        }
    ]
}

For now there will be triggered error "can not have multiple resources with the same identification".

Can we add individual id validation for included?
Just make another claimUsedResourceIdentifier for included.
Or add option for validator.

Problems using ResourceDocument::fromObject

Hi, we use your implementation quite a lot in our projets, and until now, never had any problems (we appreciate your work tremendously).
But now we have problems with answering a PUT request.
using your code leads to a structure were the attributes of an ResourceObject more or less look like this

"data": {
"type": "Event",
"id": "196",
"attributes":{
"type": "Event",
"id": "196",
"attributes": {
"ClientID": 1
}
}
}
which leads to an exception (type already used)

A solutions which works for us is simply:
rewriting the function in ResourceDocument in this way:

public static function fromObject($attributes, $type=null, $id=null, array $options=[]) {
$resourceDocument = new self();
$resourceDocument->setPrimaryResource($attributes->getResource(), $options);
return $resourceDocument;
}
the document includes now all links, relationships, and so on, from the ResourceObject

failed to generate json: Recursion detected

When i implement exception in controller, like examples:

$exception = new \Exception($message='user not found', $code=404);
$document = ErrorsDocument::fromException($exception);
$document->sendResponse();

I've got the error: failed to generate json: Recursion detected

I'm trying to use this on Lumen. I'm doing something wrong?

Import a database array as a collection response

It would be nice to do something like this:

$results = 'mysqli_query() + fetch_assoc() while-loop';
$jsonapi = new jsonapi\collection($type='users');
$jsonapi->import_from_array($results);

And maybe a way to split keys as relationships. Like import_from_array($array, $split_relation_names) or $array = collection::split_relations($array, $relation_names).

JSONP

I experience an unexpected result when trying use the JSONP option:
I used jQuery for this test:
$.getJSON('https://my.otherdomain.com/v1/myresource/123/?callback=?',response=>console.log(response))
resulting in this request:

https://my.otherdomain.com/v1/myresource/123/?callback=jQuery110209372736821749224_1518011758243&_=1518011758244
Accept:*/*

My API responses JSONP requests with:
$jsonapi->send_response(jsonapi\response::CONTENT_TYPE_JSONP, null, null, $this->getJSONPCallback());

What happens is that plain JSON gets returned, meaning not being wrapped the callback function.

I've debugged this, and noticed JSPN gets 'called off' in this if-block, resetting $content_type and thus not being treated as JSONP afterall, due to strpos($_SERVER['HTTP_ACCEPT'], '/json') check:
response.php line 165

	if (base::$debug || strpos($_SERVER['HTTP_ACCEPT'], '/json') == false) {
		$content_type = self::CONTENT_TYPE_DEBUG;
	}

Problem with Content-Type: application/vnd.api+json

When I want to response a code 204 No content the Content-Type is text/html; charset=UTF-8 (according to postman).

Content type should be always application/vnd.api+json even if there is no data, right?. I don't know if this is an issue, because I'm newbie with jsonapi.

My code to send response is: $document->sendResponse(http_response_code(204));

Sync links with the latest spec

Right now links are represented using the old syntax:

{
    "self": "http...",
    "foo": "bar"
}

The latest spec says it should be like this:

{
    "href": "http...",
    "meta": {
        "foo": "bar"
    }
}

Links can anyway be added as strings, in both spec versions and this is supported.

add_relation in resource.php overwrite (array relations)

While upgrading from an earlier version, I noticed a change in resource.php, function add_relation

On line 270 it handles array input like this

	if (is_array($relation)) {
		$this->primary_relationships[$key] = $relation;
	}

But this is overwritten on line 312 (outside the if closure), with the variable $relation_data that is not defined in that case:

$this->primary_relationships[$key] = array(
		'data'  => $relation_data,
	);

This breaks implementations who pass the relation as array.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.