Skip to content

Commit

Permalink
fix(json-mapper): add implementation on PrimitiveMapper.serialize method
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed Apr 26, 2024
1 parent f0973f9 commit 96e45fa
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 10 deletions.
38 changes: 35 additions & 3 deletions packages/specs/json-mapper/src/components/PrimitiveMapper.spec.ts
Expand Up @@ -125,7 +125,7 @@ describe("PrimitiveMapper", () => {
});
it("should return value (null => boolean)", () => {
const mapper = new PrimitiveMapper();
const ctx = {
const ctx: any = {
type: Boolean,
collectionType: undefined,
next: Sinon.stub()
Expand All @@ -136,12 +136,44 @@ describe("PrimitiveMapper", () => {
});
});
describe("serialize()", () => {
it("should return value", () => {
it("should return value (string to string)", () => {
const mapper = new PrimitiveMapper();

const value = mapper.serialize("1");
const value = mapper.serialize("1", {type: String} as any);

expect(value).toEqual("1");
});

it("should return value (string to number)", () => {
const mapper = new PrimitiveMapper();

const value = mapper.serialize("1", {type: Number} as any);

expect(value).toEqual(1);
});

it("should return value (object)", () => {
const mapper = new PrimitiveMapper();

// in this case it's probably intended to be an object (or an error but we can decide for the developer and we can broke the code)
// TODO: for the major version, we can return undefined or throw an error?
const value = mapper.serialize({"1": "1"} as any, {type: Number} as any);

expect(value).toEqual({"1": "1"});
});

it("should return value (null)", () => {
const mapper = new PrimitiveMapper();
const value = mapper.serialize(null as any, {type: Number} as any);

expect(value).toEqual(null);
});

it("should return value (undefined)", () => {
const mapper = new PrimitiveMapper();
const value = mapper.serialize(undefined as any, {type: Number} as any);

expect(value).toEqual(undefined);
});
});
});
5 changes: 3 additions & 2 deletions packages/specs/json-mapper/src/components/PrimitiveMapper.ts
Expand Up @@ -25,8 +25,8 @@ export class PrimitiveMapper implements JsonMapperMethods {
return (this as any)[nameOf(ctx.type)] ? (this as any)[nameOf(ctx.type)](data, ctx) : undefined;
}

serialize(object: string | number | boolean | BigInt): string | number | boolean | BigInt {
return object;
serialize(object: string | number | boolean | BigInt, ctx: JsonMapperCtx): string | number | boolean | BigInt {
return (this as any)[nameOf(ctx?.type)] && typeof object !== "object" ? (this as any)[nameOf(ctx.type)](object, ctx) : object;
}

protected String(data: any) {
Expand All @@ -44,6 +44,7 @@ export class PrimitiveMapper implements JsonMapperMethods {

protected Number(data: any) {
if (isNullish(data)) return null;
if (data === undefined) return data;

const n = +data;

Expand Down
7 changes: 6 additions & 1 deletion packages/specs/json-mapper/src/domain/JsonSerializer.ts
Expand Up @@ -121,7 +121,12 @@ export class JsonSerializer extends JsonMapperCompiler<JsonSerializerOptions> {

types.forEach((mapper, model) => {
if (![Array, Set, Map].includes(model as any)) {
this.addTypeMapper(model as any, mapper.serialize.bind(mapper));
this.addTypeMapper(model as any, (value: any, options: any) =>
mapper.serialize(value, {
...options,
type: model
})
);
}
});

Expand Down
17 changes: 17 additions & 0 deletions packages/specs/json-mapper/src/utils/serialize.spec.ts
@@ -1,4 +1,5 @@
import "../components/PrimitiveMapper";
import {Property} from "@tsed/schema";
import {serialize} from "./serialize";

describe("serialize()", () => {
Expand All @@ -14,5 +15,21 @@ describe("serialize()", () => {
expect(serialize(0)).toEqual(0);
expect(serialize(1)).toEqual(1);
expect(serialize(BigInt(1n))).toEqual(BigInt(1));

class Entity {
@Property()
num: number;
}

expect(
serialize(
{
num: "1"
},
{type: Entity}
)
).toEqual({
num: 1
});
});
});
8 changes: 4 additions & 4 deletions packages/specs/swagger/test/swagger.integration.spec.ts
Expand Up @@ -81,11 +81,11 @@ describe("Swagger integration", () => {

expect(result.body).toEqual([
{
id: 1,
id: "1",
name: "name"
},
{
id: 2,
id: "2",
name: "name"
}
]);
Expand Down Expand Up @@ -113,11 +113,11 @@ describe("Swagger integration", () => {

expect(result.body).toEqual([
{
id: 1,
id: "1",
name: "name"
},
{
id: 2,
id: "2",
name: "name"
}
]);
Expand Down

0 comments on commit 96e45fa

Please sign in to comment.