From 56dbe640fe8ae587f45b50b7736b1628adb007a4 Mon Sep 17 00:00:00 2001 From: Siarhei Murkou Date: Wed, 16 Oct 2024 14:48:49 +0300 Subject: [PATCH] Fix array-like params in CDS functions/actions (#363) `array of` needs to be supported as well: ```cds function check( param_2: Array of String not null ) returns ... ``` Explaining comment is [here](https://github.com/cap-js/cds-typer/pull/332#discussion_r1799647831). --------- Co-authored-by: Christian Georgi --- CHANGELOG.md | 1 + lib/resolution/resolver.js | 2 +- test/unit/actions.test.js | 2 +- test/unit/files/actions/model.cds | 4 ++-- test/unit/files/actions/model.ts | 2 +- 5 files changed, 6 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a17eb11..27ae058 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/). ### Fixed - Entity elements of named structured types are flattened when using the option `--inlineDeclarations flat` +- Properly support mandatory (`not null`) action parameters with `array of` types ## Version 0.27.0 - 2024-10-02 ### Changed diff --git a/lib/resolution/resolver.js b/lib/resolution/resolver.js index c7c794e..bfba619 100644 --- a/lib/resolution/resolver.js +++ b/lib/resolution/resolver.js @@ -65,7 +65,7 @@ class Resolver { * @returns {boolean} whether the type is configured to be optional */ isOptional(type) { - return !type.notNull + return type.items ? !type.items.notNull : !type.notNull } /** diff --git a/test/unit/actions.test.js b/test/unit/actions.test.js index e26a608..7381e80 100644 --- a/test/unit/actions.test.js +++ b/test/unit/actions.test.js @@ -178,7 +178,7 @@ describe('Actions', () => { test ('Optional Action Params', async () => { checkFunction(astwBound.tree.find(fn => fn.name === 'aMandatoryParam'), { - parameterCheck: ({members: [p1, p2, p3]}) => !check.isOptional(p1) && check.isOptional(p2) && !check.isOptional(p3), + parameterCheck: ({members: [p1, p2, p3]}) => !check.isOptional(p1) && !check.isOptional(p2) && check.isOptional(p3), }) }) diff --git a/test/unit/files/actions/model.cds b/test/unit/files/actions/model.cds index 3201cf9..cebc944 100644 --- a/test/unit/files/actions/model.cds +++ b/test/unit/files/actions/model.cds @@ -34,9 +34,9 @@ service S { action aManyParamSingleReturn(val: array of E) returns E; action aMandatoryParam( - val: E not null, + val1: E not null, + val2: array of E not null, opt: E, - val2: E not null ) returns E; /** the action */ diff --git a/test/unit/files/actions/model.ts b/test/unit/files/actions/model.ts index 8292fe5..f6938d0 100644 --- a/test/unit/files/actions/model.ts +++ b/test/unit/files/actions/model.ts @@ -49,7 +49,7 @@ export class S extends cds.ApplicationService { async init(){ this.on(aManyParamSingleReturn, req => { const {val} = req.data; return {e1:val[0].e1} satisfies E }) this.on(aMandatoryParam, req => { - false satisfies IsKeyOptional + false satisfies IsKeyOptional false satisfies IsKeyOptional true satisfies IsKeyOptional })