Skip to content

Commit

Permalink
Fix array-like params in CDS functions/actions (#363)
Browse files Browse the repository at this point in the history
`array of` needs to be supported as well:

```cds
function check(
    param_2: Array of String not null
) returns ...
```

Explaining comment is
[here](#332 (comment)).

---------

Co-authored-by: Christian Georgi <[email protected]>
  • Loading branch information
siarhei-murkou and chgeo authored Oct 16, 2024
1 parent bfed040 commit 56dbe64
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/resolution/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/**
Expand Down
2 changes: 1 addition & 1 deletion test/unit/actions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
})
})

Expand Down
4 changes: 2 additions & 2 deletions test/unit/files/actions/model.cds
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
2 changes: 1 addition & 1 deletion test/unit/files/actions/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof req.data, 'val'>
false satisfies IsKeyOptional<typeof req.data, 'val1'>
false satisfies IsKeyOptional<typeof req.data, 'val2'>
true satisfies IsKeyOptional<typeof req.data, 'opt'>
})
Expand Down

0 comments on commit 56dbe64

Please sign in to comment.