Skip to content

Commit

Permalink
fix: unable to serialize array with unique items (#98)
Browse files Browse the repository at this point in the history
* chore: reproduce failure

* chore: update unit tests

* fix: error in classes with parent BaseModel

* chore: fix invalid sub type test

---------

Co-authored-by: Eduardo Rodrigues <eduardomourar@users.noreply.github.com>
  • Loading branch information
eduardomourar and eduardomourar authored Feb 13, 2023
1 parent f449617 commit 3b44251
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
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

0 comments on commit 3b44251

Please sign in to comment.