Skip to content

Commit 71fa41a

Browse files
10.0.1 (#68)
* 10.0.1 release - fix serialization bug * fix deserializer for falsy values
1 parent 165d3fe commit 71fa41a

6 files changed

Lines changed: 139 additions & 44 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Klaviyo Typescript SDK
22

3-
- SDK version: 10.0.0
3+
- SDK version: 10.0.1
44

55
- Revision: 2024-05-15
66

@@ -48,7 +48,7 @@ This SDK is organized into the following resources:
4848

4949
You can install this library using `npm`.
5050

51-
`npm install [email protected].0`
51+
`npm install [email protected].1`
5252

5353

5454
## source code

api/apis.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import {AxiosRequestConfig, AxiosResponse, AxiosHeaders, isAxiosError} from "axi
3636
export { RequestFile } from '../model/models';
3737

3838
const revision = "2024-05-15";
39-
const userAgent = "klaviyo-api-node/10.0.0";
39+
const userAgent = "klaviyo-api-node/10.0.1";
4040

4141
export class RetryOptions {
4242

model/models.ts

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3487,7 +3487,7 @@ const oneOfMapNoDiscriminator: {[index: string]: Array<any>} = {
34873487
}
34883488

34893489
export class ObjectSerializer {
3490-
public static findCorrectType(data: any, expectedType: string) {
3490+
public static findCorrectType(data: any, expectedType: string, serializer: boolean) {
34913491
if (data == undefined) {
34923492
return expectedType;
34933493
} else if (primitives.indexOf(expectedType.toLowerCase()) !== -1) {
@@ -3505,9 +3505,16 @@ export class ObjectSerializer {
35053505
// the type does not have a discriminator.
35063506
if (oneOfMapNoDiscriminator[expectedType]) {
35073507
for (const index in oneOfMapNoDiscriminator[expectedType]) {
3508-
if (ObjectSerializer.validateType(data, typeMap[oneOfMapNoDiscriminator[expectedType][index]])) {
3509-
return oneOfMapNoDiscriminator[expectedType][index];
3508+
if (serializer) {
3509+
if (ObjectSerializer.serializerValidateType(data, typeMap[oneOfMapNoDiscriminator[expectedType][index]])) {
3510+
return oneOfMapNoDiscriminator[expectedType][index];
3511+
}
3512+
} else {
3513+
if (ObjectSerializer.deserializerValidateType(data, typeMap[oneOfMapNoDiscriminator[expectedType][index]])) {
3514+
return oneOfMapNoDiscriminator[expectedType][index];
3515+
}
35103516
}
3517+
35113518
}
35123519
}
35133520
return expectedType; // discriminator was not present (or an empty string)
@@ -3527,10 +3534,10 @@ export class ObjectSerializer {
35273534
}
35283535
}
35293536

3530-
public static validateType(data: any, potentialType: any): boolean {
3537+
public static deserializerValidateType(data: any, potentialType: any): boolean {
35313538
for (const index in potentialType.getAttributeTypeMap()) {
35323539
const attribute = potentialType.getAttributeTypeMap()[index];
3533-
if (!data[attribute.baseName]) {
3540+
if (!data.hasOwnProperty(attribute.baseName)) {
35343541
return false;
35353542
}
35363543
if (enumsMap[attribute.type]) {
@@ -3541,6 +3548,16 @@ export class ObjectSerializer {
35413548
}
35423549
return true;
35433550
}
3551+
public static serializerValidateType(data: Object, potentialType: any): boolean {
3552+
const properties = Object.getOwnPropertyNames(data)
3553+
for (const index in properties) {
3554+
const property = properties[index]
3555+
if( !potentialType.getAttributeTypeMap().find((attribute) => attribute.name === property)) {
3556+
return false
3557+
}
3558+
}
3559+
return true
3560+
}
35443561

35453562
public static serialize(data: any, type: string) {
35463563
if (data == undefined) {
@@ -3562,12 +3579,12 @@ export class ObjectSerializer {
35623579
if (enumsMap[type]) {
35633580
return data;
35643581
}
3565-
if (!typeMap[type]) { // in case we dont know the type
3566-
return data;
3582+
if (!typeMap[type] && !oneOfMapNoDiscriminator[type]) { // in case we dont know the type
3583+
return data
35673584
}
35683585

35693586
// Get the actual type of this object
3570-
type = this.findCorrectType(data, type);
3587+
type = this.findCorrectType(data, type, true);
35713588

35723589
// get the map for the correct type.
35733590
let attributeTypes = typeMap[type].getAttributeTypeMap();
@@ -3582,7 +3599,7 @@ export class ObjectSerializer {
35823599

35833600
public static deserialize(data: any, type: string) {
35843601
// polymorphism may change the actual type.
3585-
type = ObjectSerializer.findCorrectType(data, type);
3602+
type = ObjectSerializer.findCorrectType(data, type, false);
35863603
if (data == undefined) {
35873604
return data;
35883605
} else if (primitives.indexOf(type.toLowerCase()) !== -1) {

package-lock.json

Lines changed: 33 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "klaviyo-api",
3-
"version": "10.0.0",
3+
"version": "10.0.1",
44
"description": "A typescript client for the Klaviyo API",
55
"repository": {
66
"type": "git",

test/api.test.ts

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
import {describe, expect, jest, test} from '@jest/globals';
2-
import {AccountsApi, ApiKeySession, OAuthBasicSession, RetryOptions} from '../api';
2+
import {
3+
AccountsApi,
4+
ApiKeySession,
5+
CampaignMessageCreateQueryResourceObjectAttributes,
6+
CampaignResponseObjectResourceAttributes,
7+
EmailSendOptionsSubObject,
8+
EmailTrackingOptionsSubObject,
9+
OAuthBasicSession,
10+
ObjectSerializer,
11+
RetryOptions,
12+
} from '../api';
313
import axios from 'axios';
414

515
jest.mock('axios', () => jest.fn())
@@ -41,4 +51,68 @@ describe('retry', () => {
4151
expect(mockedAxios).toHaveBeenCalledTimes(3)
4252
}
4353
});
44-
});
54+
});
55+
describe('Serialize', () => {
56+
test('an oneOf item serializes correctly', async () => {
57+
let campaign: CampaignMessageCreateQueryResourceObjectAttributes = {
58+
channel: 'email',
59+
content: {
60+
subject: 'Hello',
61+
fromEmail: '[email protected]',
62+
fromLabel: 'Foo Bar'
63+
}
64+
}
65+
const serialized = ObjectSerializer.serialize(campaign, 'CampaignMessageCreateQueryResourceObjectAttributes')
66+
expect(serialized).toEqual({
67+
channel: 'email',
68+
content: {
69+
subject: 'Hello',
70+
from_email: '[email protected]',
71+
from_label: 'Foo Bar'
72+
}
73+
})
74+
});
75+
});
76+
describe('deserialize', () => {
77+
test('an oneOf item deserializes correctly', async () => {
78+
const serialized = {
79+
"name": "Email",
80+
"status": "Draft",
81+
"archived": false,
82+
"audiences": {
83+
"included": [
84+
"TEST_ID"
85+
],
86+
"excluded": []
87+
},
88+
"send_options": {
89+
"use_smart_sending": true,
90+
"ignore_unsubscribes": false
91+
},
92+
"tracking_options": {
93+
"is_add_utm": false,
94+
"utm_params": [],
95+
"is_tracking_clicks": true,
96+
"is_tracking_opens": true
97+
},
98+
"send_strategy": {
99+
"method": "static",
100+
"options_static": {
101+
"datetime": "2024-05-22T19:14:32+00:00",
102+
"is_local": false,
103+
"send_past_recipients_immediately": null
104+
},
105+
"options_throttled": null,
106+
"options_sto": null
107+
},
108+
"created_at": "2024-05-22T19:14:32.047339+00:00",
109+
"scheduled_at": null,
110+
"updated_at": "2024-05-22T19:14:32.140795+00:00",
111+
"send_time": null
112+
}
113+
const deserialize: CampaignResponseObjectResourceAttributes = ObjectSerializer.deserialize(serialized, 'CampaignResponseObjectResourceAttributes')
114+
expect(deserialize.trackingOptions).toBeInstanceOf(EmailTrackingOptionsSubObject)
115+
expect(deserialize.sendOptions).toBeInstanceOf(EmailSendOptionsSubObject)
116+
117+
});
118+
});

0 commit comments

Comments
 (0)