Can we have a ResourceCollection() under Resource()?


#1

.Net version 4.5.

I have an object called Process and under that I have collection of Activities

{
“processDocument”: [
{
“jsonapi”: {“version”: “1.1”},
“data”: {
“type”: “process-model”,
“id”: “510003”,
“attributes”: {
“process-id”: 510003,
“process-name”: “Process123”,
“enabled”: true,
“activities”: [
{
“processId”: 510003,
“activityId”: 510005,
“activityName”: “Activity 1”
},
{
“processId”: 510003,
“activityId”: 510054,
“activityName”: “Activity 2”
},
{
“processId”: 510003,
“activityId”: 510004,
“activityName”: “Activity 3”
}
]
}
}
}
]
}

Here’s what I’m trying to accomplish:

I want to be able to get the fully qualified links for each item in the activity collection. I’m able to output the above json without links by this code:

           var processdoc = documentContext
                .NewDocument(HttpContext.Current.Request.Url)
                    .SetJsonApiVersion(JsonApiVersion.Version11)
                    .Resource(process)
                    .ResourceEnd()
                .WriteDocument();

The “process” object supplied to the Resource() method above contains the Process properties and a collection (List) of activities. The JSON Api doesn’t seem to support accessing collection the ResourceCollection() under Resource()… I was expecting to do something like this:

           var processdoc = documentContext
                .NewDocument(HttpContext.Current.Request.Url)
                    .SetJsonApiVersion(JsonApiVersion.Version11)
                    .Resource(process)                            
                        .ResourceCollection(process.Activities)
                            .Links()
                                .AddSelfLink()
                            .LinksEnd()
                        .ResourceCollectionEnd()
                    .ResourceEnd()
                .WriteDocument();

Or is there some other way to accomplish this using JSON Api that I’m not aware of?

I was also hoping to convert the property name to camel case (processId, processName) instead of using hyphens (process-id, process-name) in the JSON output. Is that doable in JSON Api?

Appreciate any help on these.

Vin


#2

The quick answer is json:api resources can not be composed of other resources. Instead they have relationships to each other. See Clarification on Resources Composed of Other Resources in the json:api forum for details on this topic. A corollary to this is do not include “foreign keys” in the resources, instead favor relationships.

So for you specific case instead of having process being composed of many activities, process has a “to-many” relationship to activities where the hypermedia API server can server-side “include” the related activities if desired or if the client requests with a “include” query parameter.