[ADP-375] Using 'expand' query parameter
Overview
In some cases, instead of providing the full content of the related resource, we only include an relatedResourceId field to inform the client of the related resource's ID. It is necessary to have a mechanism to expand this ID into a complete resource object.
Guidance
- It is recommended to use
expandto expand the related resourceId field into a complete resource object.WARNING
Naming the relatedResource field can be challenging. Using
resource-typedoes not inherently convey that it represents theresource-idby default. Conversely, usingresource-idmight make it difficult to expand into a full resource object. Here, we acknowledge thatresourcecould also refer to an id. Alternatively, you MAY establish your own convention, such as:- Convention 1:
resource: {resource-id:$ID} by defaultresource: { FULL_CONTENT } by expand
- Convention 2:
resourceId:$IDby default- Remove above and provide
resource: { FULL_CONTENT } by expand
Either way, it is essential to document this convention clearly.
- Convention 1:
- It is optional to use
expandto include an additional field in the response that is not part of the default response schema.- In contrast to embed,
expandis designed to encompass not only complete resources but also non-resource properties.
- In contrast to embed,
Example
Define the
expandParameter:- The API should support an
expandquery parameter that accepts a comma-separated list of resource relationships to expand.
Example:
GET /orders/{orderId}?expand=customer,items- The API should support an
Return Expanded Resource Objects:
- When the
expandparameter is not used, the API would return the relative resourceId by default.
Example Response:
json{ "orderId": "12345", "customer": "67890", }- When the
expandparameter is used, the API should return the full resource objects for the specified relationships in the response.
Example Response:
json{ "orderId": "12345", "customer": { "customerId": "67890", "name": "John Doe", "email": "john.doe@example.com" }, "items": [ { "orderItemId": "1", "productId": "987", "productName": "Widget", "quantity": 2, "price": 25.00 }, { "orderItemId": "2", "productId": "654", "productName": "Gadget", "quantity": 1, "price": 50.00 } ] }- When the
Document the Usage:
- Clearly document the
expandparameter in the API documentation, including examples of how to use it and the expected response format.
- Clearly document the