While working with computations I came across a surprising behaviour of IDocumentRtService.
For example if there is this document and a corresponding model:
{
"id": "123",
"Root": {
"Foo": null
}
}
The model contains a computation for field Foo. Then using iDocumentRtService.compute(document, language) succeeds. But if I use the following document the computation fails:
{
"id": "123",
"Root": {
}
}
In the latter document the field is not present and the call to iDocumentRtService.compute(document, language) fails with:
Caused by: com.mgmtp.a12.md.rt.api.ComputationException: Field instance '/Root[1]/Foo[1]' not found!
at com.mgmtp.a12.md.internal.rt.ComputationResultImpl.createComputedValue(ComputationResultImpl.java:79)
at com.mgmtp.a12.md.internal.rt.ComputationResultImpl.<init>(ComputationResultImpl.java:57)
at com.mgmtp.a12.md.internal.rt.DocumentDynamicRtService.compute(DocumentDynamicRtService.java:96)
... 1 more
Is this intended behaviour? If the field is computed anyway shouldn’t it then be created automatically?
A workaround for this can be found here: How to add missing fields to a document?