Skip to content

[BUG] @mandatory annotation on function parameters causes "Unexpected mandatory after optional parameter" error #147

@JoJ123

Description

@JoJ123

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

When defining a CDS function with multiple parameters, all marked as @mandatory, the OpenAPI compiler fails with:

[ERROR] Unexpected mandatory after optional parameter (in function:"<ServiceName>.<functionName>"/param:"<secondParam>")

The compiler appears to not recognize @mandatory annotations on function parameters at all — every parameter is treated internally as optional, which then triggers the OData V4 rule that mandatory parameters must come before optional ones as soon as a second mandatory parameter is encountered.

The same annotation pattern works correctly on action definitions.

Expected Behavior

The function definition compiles successfully and the generated OpenAPI document marks both parameters as required: true, identical to how the action variant is handled.

Steps To Reproduce

Minimal CDS service definition:

service ReproService {

  // FAILS with `odata-parameter-order`
  // Both params are tagged @mandatory, so a developer reasonably expects this to be valid.
  // The first param is an enum type (no `not null`) -> compiler treats it as optional.
  // The second param references a key field (`name` is `String not null`) -> compiler treats it as mandatory.
  // -> "Unexpected mandatory after optional parameter".
  function getMapping(
    @mandatory
    @Core.Description: 'First parameter'
    paramA: TypeA,

    @mandatory
    @Core.Description: 'Second parameter'
    paramB: TypeB:name
  ) returns String;

  // WORKS — identical signature, only `function` -> `action`
  action getMappingAsAction(
    @mandatory
    @Core.Description: 'First parameter'
    paramA: String,

    @mandatory
    @Core.Description: 'Second parameter'
    paramB: String
  ) returns String;
}

define type TypeA: String enum {
  A; B; C;
}

entity TypeB{
  key id : UUID;
  name   : String not null;
}

Environment

- `@sap/cds`: 9.3.x
- `@cap-js/openapi`: latest
- Node.js: 24.x
- OS: Windows 11

Repository Containing a Minimal Reproducible Example

No response

Anything else?

No response

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions