cap-js / cds-typer

CDS type generator for JavaScript
Apache License 2.0
27 stars 8 forks source link

[BUG] ID is generated nullable if referenced by association to one - in specific situations #261

Closed rasmk closed 3 months ago

rasmk commented 3 months ago

Is there an existing issue for this?

Nature of Your Project

TypeScript

Current Behavior

We have two files:

test1.cds

namespace test;

aspect ParentAspect {
  key ID : UUID not null;
}

test2.cds

namespace test;

using {test.ParentAspect as ParentAspect} from './test1.cds';

entity E1 : ParentAspect {}

entity E2 : ParentAspect {
  serviceCall : Association to one E1;
}

Running npx @cap-js/cds-typer db/test2.cds --outputDirectory @cds-models generates type for ParentAspect with a nullable ID.

export function _ParentAspectAspect<TBase extends new (...args: any[]) => object>(Base: TBase) {
  return class ParentAspect extends Base {
        ID?: string | null;
      static readonly actions: Record<never, never>
  };
}

Expected Behavior

ID in the ParentAspect should never be null as it is a key and is marked as not null in the CDS definition.

Steps To Reproduce

Create two files: test1.cds and test2.cds, as decribed in current behavior. Run npx @cap-js/cds-typer db/test2.cds --outputDirectory @cds-models.

Environment

- **OS**: macOS 14.5
- **Node**: v18.20.1
- **npm**: 10.5.0
- **cds-typer**: 0.21.2
- **cds**: 7.8.0

Repository Containing a Minimal Reproducible Example

No response

Anything else?

The behavior is correct, if everything is defined in a single cds file. Then the generated ID is non-nullable.

Provided the files test1.cds and test2.cds, as described above, the commands:

daogrady commented 3 months ago

Possibly duplicate of https://github.com/cap-js/cds-typer/issues/219 ?

rasmk commented 3 months ago

It looks like a duplicate and the fix proposed in #213 seems to help. Thanks @daogrady

daogrady commented 3 months ago

Hi Rafal,

that's good news then! I will make sure to merge the contributor's PR and send out a release today.

Best, Daniel