Skip to content

Commit f11581d

Browse files
committed
More unit tests and fix
1 parent e5cf6e7 commit f11581d

File tree

2 files changed

+64
-15
lines changed

2 files changed

+64
-15
lines changed

src/main/DeserializationHelper.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,6 @@ export const DeserializeComplexType = (
174174
if (metadata === undefined) {
175175
metadata = { name: key, required: false, access: AccessType.BOTH };
176176
}
177-
if (config.ignoreNameMetadata === true) {
178-
metadata.name = undefined;
179-
}
180177
// tslint:disable-next-line:triple-equals
181178
if (AccessType.WRITE_ONLY != metadata.access) {
182179
/**
@@ -193,7 +190,8 @@ export const DeserializeComplexType = (
193190
);
194191
}
195192
// tslint:disable-next-line:triple-equals
196-
const jsonKeyName = metadata.name != undefined ? metadata.name : key;
193+
const jsonKeyName =
194+
config.ignoreNameMetadata === true ? key : metadata.name || key;
197195
// tslint:disable-next-line:triple-equals
198196
if (json[jsonKeyName] != undefined) {
199197
/**

src/test/index.spec.ts

Lines changed: 62 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -722,22 +722,73 @@ describe("Testing JsonIgnore decorator", () => {
722722
expect(testInstance.state).toBe("old");
723723
});
724724

725-
it("Testing DeserializationConfig", () => {
726-
class Event {
725+
describe("DeserializationConfig", () => {
726+
it("{ ignoreNameMetadata: true } should use property name", () => {
727+
class Event {
728+
@JsonProperty()
729+
id: number = undefined;
730+
@JsonProperty({ name: "geo-location" })
731+
location = "old";
732+
}
733+
734+
const json = {
735+
id: "1",
736+
location: "Canberra",
737+
};
738+
739+
const testInstance: Event = ObjectMapper.deserialize(Event, json, {
740+
ignoreNameMetadata: true,
741+
});
742+
expect(testInstance.location).toBe("Canberra");
743+
});
744+
745+
it("{ ignoreNameMetadata: true } should use name metadata during serialization", () => {
746+
class Event {
747+
@JsonProperty()
748+
id: number = undefined;
749+
@JsonProperty({ name: "geo-location" })
750+
location = "old";
751+
}
752+
753+
const json = {
754+
id: "1",
755+
location: "Canberra",
756+
};
757+
758+
const testInstance: Event = ObjectMapper.deserialize(Event, json, {
759+
ignoreNameMetadata: true,
760+
});
761+
762+
const serialized = ObjectMapper.serialize(testInstance);
763+
expect(serialized).toBe('{"id":"1","geo-location":"Canberra"}');
764+
});
765+
});
766+
767+
it("{ ignoreNameMetadata: true } should work with array serialization", () => {
768+
class DeserializeComplexTypeArrayTest {
727769
@JsonProperty()
728-
id: number = undefined;
729-
@JsonProperty({ name: "geo-location" })
730-
location = "old";
770+
storeName: string = undefined;
771+
772+
@JsonProperty({ name: "AVAILABLE_AT" })
773+
availableAt: String[] = undefined;
731774
}
732775

733776
const json = {
734-
id: "1",
735-
location: "Canberra",
777+
storeName: "PizzaHut",
778+
availableAt: ["2000", "3000", "4000", "5000"],
736779
};
737780

738-
const testInstance: Event = ObjectMapper.deserialize(Event, json, {
739-
ignoreNameMetadata: true,
740-
});
741-
expect(testInstance.location).toBe("Canberra");
781+
const testInstance = ObjectMapper.deserialize(
782+
DeserializeComplexTypeArrayTest,
783+
json,
784+
{
785+
ignoreNameMetadata: true,
786+
}
787+
);
788+
789+
const serialized = ObjectMapper.serialize(testInstance);
790+
expect(serialized).toBe(
791+
'{"storeName":"PizzaHut","AVAILABLE_AT":["2000","3000","4000","5000"]}'
792+
);
742793
});
743794
});

0 commit comments

Comments
 (0)