Grouping embedded resources in HAL

50 views
Skip to first unread message

Anthony Pane

unread,
Mar 24, 2017, 1:56:07 PM3/24/17
to API Craft

Is it ok for embedded to be used in sub properties?  For example, this resource is for a work time card for a week.  Rather than just show all shifts as a single embedded list on the time card, group them by day and provide some summary data for each day. 

 

{
   
"_links": {
     
"self": {
         
"href": "/timecard/2"
     
}
   
},
   
"shiftsSummary": {
     
"numberShiftsWorked": 9,
     
"hoursWorked": 45.5,
     
"hoursPaid": 48.5,
     
"numberOfMissingPunches": 0
   
},
   
"numberOfEdits": 14,
   
"startDate": "2016-07-26T00:00",
   
"endDate": "2016-08-01T00:00",
   
"days": [
     
{
         
"date": "2016-07-26",
         
"totalHoursWorked": 8.0,
         
"_embedded": {
           
"shifts": [
               
{
                 
"date": "2016-07-26T00:00",
                 
"startDateTime": "2016-07-26T09:00",
                 
"endDateTime": "2016-07-26T10:00",
                 
"paidHours": 6.0,
                 
"_links": {
                     
"self": {
                       
"href": "/shift/123"
                     
}
                 
}
               
}
           
]
         
}
     
},
     
{
         
"date": "2016-07-27",
         
"totalHoursWorked": 8.0,
         
"_embedded": {
           
"shifts": [
               
{
                 
"date": "2016-07-27T00:00",
                 
"startDateTime": "2016-07-27T09:00",
                 
"endDateTime": "2016-07-27T10:00",
                 
"paidHours": 4.0,
                 
"_links": {
                     
"self": {
                       
"href": "/shift/124"
                     
}
                 
}
               
},
               
{
                 
"date": "2016-07-27T00:00",
                 
"startDateTime": "2016-07-27T16:00",
                 
"endDateTime": "2016-07-27T20:00",
                 
"paidHours": 4.0,
                 
"_links": {
                     
"self": {
                       
"href": "/shift/125"
                     
}
                 
}
               
}
           
]
         
}
     
}
   
]
}



Doug Orleans

unread,
Apr 1, 2017, 2:43:35 PM4/1/17
to api-...@googlegroups.com
I don't think you can use embedded in sub-properties; the "days" objects in your example are not HAL resources, although I suppose you could declare them to be so with a JSON Schema. The point is, clients wouldn't know that the sub-objects are resources and not just plain objects, unless you told them somehow.

You could just make "days" be an embedded array, though, no?

{
  "_links": {
    "self": {
      "href": "/timecard/2"
    }
  },
  "_embedded": {
    "days": [
      {
         "date": "2016-07-26",
         
"totalHoursWorked": 8.0,
         
"_embedded"
: {
            
"shifts": [ ... ]
         }
       },
       { ... }
     ]
  }
}

The embedded objects are not required to have self links, though it would be nice to provide them if that's feasible, i.e. to promote "day" to be a first-class resource. (Perhaps as "/timecard/2/day/1", etc).

--Doug


--
You received this message because you are subscribed to the Google Groups "API Craft" group.
To unsubscribe from this group and stop receiving emails from it, send an email to api-craft+unsubscribe@googlegroups.com.
Visit this group at https://groups.google.com/group/api-craft.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages