eosnewmedia / JSON-API-Common

Basic php implementation (shared structures for client and server) of the json api specification (jsonapi.org)
https://eosnewmedia.github.io/JSON-API-Common/
MIT License
14 stars 13 forks source link

Allow relative URLs #5

Closed neilmcgibbon closed 6 years ago

neilmcgibbon commented 6 years ago

The JSON API Spec does not explicitly define the structure of URL in a Link (either when the link value is a URL string or when the link value is an object with an href string property).

The schema defines a link as uri-reference (https://github.com/json-api/json-api/blob/gh-pages/schema#L180) which may be relative or absolute as opposed to uri which would be absolute only*.

The move from uri to uri-reference happened in this (merged) PR: https://github.com/json-api/json-api/pull/1169/files

This PR checks if the provided href is prefixed by / (indicating a relative path) and if so, prepends the valid "http://www.example.com" string to the path, which allows us to still use PHP's filter_var function with FILTER_VALIDATE_URL definition. Both absolute and relative URL's now pass in the test(s) and the relative URL is still validated by PHP internals.