Skip to content

Commit

Permalink
feat(type-generator): support properties with type array and undefine…
Browse files Browse the repository at this point in the history
…d items (#1203)

If a property is of `type: array` but does not have `items` or `items`
are not of `object` type, then code generation fails.

Instead of failing, this PR would let it fallback to array of `any` item
types.

Fixes #1194

---------

Signed-off-by: Vinayak Kukreja <vinakuk@amazon.com>
  • Loading branch information
vinayak-kukreja committed Oct 5, 2023
1 parent 762707c commit f81fa55
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/type-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,8 @@ export class TypeGenerator {

private typeForArray(propertyFqn: string, def: JSONSchema4): EmittedType {
if (!def.items || typeof(def.items) !== 'object') {
throw new Error(`unsupported array type ${def.items}`);
// Falling back to an array of any type
def.items = {};
}

return this.typeForProperty(propertyFqn, def.items);
Expand Down
56 changes: 56 additions & 0 deletions test/__snapshots__/tojson.test.ts.snap

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

33 changes: 33 additions & 0 deletions test/tojson.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,39 @@ describe('arrays', () => {
});
});

test('without items', () => {
const toJson = generateToJson({
properties: {
ArrayProp: { type: 'array' },
},
});

expect(toJson({
arrayProp: ['bello', { foo: 88, bar: 'world' }],
})).toStrictEqual({
ArrayProp: ['bello', { foo: 88, bar: 'world' }],
});
});

test('with items', () => {
const toJson = generateToJson({
properties: {
ArrayProp: {
type: 'array',
items: {
type: 'string',
},
},
},
});

expect(toJson({
arrayProp: ['bello', { foo: 88, bar: 'world' }],
})).toStrictEqual({
ArrayProp: ['bello', { foo: 88, bar: 'world' }],
});
});

});

test('any', () => {
Expand Down

0 comments on commit f81fa55

Please sign in to comment.