Skip to content

Commit

Permalink
fix implementation and test
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni committed May 1, 2024
1 parent d1e64d6 commit 8484a2d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/generators/python/PythonConstrainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ export const PythonDefaultTypeMapping: PythonTypeMapping = {
const unionTypes = constrainedModel.union.map((unionModel) => {
return unionModel.type;
});
return unionTypes.join(' | ');
const uniqueSet = new Set(unionTypes);
return [...uniqueSet].join(' | ');
},
Dictionary({ constrainedModel }): string {
return `dict[${constrainedModel.key.type}, ${constrainedModel.value.type}]`;
Expand Down
25 changes: 24 additions & 1 deletion test/generators/python/PythonConstrainer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,29 @@ describe('PythonConstrainer', () => {
expect(type).toEqual('str');
});
test('should render multiple types', () => {
const unionModel1 = new ConstrainedStringModel(
'test',
undefined,
{},
'str'
);
const unionModel2 = new ConstrainedStringModel(
'test',
undefined,
{},
'str2'
);
const model = new ConstrainedUnionModel('test', undefined, {}, '', [
unionModel1,
unionModel2
]);
const type = PythonDefaultTypeMapping.Union({
constrainedModel: model,
...defaultOptions
});
expect(type).toEqual('str | str2');
});
test('should render unique types', () => {
const unionModel1 = new ConstrainedStringModel(
'test',
undefined,
Expand All @@ -219,7 +242,7 @@ describe('PythonConstrainer', () => {
constrainedModel: model,
...defaultOptions
});
expect(type).toEqual('str | str');
expect(type).toEqual('str');
});
});

Expand Down

0 comments on commit 8484a2d

Please sign in to comment.