JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources
Expose JSONAPI::ResourceSerializer#attribute_hash ?
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Ruby
- Star
- 2.3k
- Fork
- 546
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Mô tả
Thanks for all the work on this gem. I've been using it for a new project and I really enjoy it.
For a project I'm currently working on, I have a Song model that has_one :embed. I don't want to expose the Embed model as part of the REST API. I always want to include the attributes of the embed in the response.
The JSON API format document explicitly states that says that attributes "may contain any valid JSON value" and that "complex data structures involving JSON objects and arrays are allowed as attribute values."
I propose that models specified as attributes in a resource should be serialized simply, using #attribute_hash.
For example, this is what my JSON looks like if I simply add :embed as an attribute to my SongResource
"embed": {
"id": 1,
"identifier": "1101",
"site": "youtube",
"art_content_id": 1,
"art_content_type": "Song",
"created_at": "2015-09-04T19:55:41.596Z",
"updated_at": "2015-09-04T19:55:41.596Z"
}
If it was passed through the serializer, this is what I would get. (this is what I want)
"embed": {
"identifier": "1101",
"site": "youtube"
}
Right now, I can do this by manually with code like this in my SongResource
def embed
resource = JSONAPI::Resource.resource_for @model.embed.class.to_s.underscore
serializer = JSONAPI::ResourceSerializer.new resource
serializer.send :attribute_hash, resource.new(@model.embed)
end
If you would be willing to consider a feature like this, I would be happy to put together a pull request for it
Hướng dẫn đóng góp
Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu bằng việc đọc JSONAPI::ResourceSerializer và entry point private attribute_hash của nó, sau đó so sánh cách các thuộc tính và quan hệ của SongResource hiện đang được serialize. Xác nhận rằng một model được nhúng có thể được serialize dưới dạng các thuộc tính lồng nhau, với các trường identifier và site được yêu cầu, mà không expose Embed như một resource riêng biệt.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- ruby
- Lĩnh vực
- api, backend
- Loại issue
- Tính năng
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 42/100