{"action":"created","issue":{"url":"https://api.github.com/repos/phoenixframework/phoenix/issues/1526","repository_url":"https://api.github.com/repos/phoenixframework/phoenix","labels_url":"https://api.github.com/repos/phoenixframework/phoenix/issues/1526/labels{/name}","comments_url":"https://api.github.com/repos/phoenixframework/phoenix/issues/1526/comments","events_url":"https://api.github.com/repos/phoenixframework/phoenix/issues/1526/events","html_url":"https://github.com/phoenixframework/phoenix/issues/1526","id":131771996,"number":1526,"title":"Generated JSON views should use specific model names rather than \"data\"","user":{"login":"brado","id":453262,"avatar_url":"https://avatars.githubusercontent.com/u/453262?v=3","gravatar_id":"","url":"https://api.github.com/users/brado","html_url":"https://github.com/brado","followers_url":"https://api.github.com/users/brado/followers","following_url":"https://api.github.com/users/brado/following{/other_user}","gists_url":"https://api.github.com/users/brado/gists{/gist_id}","starred_url":"https://api.github.com/users/brado/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/brado/subscriptions","organizations_url":"https://api.github.com/users/brado/orgs","repos_url":"https://api.github.com/users/brado/repos","events_url":"https://api.github.com/users/brado/events{/privacy}","received_events_url":"https://api.github.com/users/brado/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2016-02-05T22:14:04Z","updated_at":"2016-02-06T00:41:27Z","closed_at":"2016-02-06T00:41:27Z","body":"When running the JSON generator, the generated view results in a parent JSON name of \"data\" rather than the specific model name. This isn't an error, but I believe it is an oversight and/or not the best alternative. Example, if you execute the following: \r\n```\r\nmix phoenix.gen.json API.V1.Car cars name:string description:string\r\n```\r\nit will generate a view which looks like this: \r\n```\r\ndefmodule MyProject.API.V1.CarView do\r\n  use MyProject.Web, :view\r\n\r\n  def render(\"index.json\", %{cars: cars}) do\r\n    %{data: render_many(cars, MyProject.API.V1.CarView, \"car.json\")}\r\n  end\r\n\r\n  def render(\"show.json\", %{car: car}) do\r\n    %{data: render_one(car, MyProject.API.V1.CarView, \"car.json\")}\r\n  end\r\n\r\n  def render(\"car.json\", %{car: car}) do\r\n    %{id: car.id,\r\n      name: car.name,\r\n      description: car.description}\r\n  end\r\nend\r\n```\r\nThis isn't the best option. While prefacing the specific JSON output with \"data\" is syntactically correct, the resulting JSON looks as follows: \r\n```\r\ndata : {\r\n...\r\n}\r\n```\r\nIt would be much better for both readability and downstream integration development / debugging if the JSON document indicated its specific contents, as such: \r\n```\r\ncar: {\r\n...\r\n}\r\n```\r\nSo for the generated view, it would be better if it were generated as follows: \r\n```\r\ndefmodule MyProject.API.V1.CarView do\r\n  use MyProject.Web, :view\r\n\r\n  def render(\"index.json\", %{cars: cars}) do\r\n    %{cars: render_many(cars, MyProject.API.V1.CarView, \"car.json\")}\r\n  end\r\n\r\n  def render(\"show.json\", %{car: car}) do\r\n    %{car: render_one(car, MyProject.API.V1.CarView, \"car.json\")}\r\n  end\r\n\r\n  def render(\"car.json\", %{car: car}) do\r\n    %{id: car.id,\r\n      name: car.name,\r\n      description: car.description}\r\n  end\r\nend\r\n```\r\nThe resulting JSON will then properly describe its contents in both plural (\"cars\") and singular (\"car\") scenarios. "},"comment":{"url":"https://api.github.com/repos/phoenixframework/phoenix/issues/comments/180636819","html_url":"https://github.com/phoenixframework/phoenix/issues/1526#issuecomment-180636819","issue_url":"https://api.github.com/repos/phoenixframework/phoenix/issues/1526","id":180636819,"user":{"login":"chrismccord","id":576796,"avatar_url":"https://avatars.githubusercontent.com/u/576796?v=3","gravatar_id":"","url":"https://api.github.com/users/chrismccord","html_url":"https://github.com/chrismccord","followers_url":"https://api.github.com/users/chrismccord/followers","following_url":"https://api.github.com/users/chrismccord/following{/other_user}","gists_url":"https://api.github.com/users/chrismccord/gists{/gist_id}","starred_url":"https://api.github.com/users/chrismccord/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/chrismccord/subscriptions","organizations_url":"https://api.github.com/users/chrismccord/orgs","repos_url":"https://api.github.com/users/chrismccord/repos","events_url":"https://api.github.com/users/chrismccord/events{/privacy}","received_events_url":"https://api.github.com/users/chrismccord/received_events","type":"User","site_admin":false},"created_at":"2016-02-06T00:41:26Z","updated_at":"2016-02-06T00:41:26Z","body":"We went with `data` because of JSON API http://jsonapi.org/ . Not that we generate a fully json api compliant payload, but it's designed to provide structure for you to build from. As this is not an issue, please take changes like this to the mailing list since we reserve the issue tracker for active feature development and bugs. Thanks!"}}