Skip to content

[Version: 3.11.0 ] Runtime Errors due to missing null/undefined checks #419

Description

@Andreas-Meindl-msggroup

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:

  1. POST request like this.service.send({ method: 'POST', path: ${serviceName}.${actionName}, data: { noAttachmentsPayload } })
  2. this.before("CREATE", async (req) => { ... onPrepareAttachment(req) }) invoked
  3. async function onPrepareAttachment crashes at
    !req.target?._attachments.isAttachmentsEntity &&
    !req.target?._attachments.hasAttachmentsComposition
  4. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions