Skip to content

Commit

Permalink
fix: rename entity property to _entity (#2577)
Browse files Browse the repository at this point in the history
  • Loading branch information
deekshas8 authored Jun 20, 2022
1 parent f0a1104 commit 0675ee3
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 7 deletions.
6 changes: 6 additions & 0 deletions .changeset/soft-beers-run.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@sap-cloud-sdk/generator': patch
'@sap-cloud-sdk/odata-common': patch
---

[Fixed Issue] Allow OData service to contain an entity name 'entity'.
1 change: 1 addition & 0 deletions packages/generator/src/reserved-words.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const reservedServiceKeywords = [
*/
export const reservedVdmKeywords = [
'builder',
'_entity',
'entityBuilder',
'requestBuilder'
] as const;
Expand Down
3 changes: 3 additions & 0 deletions packages/generator/src/service-name-formatter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ describe('name-formatter', () => {
expect(
formatter.originalToInstancePropertyName('A_SomeEntity', 'someProperty')
).toBe('someProperty_1');
expect(
formatter.originalToInstancePropertyName('A_SomeEntity', '_entity')
).toBe('entity');
expect(
formatter.originalToStaticPropertyName('A_SomeEntity', 'SomeProperty')
).toBe('SOME_PROPERTY');
Expand Down
2 changes: 1 addition & 1 deletion packages/odata-common/src/entity-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ export function entityBuilder<
Object.values(entityApi.schema).forEach((field: any) => {
const fieldName = `${camelCase(field._fieldName)}`;
builder[fieldName] = function (value) {
this.entity[fieldName] = value;
this._entity[fieldName] = value;
return this;
};
});
Expand Down
14 changes: 8 additions & 6 deletions packages/odata-common/src/entity-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ export class EntityBuilder<
EntityT extends EntityBase,
DeSerializersT extends DeSerializers
> {
protected entity: EntityT;
protected _entity: EntityT;

constructor(private _entityApi: EntityApi<EntityT, DeSerializersT>) {
if (!this.entity) {
this.entity = new _entityApi.entityConstructor(_entityApi.schema);
if (!this._entity) {
this._entity = new _entityApi.entityConstructor(_entityApi.schema);
}
}

Expand All @@ -59,7 +59,7 @@ export class EntityBuilder<
*/
public withCustomFields(customFields: Record<string, any>): this {
const validCustomFields = this.filterCustomFields(customFields);
this.entity = this.entity.setCustomFields(validCustomFields);
this._entity = this._entity.setCustomFields(validCustomFields);
return this;
}

Expand All @@ -68,8 +68,10 @@ export class EntityBuilder<
* @returns The entity.
*/
public build(): EntityT {
const entity = this.entity;
this.entity = new this._entityApi.entityConstructor(this._entityApi.schema);
const entity = this._entity;
this._entity = new this._entityApi.entityConstructor(
this._entityApi.schema
);
return entity;
}

Expand Down

0 comments on commit 0675ee3

Please sign in to comment.