Skip to content

Commit

Permalink
fix(json-mapper): fix deserialize method when the input is an Array o…
Browse files Browse the repository at this point in the history
…f model
  • Loading branch information
Romakita committed Sep 13, 2023
1 parent e9811c5 commit 3ae4858
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 5 deletions.
6 changes: 3 additions & 3 deletions packages/specs/json-mapper/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ module.exports = {
roots: ["<rootDir>/src", "<rootDir>/test"],
coverageThreshold: {
global: {
statements: 99.66,
branches: 97.94,
statements: 99.53,
branches: 97.46,
functions: 100,
lines: 99.66
lines: 99.53
}
},
moduleNameMapper: {
Expand Down
60 changes: 60 additions & 0 deletions packages/specs/json-mapper/src/domain/JsonSerializer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,20 @@ describe("JsonSerializer", () => {
arrayLike.push(1);

expect(serialize(arrayLike)).toEqual([1]);

class SetLike extends Set {}

const setLike = new SetLike();
setLike.add(1);

expect(serialize(setLike)).toEqual([1]);

class MapLike extends Map {}

const mapLike = new MapLike();
mapLike.set("i", 1);

expect(serialize(mapLike)).toEqual({i: 1});
});
});
describe("Map<primitive>", () => {
Expand Down Expand Up @@ -644,6 +658,52 @@ describe("JsonSerializer", () => {
prop4: null
});
});
it("should serialize array model with alias property", () => {
class SpaBooking {
@Required()
@Name("booking_number")
bookingNumber: string;

@Required()
status: string;

@Required()
@Name("order_id")
orderId: number;

@Required()
@Name("appointment_id")
appointmentId: number;

@Name("customer_id")
@Groups("!create", "!read")
customerId: number;
}

const appointments = [
{
bookingNumber: "100566224434",
status: "Booked",
orderId: 711376505,
appointmentId: 566224434,
customerId: null
}
];

const serializedResult = serialize(appointments, {
type: SpaBooking
});

expect(serializedResult).toEqual([
{
booking_number: "100566224434",
status: "Booked",
order_id: 711376505,
appointment_id: 566224434,
customer_id: null
}
]);
});
});
describe("class with toJSON/toClass", () => {
it("should serialize model", () => {
Expand Down
9 changes: 7 additions & 2 deletions packages/specs/json-mapper/src/domain/JsonSerializer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
classOf,
hasJsonMethod,
isArray,
isBoolean,
isClassObject,
isCollection,
Expand All @@ -21,10 +22,14 @@ import {getJsonMapperTypes} from "./JsonMapperTypesContainer";
import {JsonSerializerOptions} from "./JsonSerializerOptions";
import {Writer} from "./Writer";

const getCollectionType = (input: any) => {
return isArray(input) ? "Array" : input instanceof Set ? "Set" : input instanceof Map ? "Map" : undefined;
};

function getBestType(type: Type<any>, obj: any) {
const dataType = classOf(obj);

if (dataType && !isClassObject(dataType)) {
if (dataType && !isClassObject(dataType) && !isCollection(obj)) {
return dataType;
}

Expand Down Expand Up @@ -229,7 +234,7 @@ export class JsonSerializer extends JsonMapperCompiler<JsonSerializerOptions> {
}

if (input && isCollection(input)) {
return this.execMapper(nameOf(classOf(input)), input, options);
return this.execMapper(getCollectionType(input), input, options);

Check failure on line 237 in packages/specs/json-mapper/src/domain/JsonSerializer.ts

View workflow job for this annotation

GitHub Actions / test-graphql (18.x)

Argument of type 'string | undefined' is not assignable to parameter of type 'string'.

Check failure on line 237 in packages/specs/json-mapper/src/domain/JsonSerializer.ts

View workflow job for this annotation

GitHub Actions / test-integration (ubuntu-latest, 19.x)

Argument of type 'string | undefined' is not assignable to parameter of type 'string'.

Check failure on line 237 in packages/specs/json-mapper/src/domain/JsonSerializer.ts

View workflow job for this annotation

GitHub Actions / test-formio (18.x)

Argument of type 'string | undefined' is not assignable to parameter of type 'string'.

Check failure on line 237 in packages/specs/json-mapper/src/domain/JsonSerializer.ts

View workflow job for this annotation

GitHub Actions / test-core (18.x)

Argument of type 'string | undefined' is not assignable to parameter of type 'string'.

Check failure on line 237 in packages/specs/json-mapper/src/domain/JsonSerializer.ts

View workflow job for this annotation

GitHub Actions / test-third-parties (18.x)

Argument of type 'string | undefined' is not assignable to parameter of type 'string'.

Check failure on line 237 in packages/specs/json-mapper/src/domain/JsonSerializer.ts

View workflow job for this annotation

GitHub Actions / test-core (18.x)

Argument of type 'string | undefined' is not assignable to parameter of type 'string'.

Check failure on line 237 in packages/specs/json-mapper/src/domain/JsonSerializer.ts

View workflow job for this annotation

GitHub Actions / test-platform (18.x)

Argument of type 'string | undefined' is not assignable to parameter of type 'string'.

Check failure on line 237 in packages/specs/json-mapper/src/domain/JsonSerializer.ts

View workflow job for this annotation

GitHub Actions / test-integration (ubuntu-latest, 18.x)

Argument of type 'string | undefined' is not assignable to parameter of type 'string'.

Check failure on line 237 in packages/specs/json-mapper/src/domain/JsonSerializer.ts

View workflow job for this annotation

GitHub Actions / test-orm (18.x)

Argument of type 'string | undefined' is not assignable to parameter of type 'string'.

Check failure on line 237 in packages/specs/json-mapper/src/domain/JsonSerializer.ts

View workflow job for this annotation

GitHub Actions / test-security (18.x)

Argument of type 'string | undefined' is not assignable to parameter of type 'string'.

Check failure on line 237 in packages/specs/json-mapper/src/domain/JsonSerializer.ts

View workflow job for this annotation

GitHub Actions / test-specs (18.x)

Argument of type 'string | undefined' is not assignable to parameter of type 'string'.

Check failure on line 237 in packages/specs/json-mapper/src/domain/JsonSerializer.ts

View workflow job for this annotation

GitHub Actions / test-orm (18.x)

Argument of type 'string | undefined' is not assignable to parameter of type 'string'.

Check failure on line 237 in packages/specs/json-mapper/src/domain/JsonSerializer.ts

View workflow job for this annotation

GitHub Actions / test-integration (ubuntu-latest, 18.x)

Argument of type 'string | undefined' is not assignable to parameter of type 'string'.

Check failure on line 237 in packages/specs/json-mapper/src/domain/JsonSerializer.ts

View workflow job for this annotation

GitHub Actions / test-specs (18.x)

Argument of type 'string | undefined' is not assignable to parameter of type 'string'.

Check failure on line 237 in packages/specs/json-mapper/src/domain/JsonSerializer.ts

View workflow job for this annotation

GitHub Actions / test-integration (ubuntu-latest, 16.x)

Argument of type 'string | undefined' is not assignable to parameter of type 'string'.

Check failure on line 237 in packages/specs/json-mapper/src/domain/JsonSerializer.ts

View workflow job for this annotation

GitHub Actions / test-integration (ubuntu-latest, 19.x)

Argument of type 'string | undefined' is not assignable to parameter of type 'string'.

Check failure on line 237 in packages/specs/json-mapper/src/domain/JsonSerializer.ts

View workflow job for this annotation

GitHub Actions / test-third-parties (18.x)

Argument of type 'string | undefined' is not assignable to parameter of type 'string'.

Check failure on line 237 in packages/specs/json-mapper/src/domain/JsonSerializer.ts

View workflow job for this annotation

GitHub Actions / test-graphql (18.x)

Argument of type 'string | undefined' is not assignable to parameter of type 'string'.

Check failure on line 237 in packages/specs/json-mapper/src/domain/JsonSerializer.ts

View workflow job for this annotation

GitHub Actions / test-platform (18.x)

Argument of type 'string | undefined' is not assignable to parameter of type 'string'.

Check failure on line 237 in packages/specs/json-mapper/src/domain/JsonSerializer.ts

View workflow job for this annotation

GitHub Actions / test-formio (18.x)

Argument of type 'string | undefined' is not assignable to parameter of type 'string'.

Check failure on line 237 in packages/specs/json-mapper/src/domain/JsonSerializer.ts

View workflow job for this annotation

GitHub Actions / test-security (18.x)

Argument of type 'string | undefined' is not assignable to parameter of type 'string'.
}

if (hasJsonMethod(input)) {
Expand Down

0 comments on commit 3ae4858

Please sign in to comment.