Skip to content

fix: unable to serialize array with unique items #98

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/recast.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { InvalidRequest } from './exceptions';
import { Callable, integer, Integer } from './interface';
import { BaseModel, Callable, integer, Integer } from './interface';

type primitive = string | number | boolean | bigint | integer | object;

Expand Down Expand Up @@ -70,7 +70,7 @@ export const transformValue = (
});
} else {
// if type is plain object, we leave it as is
if (Object.is(cls, Object)) {
if (Object.is(cls, Object) || cls.prototype instanceof BaseModel) {
return value;
}
if (
Expand Down
19 changes: 19 additions & 0 deletions tests/data/sample-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,25 @@ export class DeeperDict extends BaseModel {
deepestList?: Optional<Array<integer>>;
}

export class TagsModel extends BaseModel {
['constructor']: typeof TagsModel;

@Expose({ name: 'Tags' })
@Transform((value, obj) => transformValue(Tag, 'tags', value, obj, [Set]), {
toClassOnly: true,
})
tags?: Optional<Set<Tag>>;
}

class Tag extends BaseModel {
['constructor']: typeof Tag;

@Expose({ name: 'Name' })
name: string;
@Expose({ name: 'Value' })
value: string;
}

export class SimpleResourceModel extends BaseModel {
['constructor']: typeof SimpleResourceModel;

Expand Down
19 changes: 17 additions & 2 deletions tests/lib/recast.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { transformValue, recastPrimitive } from '~/recast';
import {
ResourceModel as ComplexResourceModel,
SimpleResourceModel,
TagsModel,
} from '../data/sample-model';

describe('when recasting objects', () => {
Expand Down Expand Up @@ -105,15 +106,29 @@ describe('when recasting objects', () => {
);
});

test('recast set type - array with unique items', () => {
const payload = {
Tags: [{ key: 'name', value: 'value' }],
};
const expected = {
Tags: new Set([{ key: 'name', value: 'value' }]),
};
const model = TagsModel.deserialize(payload);
const serialized = JSON.parse(JSON.stringify(model));
expect(serialized).toMatchObject(expected);
expect(TagsModel.deserialize(serialized).serialize()).toMatchObject(expected);
});

test('recast object invalid sub type', () => {
class InvalidClass {}
const k = 'key';
const v = { a: 1, b: 2 };
const recastObject = () => {
transformValue(SimpleResourceModel, k, v, {});
transformValue(InvalidClass, k, v, {});
};
expect(recastObject).toThrow(exceptions.InvalidRequest);
expect(recastObject).toThrow(
`Unsupported type: ${typeof v} [${SimpleResourceModel.name}] for ${k}`
`Unsupported type: ${typeof v} [${InvalidClass.name}] for ${k}`
);
});

Expand Down