Root cause of the bug are missing undefined/null checks on intercepted calls without attachment payload.
The attachments plugin's "handle_attachments" injects handlers on every cds.ApplicationService, including our Service.
For example, in our Service, we are using service.send({ method: 'POST' }), which dispatches as a POST event. This triggers the plugin handlers, but this POST request has no attachment data.
The entitiy on which our service is based has an attachment composition, but the POST request is just an action call to another endpoint without affecting attachment data.
Due to missing optional chaning operators on multiple code parts, errors occur.
We have fixed the affected parts ourselves and would like to provide the solution by a pull request.
To Reproduce
one of several affected code parts:
- POST request like this.service.send({ method: 'POST', path:
${serviceName}.${actionName}, data: { noAttachmentsPayload } })
- this.before("CREATE", async (req) => { ... onPrepareAttachment(req) }) invoked
- async function onPrepareAttachment crashes at
!req.target?._attachments.isAttachmentsEntity &&
!req.target?._attachments.hasAttachmentsComposition
- adding chaining operator solves the issue
!req.target?._attachments ? .isAttachmentsEntity &&
!req.target?._attachments ? .hasAttachmentsComposition
Expected behavior
No error for requests without attachment payload.
Versions:
@cap-js/db-service: 2.9.0
@cap-js/hana: 2.7.0
@sap/cds: 9.8.3
@sap/cds-compiler: 6.7.3
@sap/cds-dk: 9.7.2
@sap/cds-dk (global): 9.1.0
Node.js: v22.22.0
Root cause of the bug are missing undefined/null checks on intercepted calls without attachment payload.
The attachments plugin's "handle_attachments" injects handlers on every cds.ApplicationService, including our Service.
For example, in our Service, we are using service.send({ method: 'POST' }), which dispatches as a POST event. This triggers the plugin handlers, but this POST request has no attachment data.
The entitiy on which our service is based has an attachment composition, but the POST request is just an action call to another endpoint without affecting attachment data.
Due to missing optional chaning operators on multiple code parts, errors occur.
We have fixed the affected parts ourselves and would like to provide the solution by a pull request.
To Reproduce
one of several affected code parts:
${serviceName}.${actionName}, data: { noAttachmentsPayload } })!req.target?._attachments.isAttachmentsEntity &&
!req.target?._attachments.hasAttachmentsComposition
!req.target?._attachments ? .isAttachmentsEntity &&
!req.target?._attachments ? .hasAttachmentsComposition
Expected behavior
No error for requests without attachment payload.
Versions:
@cap-js/db-service: 2.9.0
@cap-js/hana: 2.7.0
@sap/cds: 9.8.3
@sap/cds-compiler: 6.7.3
@sap/cds-dk: 9.7.2
@sap/cds-dk (global): 9.1.0
Node.js: v22.22.0