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

fix: prevent usage of superclass schema #2126

Merged
merged 1 commit into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/app/core/entity/database-entity-decorator.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { DatabaseEntity } from "./database-entity.decorator";
import { Entity } from "./model/entity";

describe("DatabaseEntityDecorator", () => {
it("should use a new schema object", () => {
@DatabaseEntity("OnlyEntityDecorator")
class OnlyEntityDecorator extends Entity {}

expect(OnlyEntityDecorator.schema).not.toBe(Entity.schema);
});
});
6 changes: 3 additions & 3 deletions src/app/core/entity/database-entity.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Entity, EntityConstructor } from "./model/entity";
import { Registry } from "../config/registry/dynamic-registry";
import { getEntitySchema } from "./database-field.decorator";

export class EntityRegistry extends Registry<EntityConstructor> {}

Expand Down Expand Up @@ -27,8 +28,7 @@ export function DatabaseEntity(entityType: string) {

// append parent schema definitions
const parentConstructor = Object.getPrototypeOf(constructor);
parentConstructor.schema.forEach((value, key) =>
constructor.schema.set(key, value),
);
const schema = getEntitySchema(constructor);
parentConstructor.schema.forEach((value, key) => schema.set(key, value));
};
}
18 changes: 13 additions & 5 deletions src/app/core/entity/database-field.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import "reflect-metadata";
import { EntitySchemaField } from "./schema/entity-schema-field";
import { EntitySchema } from "./schema/entity-schema";

/**
* Decorator (Annotation `@DatabaseField()`) to mark a property of an Entity that should be persisted in the database.
Expand Down Expand Up @@ -29,11 +30,18 @@ export function addPropertySchema(
propertySchema: EntitySchemaField,
) {
target[propertyName] = undefined; // This ensures that the field is not read only
getEntitySchema(target.constructor).set(propertyName, propertySchema);
return target;
}

if (Object.getOwnPropertyDescriptor(target.constructor, "schema") == null) {
target.constructor.schema = new Map<string, EntitySchemaField>();
/**
* Returns the schema map of this entity (not the superclass).
* Creates and assigns a new one if it doesn't exist yet.
* @param ctor
*/
export function getEntitySchema(ctor): EntitySchema {
if (Object.getOwnPropertyDescriptor(ctor, "schema") == null) {
ctor.schema = new Map<string, EntitySchemaField>();
}
target.constructor.schema.set(propertyName, propertySchema);

return target;
return ctor.schema;
}