cap-js / cds-typer

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

[QUESTION] Version 0.20.1 question about action typing. #223

Closed dragolea closed 5 months ago

dragolea commented 5 months ago

Question

Hi guys, Recently after updating to the latest CDS-Typer, I've seen that the action now has a different structure than before.

New version, the req contains a new property ID which points to the Entity, this means you will have to write

req.data.ID.ID //  to fetch the id, is this correct ? 
image

or

this means that that you will always have to say to all properties

req.data.ID.ID to fetch the ID
req.data.ID.author ... etc
image

Previously in the past version was like :

image
daogrady commented 5 months ago

Hi Daniel,

no, this does not look like it is intended. But it looks like it belongs in cds-types instead, no? Or did the structure of the action, generated by cds-typer, change to cause this new behaviour?

Best, Daniel

dragolea commented 5 months ago

Looks like the cds-typer caused this issue, version 0.19.0 has a totally different structure than 0.20.0.

In the example below the format field should not point to the Entity itself, but to the entity field which can be (number, string, undefined ...)

image
daogrady commented 5 months ago

Could you please attach a minimal sample model in which I can observe this behaviour?

dragolea commented 5 months ago

Sure, Below we have the CDS structure

  // Unbound action
  action   changeBookProperties(format : BookFormats:format, language : BookFormats:language) returns {
    language : String;
    format : BookFormats:format
  };

  // Unbound action
  action   submitOrder(book : Books:ID, quantity : Integer)                                   returns {
    stock : Integer
  };

  // Unbound function
  function submitOrderFunction(book : Books:ID, quantity : Integer)                           returns {
    stock : Integer
  };

Below we have a CDS Typer TS generated entity

export declare const changeBookProperties: { (format: BookFormat | null, language: BookFormat | null):  {
  language?: string | null,
  format?: __.DeepRequired<BookFormat>['format'] | null,
} | null | null, __parameters: {format: BookFormat | null, language: BookFormat | null}, __returns:  {
  language?: string | null,
  format?: __.DeepRequired<BookFormat>['format'] | null,
} | null | null, kind: 'action'};
export declare const submitOrder: { (book: Book | null, quantity: number | null):  {
  stock?: number | null,
} | null | null, __parameters: {book: Book | null, quantity: number | null}, __returns:  {
  stock?: number | null,
} | null | null, kind: 'action'};
export declare const submitOrderFunction: { (book: Book | null, quantity: number | null):  {
  stock?: number | null,
} | null | null, __parameters: {book: Book | null, quantity: number | null}, __returns:  {
  stock?: number | null,
} | null | null, kind: 'function'};
daogrady commented 5 months ago

Thanks for the model so far! I'm afraid it is not self-contained/ minimal, as it refers to BookFormats and Books, which are not included in the model, so I can not reproduce the issue on my end. Could you please extend the sample to be self-contained?

dragolea commented 5 months ago

Sure,

  1. Here you can find the structure of actions Service actions
  2. Here you can find the structure of the DB schema DB schema

Just a summary and a helping hand :

entity Books : managed {
  key ID                  : Integer;
      title               : localized String(111)  @mandatory;
      descr               : localized String(1111);
      stock               : Integer;
      price               : Decimal;
      currency            : Currency;
      image               : LargeBinary            @Core.MediaType: 'image/png';
      // Associations
      author              : Association to Authors @mandatory;
      genre               : Association to Genres;

      reviews             : Association to many Reviews
                              on reviews.book = $self;

      stats               : Association to one BookStats
                              on stats.book = $self;

      bookFormats         : Association to many BookFormats
                              on bookFormats.book = $self;

      bookRecomanddations : Association to many BookRecommendations
                              on bookRecomanddations.book = $self;

}
daogrady commented 5 months ago

Hi Daniel,

I have prepared a fix which I will release later this day: https://github.com/cap-js/cds-typer/pull/227 Feel free to try it out beforehand on your model.

Best, Daniel