{"action":"created","issue":{"url":"https://api.github.com/repos/stoplightio/prism/issues/238","repository_url":"https://api.github.com/repos/stoplightio/prism","labels_url":"https://api.github.com/repos/stoplightio/prism/issues/238/labels{/name}","comments_url":"https://api.github.com/repos/stoplightio/prism/issues/238/comments","events_url":"https://api.github.com/repos/stoplightio/prism/issues/238/events","html_url":"https://github.com/stoplightio/prism/issues/238","id":430560836,"node_id":"MDU6SXNzdWU0MzA1NjA4MzY=","number":238,"title":"Rewrite URLs for improved HATEOAS ","user":{"login":"philsturgeon","id":67381,"node_id":"MDQ6VXNlcjY3Mzgx","avatar_url":"https://avatars1.githubusercontent.com/u/67381?v=4","gravatar_id":"","url":"https://api.github.com/users/philsturgeon","html_url":"https://github.com/philsturgeon","followers_url":"https://api.github.com/users/philsturgeon/followers","following_url":"https://api.github.com/users/philsturgeon/following{/other_user}","gists_url":"https://api.github.com/users/philsturgeon/gists{/gist_id}","starred_url":"https://api.github.com/users/philsturgeon/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/philsturgeon/subscriptions","organizations_url":"https://api.github.com/users/philsturgeon/orgs","repos_url":"https://api.github.com/users/philsturgeon/repos","events_url":"https://api.github.com/users/philsturgeon/events{/privacy}","received_events_url":"https://api.github.com/users/philsturgeon/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":3,"created_at":"2019-04-08T17:06:54Z","updated_at":"2019-04-08T17:43:45Z","closed_at":null,"author_association":"NONE","body":"One request came in for Prism v2 and help with [replacing hosts in JSON:API links](https://community.stoplight.io/t/mocking-replace-url-in-responses-hateoas/567), and I was wondering if we could solve that in Prism v3.\r\n\r\nSo, taking their link-in-the-body example:\r\n\r\n![image](https://user-images.githubusercontent.com/67381/55742467-0c221100-5a30-11e9-94a5-bd77314ae453.png)\r\n\r\nIt is a little unfortunate they have had to pollute their spec with a variable, which will show up and look funny in other tools. Instead, what if they hardcode all links to match the content of a valid server. \r\n\r\nSo, in their spec, define the servers array:\r\n\r\n```\r\nservers: \r\n  - url: https://api.foo.com\r\n    description: Production server\r\n\r\n  - url: https://staging-api.foo.com\r\n    description: Staging server\r\n```\r\n\r\nContinuging with their example: \r\n\r\n```js\r\n\"links\": {\r\n  \"self\": \"https://api.foo.com/api/shared_file_categories\"\r\n}\r\n```\r\n\r\nThis is a nice default because it includes the production server, and it is only a problem when Prism returns that link. When clients follow it, they end up on production, so boo, let's turn that into a local URL.\r\n\r\nIf we add a CLI option `--rewrite-urls`, it could be implemented as a Fastly middleware which looks through the JSON/XML/HTML content for a URL which matches a known servers.url + a valid path as defined in the spec, it rewrites the server URL portion (everything before the path) of that link to whatever prism is currently running under. \r\n\r\nIf Prism was running as usual:\r\n\r\n```\r\n$ prism mock links.yaml --rewrite-urls\r\nhttp://127.0.0.1:4010\r\n```\r\n\r\n...and the calls were as usual:\r\n\r\n```\r\nhttp GET http://127.0.0.1:4010\r\n```\r\n\r\nThe links would be rewritten to:\r\n\r\n```js\r\n\"links\": {\r\n  \"self\": \"http://127.0.0.1:4010/api/shared_file_categories\"\r\n}\r\n```\r\n\r\nIf Prism was called in conjunction with server validation (missing from Prism v3 currently but implemented in v2):\r\n\r\n```\r\nhttp GET http://127.0.0.1:4010?__server=https://staging-api.foo.com\r\n```\r\n\r\nthen we would see links rewritten like this:\r\n\r\n```js\r\n\"links\": {\r\n  \"self\": \"https://staging-api.foo.com/api/shared_file_categories\"\r\n}\r\n```\r\n\r\nWhat do you think?\r\n\r\nI started off considering this somehow related to [OpenAPI \"Links\"](https://swagger.io/docs/specification/links/) but I think it can be done without going anywhere near them, as it is links in the body we are struggling with currently. \r\n\r\n"},"comment":{"url":"https://api.github.com/repos/stoplightio/prism/issues/comments/480932782","html_url":"https://github.com/stoplightio/prism/issues/238#issuecomment-480932782","issue_url":"https://api.github.com/repos/stoplightio/prism/issues/238","id":480932782,"node_id":"MDEyOklzc3VlQ29tbWVudDQ4MDkzMjc4Mg==","user":{"login":"philsturgeon","id":67381,"node_id":"MDQ6VXNlcjY3Mzgx","avatar_url":"https://avatars1.githubusercontent.com/u/67381?v=4","gravatar_id":"","url":"https://api.github.com/users/philsturgeon","html_url":"https://github.com/philsturgeon","followers_url":"https://api.github.com/users/philsturgeon/followers","following_url":"https://api.github.com/users/philsturgeon/following{/other_user}","gists_url":"https://api.github.com/users/philsturgeon/gists{/gist_id}","starred_url":"https://api.github.com/users/philsturgeon/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/philsturgeon/subscriptions","organizations_url":"https://api.github.com/users/philsturgeon/orgs","repos_url":"https://api.github.com/users/philsturgeon/repos","events_url":"https://api.github.com/users/philsturgeon/events{/privacy}","received_events_url":"https://api.github.com/users/philsturgeon/received_events","type":"User","site_admin":false},"created_at":"2019-04-08T17:43:45Z","updated_at":"2019-04-08T17:43:45Z","author_association":"NONE","body":"I think we can wrap up official plugins with a bit of a nicer API than that, right? Save a lot of faff? \r\n\r\nTo me, common functionality can be in the core, and as things get more outlandish they can go elsewhere, but this is a pretty common thing. Building a REST API is pretty much our whole thing, and supporting links is necessary for that."}}
{"actor":{"display_login":"philsturgeon"}}