Skip to content

Commit

Permalink
10.0.1 (#68)
Browse files Browse the repository at this point in the history
* 10.0.1 release - fix serialization bug

* fix deserializer for falsy values
  • Loading branch information
Ian-Montgomery-Klaviyo authored May 22, 2024
1 parent 165d3fe commit 71fa41a
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 44 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Klaviyo Typescript SDK

- SDK version: 10.0.0
- SDK version: 10.0.1

- Revision: 2024-05-15

Expand Down Expand Up @@ -48,7 +48,7 @@ This SDK is organized into the following resources:

You can install this library using `npm`.

`npm install klaviyo-api@10.0.0`
`npm install klaviyo-api@10.0.1`


## source code
Expand Down
2 changes: 1 addition & 1 deletion api/apis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import {AxiosRequestConfig, AxiosResponse, AxiosHeaders, isAxiosError} from "axi
export { RequestFile } from '../model/models';

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

export class RetryOptions {

Expand Down
35 changes: 26 additions & 9 deletions model/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3487,7 +3487,7 @@ const oneOfMapNoDiscriminator: {[index: string]: Array<any>} = {
}

export class ObjectSerializer {
public static findCorrectType(data: any, expectedType: string) {
public static findCorrectType(data: any, expectedType: string, serializer: boolean) {
if (data == undefined) {
return expectedType;
} else if (primitives.indexOf(expectedType.toLowerCase()) !== -1) {
Expand All @@ -3505,9 +3505,16 @@ export class ObjectSerializer {
// the type does not have a discriminator.
if (oneOfMapNoDiscriminator[expectedType]) {
for (const index in oneOfMapNoDiscriminator[expectedType]) {
if (ObjectSerializer.validateType(data, typeMap[oneOfMapNoDiscriminator[expectedType][index]])) {
return oneOfMapNoDiscriminator[expectedType][index];
if (serializer) {
if (ObjectSerializer.serializerValidateType(data, typeMap[oneOfMapNoDiscriminator[expectedType][index]])) {
return oneOfMapNoDiscriminator[expectedType][index];
}
} else {
if (ObjectSerializer.deserializerValidateType(data, typeMap[oneOfMapNoDiscriminator[expectedType][index]])) {
return oneOfMapNoDiscriminator[expectedType][index];
}
}

}
}
return expectedType; // discriminator was not present (or an empty string)
Expand All @@ -3527,10 +3534,10 @@ export class ObjectSerializer {
}
}

public static validateType(data: any, potentialType: any): boolean {
public static deserializerValidateType(data: any, potentialType: any): boolean {
for (const index in potentialType.getAttributeTypeMap()) {
const attribute = potentialType.getAttributeTypeMap()[index];
if (!data[attribute.baseName]) {
if (!data.hasOwnProperty(attribute.baseName)) {
return false;
}
if (enumsMap[attribute.type]) {
Expand All @@ -3541,6 +3548,16 @@ export class ObjectSerializer {
}
return true;
}
public static serializerValidateType(data: Object, potentialType: any): boolean {
const properties = Object.getOwnPropertyNames(data)
for (const index in properties) {
const property = properties[index]
if( !potentialType.getAttributeTypeMap().find((attribute) => attribute.name === property)) {
return false
}
}
return true
}

public static serialize(data: any, type: string) {
if (data == undefined) {
Expand All @@ -3562,12 +3579,12 @@ export class ObjectSerializer {
if (enumsMap[type]) {
return data;
}
if (!typeMap[type]) { // in case we dont know the type
return data;
if (!typeMap[type] && !oneOfMapNoDiscriminator[type]) { // in case we dont know the type
return data
}

// Get the actual type of this object
type = this.findCorrectType(data, type);
type = this.findCorrectType(data, type, true);

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

public static deserialize(data: any, type: string) {
// polymorphism may change the actual type.
type = ObjectSerializer.findCorrectType(data, type);
type = ObjectSerializer.findCorrectType(data, type, false);
if (data == undefined) {
return data;
} else if (primitives.indexOf(type.toLowerCase()) !== -1) {
Expand Down
62 changes: 33 additions & 29 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "klaviyo-api",
"version": "10.0.0",
"version": "10.0.1",
"description": "A typescript client for the Klaviyo API",
"repository": {
"type": "git",
Expand Down
78 changes: 76 additions & 2 deletions test/api.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import {describe, expect, jest, test} from '@jest/globals';
import {AccountsApi, ApiKeySession, OAuthBasicSession, RetryOptions} from '../api';
import {
AccountsApi,
ApiKeySession,
CampaignMessageCreateQueryResourceObjectAttributes,
CampaignResponseObjectResourceAttributes,
EmailSendOptionsSubObject,
EmailTrackingOptionsSubObject,
OAuthBasicSession,
ObjectSerializer,
RetryOptions,
} from '../api';
import axios from 'axios';

jest.mock('axios', () => jest.fn())
Expand Down Expand Up @@ -41,4 +51,68 @@ describe('retry', () => {
expect(mockedAxios).toHaveBeenCalledTimes(3)
}
});
});
});
describe('Serialize', () => {
test('an oneOf item serializes correctly', async () => {
let campaign: CampaignMessageCreateQueryResourceObjectAttributes = {
channel: 'email',
content: {
subject: 'Hello',
fromEmail: 'foo.bar@example.com',
fromLabel: 'Foo Bar'
}
}
const serialized = ObjectSerializer.serialize(campaign, 'CampaignMessageCreateQueryResourceObjectAttributes')
expect(serialized).toEqual({
channel: 'email',
content: {
subject: 'Hello',
from_email: 'foo.bar@example.com',
from_label: 'Foo Bar'
}
})
});
});
describe('deserialize', () => {
test('an oneOf item deserializes correctly', async () => {
const serialized = {
"name": "Email",
"status": "Draft",
"archived": false,
"audiences": {
"included": [
"TEST_ID"
],
"excluded": []
},
"send_options": {
"use_smart_sending": true,
"ignore_unsubscribes": false
},
"tracking_options": {
"is_add_utm": false,
"utm_params": [],
"is_tracking_clicks": true,
"is_tracking_opens": true
},
"send_strategy": {
"method": "static",
"options_static": {
"datetime": "2024-05-22T19:14:32+00:00",
"is_local": false,
"send_past_recipients_immediately": null
},
"options_throttled": null,
"options_sto": null
},
"created_at": "2024-05-22T19:14:32.047339+00:00",
"scheduled_at": null,
"updated_at": "2024-05-22T19:14:32.140795+00:00",
"send_time": null
}
const deserialize: CampaignResponseObjectResourceAttributes = ObjectSerializer.deserialize(serialized, 'CampaignResponseObjectResourceAttributes')
expect(deserialize.trackingOptions).toBeInstanceOf(EmailTrackingOptionsSubObject)
expect(deserialize.sendOptions).toBeInstanceOf(EmailSendOptionsSubObject)

});
});

0 comments on commit 71fa41a

Please sign in to comment.