Formating#
Response are formated using the Writer, itself called in the getDetails/getList
methods of RouteController. That means auto-reloaded entities are also
formated
The writer process each response item/items and call every Decorator functions that
are registered.
These are the defaults decorator functions that will be used when enabled in
WriterOptions :
| Function | Description | WriterOptions's key | Default enabled |
|---|---|---|---|
| setComputedPropsOnItem | Add computed properties to this item | shouldSetComputedPropsOnItem | true |
| setSubresourcesIriOnItem | Set subresources IRI | shouldSetSubresourcesIriOnItem | true |
| flattenItem | Flatten item with only id if needed | shouldEntityWithOnlyIdBeFlattened | true |
The Writer also performs the following tasks :
- Sort item's property keys (if
shouldSortItemKeysis true), can pass a customsortComparatorFn, defaults tosort() - Remove any object that is not another Entity (object that
implements
GenericEntity) or is not a Date - Can opt-in/out IRI's usage with the
useIrisoption
Examples#
Below examples use these entities
1. Remove any object that is not another Entity#
Let's say you have a User entity such as :
- The
repositorykey will not be returned in response since it does not implement theGenericEntityinterface. - The
tableNamewill be returned since it's just a string
2. Flatten item with only id if needed#
Enabled by default (see defaultEntityRouteOptions). You can opt-out of
this behavior by setting the shouldEntityWithOnlyIdBeFlattened key of
EntityRouteOptions to false.
Let's say you have a Role and a User entity such as :
The User.id = 1 has a relation to the Role.id = 321.
Since no properties except id is exposed on the user.details route scope :
- The
rolerelation will be returned as an IRI. - If you opted-out of IRIs, then the
idwill be returned as is.
3. Set subresources IRI#
Enabled by default (see defaultEntityRouteOptions). You can opt-out of
this behavior by setting the defaultWriterOptions.shouldSetSubresourcesIriOnItem key of
EntityRouteOptions to false.
- The
articlesproperty will be set to its IRI/api/user/1/articles(if not exposed through@Groups)
4. Sort item's property keys#
Enabled by default (see defaultEntityRouteOptions). You can opt-out of
this behavior by setting the defaultWriterOptions.shouldSortItemKeys key of
EntityRouteOptions to false.
- All responses keys will be sorted alphabetically, recursively through all nested entities as well