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

Singular Entity Not Generated Correctly by cds-typer #425

Open
1 task done
ocolisanrares97 opened this issue Dec 6, 2024 · 1 comment
Open
1 task done

Singular Entity Not Generated Correctly by cds-typer #425

ocolisanrares97 opened this issue Dec 6, 2024 · 1 comment
Labels
bug Something isn't working new

Comments

@ocolisanrares97
Copy link

ocolisanrares97 commented Dec 6, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Nature of Your Project

TypeScript

Current Behavior

I am working on an SAP CAP application using TypeScript, and I’ve encountered an issue where cds-typer does not generate the singular form of an entity correctly in the index.ts and index.js files. This causes the singular entity reference to be undefined in the service handler.

Expected Behavior

The singular form of the entity (ParentEntity) should be properly generated and accessible in the service handler as this.cds.CustomParentEntity.

Impact:
Without a properly generated singular entity, I cannot reference it directly in the service handler, requiring workarounds such as manually using @singular and @plural on service level.

Steps To Reproduce

  1. Define an entity in the db folder with @singular and @plural annotations, including a composition.
  2. Create a service projection on the entity.
  3. Attempt to reference the singular entity in the service handler.

Scenario:

  1. Entity Definition in the db Folder:
    I have a projection entity defined with @singular and @plural annotations. The entity includes a composition that references another entity. Here's an example:
//CDS code
@plural  : 'CustomParentEntities'
@singular: 'CustomParentEntity'
entity ParentEntities as projection on BaseParent {
    *,
    relatedChild : Composition of one ChildEntities on 
                   relatedChild.parentID = $self.ID
};
  1. Service Definition:
    In my service file, I have another projection on this entity:
    entity ProjectedEntities as projection on ParentEntities;

  2. Service Handler:
    Inside the service handler, I attempt to reference the singular entity (CustomParentEntity) for an operation:

//TS Code
srv.on("DELETE", this.cds.CustomParentEntity, async (req, next) => {
   // logic
});

Problem

In this scenario, this.cds.CustomParentEntity is undefined. After inspecting the generated TypeScript and JavaScript files, I observed the following:

Generated index.ts:

//TS Code
export class CustomParentEntity extends _CustomParentEntity Aspect(__.Entity) {}
Object.defineProperty(CustomParentEntity , 'name', { value: 'Service.ParentEntities' });
Object.defineProperty(CustomParentEntity , 'is_singular', { value: true });

export class CustomParentEntitiesextends Array<CustomParentEntity > { $count?: number }
Object.defineProperty(CustomParentEntities, 'name', { value: 'Service.CustomParentEntities' });

Generated index.js:

//JS Code
// ParentEntities
module.exports.ParentEntity = { is_singular: true, __proto__: csn.ParentEntities };
module.exports.CustomParentEntities= csn.ParentEntities;
module.exports.ParentEntities = { is_singular: true, __proto__: csn.ContactCustomers }

The CustomParentEntity is not properly generated as a singular class.

Suggested Fix:

Ensure that the singular entity is correctly generated in both index.ts and index.js files, so it can be directly referenced in TypeScript and JavaScript code, like:

this.cds.CustomParentEntity; // Should not be undefined

Environment

@cap-js/asyncapi: 1.0.2
@cap-js/audit-logging: 0.8.1
@cap-js/cds-typer: 0.26.0
@cap-js/cds-types: 0.6.5
@cap-js/db-service: 1.14.0
@cap-js/hana: 1.3.0
@cap-js/openapi: 1.0.6
@cap-js/sqlite: 1.7.3
@sap-ecs/lucas: 0.1.0
@sap/cds: 8.3.1
@sap/cds-compiler: 5.3.2
@sap/cds-dk: 8.3.0
@sap/cds-fiori: 1.2.7
@sap/cds-foss: 5.0.1
@sap/cds-lsp: 8.3.2
@sap/cds-mtxs: 2.2.0
@sap/eslint-plugin-cds: 3.1.0
Node.js: v20.9.0

Repository Containing a Minimal Reproducible Example

No response

Anything else?

No response

@ocolisanrares97 ocolisanrares97 added bug Something isn't working new labels Dec 6, 2024
@daogrady
Copy link
Contributor

daogrady commented Dec 9, 2024

Hi @ocolisanrares97 ,

can you please extend your sample code to be a minimal reproducible example, i.e. include all missing entities, like BaseParent, etc. and clearly label where each file is supposed to be located? Thanks!

Best,
Daniel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working new
Projects
None yet
Development

No branches or pull requests

2 participants