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

fix: creates `drafts` property only at entities that are truly draft enabled

Open stockbal opened this issue 4 months ago • 0 comments

Uses a new approach taken from @sap/cds-compiler/lib/transform/draft.js#generateDraft to detect draft enabled entities.

In essence there are only 2 cases when a draft table will be generated for an entity and therefore the .drafts property of an entity will return a valid CSN entity reference during runtime.

  1. Entity A is exposed in a service and has the annotation @odata.draft.enabled

  2. Entity B is used as composition in Entity A or an Entity (C-Z) that is a composition of Entity A

    // model.cds
    entity A { b : Composition of B }
    entity B { c : Composition of C }
    entity C { d: Composition of D }
    entity D { }
    
    // service.cds
    serivce Service {
      @odata.draft.enabled
      entity P1 as projection on A;
    }
    
    • Service.P1 is draft enabled via annotation
    • Service.B, Service.C and Service.D are draft enabled via composition relationship
    • A, B, C, D remain as is and won't have access to a .drafts property

Further remarks

  • The refactoring uses the inferred csn model, because the xtended one does not include the autoexposed entities.
  • The draft state is no longer propagated top-down, as the model entities must not get the drafts properties. Instead, all draft enabled service-entities are collected in an array

Fixes #68

stockbal avatar Oct 05 '24 15:10 stockbal