Skip to content

Commit

Permalink
fix(json-mapper): fix issue with ReadOnly decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed Aug 29, 2022
1 parent b0b4360 commit 78eced3
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 5 deletions.
4 changes: 2 additions & 2 deletions packages/orm/mongoose/src/utils/createSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ export function buildMongooseSchema(target: any): MongooseSchemaMetadata {
return;
}

// Keeping the Mongoose Schema separate so it can overwrite everything once schema has been built.
// Keeping the Mongoose Schema separate, so it can overwrite everything once schema has been built.
const schemaTypeOptions: any = propertyMetadata.store.get(MONGOOSE_SCHEMA) || {};

if (schemaTypeOptions.schemaIgnore || propertyMetadata.schema.isReadOnly) {
if (schemaTypeOptions.schemaIgnore || propertyMetadata.isGetterOnly()) {
return;
}

Expand Down
4 changes: 3 additions & 1 deletion packages/orm/mongoose/test/readonly.integration.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Inject, Injectable, PlatformTest} from "@tsed/common";
import {getJsonSchema, Groups, Name, Property, ReadOnly, Required} from "@tsed/schema";
import {TestMongooseContext} from "@tsed/testing-mongoose";
import {Immutable, Model, MongooseModel, ObjectID} from "../src/index";
import {Immutable, Model, MongooseModel, ObjectID, SchemaIgnore} from "../src/index";

class BaseModel {
@ObjectID("id")
Expand All @@ -12,12 +12,14 @@ class BaseModel {
@Required()
@Immutable()
@ReadOnly()
@SchemaIgnore()
@Name("created_at")
@Groups("!create", "!update")
createdAt: number = Date.now();

@Required()
@ReadOnly()
@SchemaIgnore()
@Name("updated_at")
@Groups("!create", "!update")
updatedAt: number = Date.now();
Expand Down
2 changes: 1 addition & 1 deletion packages/specs/json-mapper/src/utils/deserialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export function plainObjectToClass<T = any>(src: any, options: JsonDeserializerO
collectionType: propStore.collectionType
});

if (!propStore.schema.isReadOnly) {
if (!propStore.isGetterOnly()) {
if (value !== undefined) {
out[propStore.propertyName] = value;
} else if (options.partial) {
Expand Down
32 changes: 32 additions & 0 deletions packages/specs/schema/src/domain/JsonEntityStore.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ describe("JsonEntityStore", () => {
expect(storeProp.isPrimitive).toBe(true);
expect(storeProp.isObject).toBe(false);
expect(storeProp.isClass).toBe(false);
expect(storeProp.isGetterOnly()).toBeFalsy();

// METHOD
const storeMethod = JsonEntityStore.from(Model).children.get("method") as JsonMethodStore;
Expand Down Expand Up @@ -92,6 +93,7 @@ describe("JsonEntityStore", () => {
expect(storeParam.isPrimitive).toBe(true);
expect(storeParam.isObject).toBe(false);
expect(storeParam.isClass).toBe(false);
expect(storeParam.isGetterOnly()).toBe(false);
});
it("should manage enum from babel", () => {
enum MyEnum {
Expand All @@ -110,4 +112,34 @@ describe("JsonEntityStore", () => {

expect(store.type).toEqual(String);
});

describe("isGetterOnly()", () => {
it("should create JsonEntityStore on getter", () => {
class Model {
@Property()
get id() {
return "id";
}
}

// CLASS
// PROPERTY
const storeProp = JsonEntityStore.from(Model).children.get("id") as JsonPropertyStore;
expect(storeProp.isGetterOnly()).toBeTruthy();
});
it("should create JsonEntityStore on getter/setter", () => {
class Model {
@Property()
get id() {
return "id";
}
set id(id: string) {}
}

// CLASS
// PROPERTY
const storeProp = JsonEntityStore.from(Model).children.get("id") as JsonPropertyStore;
expect(storeProp.isGetterOnly()).toBeFalsy();
});
});
});
5 changes: 4 additions & 1 deletion packages/specs/schema/src/domain/JsonEntityStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export abstract class JsonEntityStore implements JsonEntityStoreOptions {
/**
* Method's descriptor
*/
readonly descriptor: number;
readonly descriptor: PropertyDescriptor;
/**
* Decorator type used to declare the JsonSchemaStore.
*/
Expand Down Expand Up @@ -95,6 +95,9 @@ export abstract class JsonEntityStore implements JsonEntityStoreOptions {
this.parent = this;
}

isGetterOnly() {
return isObject(this.descriptor) && !this.descriptor.value && this.descriptor.get && !this.descriptor.set;
}
/**
* Return the class name of the entity.
* @returns {string}
Expand Down

0 comments on commit 78eced3

Please sign in to comment.