cds-typer icon indicating copy to clipboard operation
cds-typer copied to clipboard

[BUG] Entity element with named struct type not correctly reflected in generated types

Open stockbal opened this issue 4 months ago • 1 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Nature of Your Project

TypeScript

Current Behavior

Let's assume the following sample model.

// schema.cds

type Author {
  firstName : String;
  lastName : String;
}

entity Books : cuid, managed {
  title : String;
  author : Author;
}

// index.ts

export function _AuthorAspect<TBase extends new (...args: any[]) => object>(Base: TBase) {
  return class Author extends Base {
    firstName?: string | null
    lastName?: string | null
  };
}
export class Author extends _AuthorAspect(__.Entity) {}

export function _BookAspect<TBase extends new (...args: any[]) => object>(Base: TBase) {
  return class Book extends _._managedAspect(_._cuidAspect(Base)) {
    declare title?: string | null
    declare author?: Author | null
  };
}

If we look at the generated Book type, we expect to be able to access the author information with the name author. Unfortunately this is not possible as the CDS runtime is flattening the author element down to

  • author_firstName
  • author_lastName

Therefore the generated types for element author are not usable.

Expected Behavior

The generated types correctly reflect the CDS model during runtime

export function _BookAspect<TBase extends new (...args: any[]) => object>(Base: TBase) {
  return class Book extends _._managedAspect(_._cuidAspect(Base)) {
    declare title?: string | null
    declare author_firstName?: string | null
    declare author_lastName?: string | null
  };
}

Steps To Reproduce

No response

Environment

- **OS**: Win 11, WSL 2, Ubuntu 22.04.4
- **Node**: 20.16.0
- **npm**: 10.8.2
- **cds-typer**: 0.27.0
- **cds**: 8.2.3

Repository Containing a Minimal Reproducible Example

https://github.com/stockbal/cap-samples/tree/typer-playground?tab=readme-ov-file#named-type-definitions-as-elements-not-reflected-correctly-in-class-properties

Anything else?

I provided a fix with PR #346.

stockbal avatar Oct 04 '24 16:10 stockbal