cap-js / cds-typer

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

[BUG] String enum in composition of many potentially not supported #116

Open tsteckenborn opened 9 months ago

tsteckenborn commented 9 months ago

Is there an existing issue for this?

Nature of Your Project

TypeScript

Current Behavior

Given cds such as:

entity Something : cuid {
  taggedElements : Composition of many {
                                  hierarchyLevel : String enum {
                                    oneTaggedElementHierarchyLevel
                                  };
                              key elementGuid           : String;
                            };

The following type is generated:

export function _SomethingAspect<TBase extends new (...args: any[]) => object>(Base: TBase) {
  return class Something extends Base {
        taggedElements?: __.Composition.of.many<Array< {
  hierarchyLevel?: taggedElements_hierarchyLevel | null,
  elementGuid?: string,
}>>;
  };
}

Yet taggedElements_hierarchyLevel is not defined.

Expected Behavior

I would've expected taggedElements_hierarchyLevel to exist.

Steps To Reproduce

No response

Environment

- **OS**: Mac OS
- **Node**: v20.10.0
- **npm**: 10.2.3
- **cds-typer**: 0.12.0
- **cds**: 7.4.1

Repository Containing a Minimal Reproducible Example

No response

Anything else?

No response

github-actions[bot] commented 8 months ago

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.

tsteckenborn commented 7 months ago

Potentially any updates on this? A first step could be just typing them as the base type, e.g. string for now?

daogrady commented 7 months ago

Hi Tobias,

sorry for the long silence. I'm afraid there has been no work done on this issue so far.

I am actually wondering if this use case is valid. When I replace the inline enum with a named one, I am seeing an error:

type hierarchyLevel: String enum {
  oneTaggedElementHierarchyLevel
}

entity Something {
  key elementGuid: String;
  taggedElements: Composition of many hierarchyLevel  // Expecting an entity or aspect as composition targetCDS (compiler)(ref-invalid-target)
}

It reads like the same should apply for inline-defined enums. So it kind of reads like your example should already be rejected by the compiler. I will investigate on this.

Best Daniel

EDIT: I have misread your initial example, thanks for pointing that out. The following code works as expected:

type hierarchyLevel: String enum {
  oneTaggedElementHierarchyLevel
};

entity Something.taggedElements {
  hierarchyLevel: hierarchyLevel;
  key elementGuid: String;
}

entity Something {
  taggedElements: Composition of many Something.taggedElements
}

I can reproduce the issue. As users can use a named enum to work around this problem, I'd like to prioritise it fairly low. If you see an immediate solution and think this should be addressed asap, I would welcome any PR.