Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
1 task done
stockbal opened this issue Oct 4, 2024 · 2 comments
Open
1 task done
Labels
bug Something isn't working keepalive Will not be closed by Stale bot new

Comments

@stockbal
Copy link
Contributor

stockbal commented Oct 4, 2024

Is there an existing issue for this?

  • 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 stockbal added bug Something isn't working new labels Oct 4, 2024
@stockbal
Copy link
Contributor Author

stockbal commented Oct 4, 2024

The fix in #346 currently works only by setting the option inlineDeclarations to flat. This would be another opportunity to revisit this CLI option, as both inline and named struct type elements result in erroneous generated types, when option structured is used.

Copy link

github-actions bot commented Nov 3, 2024

This issue has not been updated in a while. If it is still relevant, please comment on it to keep it open. The issue will be closed soon if it remains inactive.

@github-actions github-actions bot added the stale label Nov 3, 2024
@daogrady daogrady added keepalive Will not be closed by Stale bot and removed stale labels Nov 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working keepalive Will not be closed by Stale bot new
Projects
None yet
Development

No branches or pull requests

2 participants