Fix issue when undefined or missing object entries were incorrectly converted to aqua (#140)

This commit is contained in:
Pavel 2022-03-17 16:27:33 +03:00 committed by GitHub
parent a0a7a9e19b
commit 4910901dc9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 1 deletions

View File

@ -185,3 +185,39 @@ describe('Conversion from aqua to typescript', () => {
},
);
});
describe('Conversion corner cases', () => {
it('Should accept undefined in object entry', () => {
// arrange
const type = {
tag: 'labeledProduct',
fields: {
x: opt_i32,
y: opt_i32,
},
} as const;
const valueInTs = {
x: 1,
};
const valueInAqua = {
x: [1],
y: [],
};
// act
const aqua = ts2aqua(valueInTs, type);
const ts = aqua2ts(valueInAqua, type);
// assert
expect(aqua).toStrictEqual({
x: [1],
y: [],
});
expect(ts).toStrictEqual({
x: 1,
y: null,
});
});
});

View File

@ -96,7 +96,7 @@ export const ts2aqua = (value: any, type: NonArrowType) => {
return null;
})
.with({ tag: 'option' }, (opt) => {
if (value === null) {
if (value === null || value === undefined) {
return [];
} else {
return [ts2aqua(value, opt.type)];