Skip to content

Commit

Permalink
feat: support type array
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip authored and RomanHotsiy committed May 25, 2021
1 parent 8ba3c31 commit 7bdeccd
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/infer.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const schemaKeywordTypes = {

export function inferType(schema) {
if (schema.type !== undefined) {
return schema.type;
return Array.isArray(schema.type) ? schema.type.length === 0 ? null : schema.type[0] : schema.type;
}
const keywords = Object.keys(schemaKeywordTypes);
for (var i = 0; i < keywords.length; i++) {
Expand Down
3 changes: 3 additions & 0 deletions src/traverse.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ export function traverse(schema, options, spec, context) {
example = schema.examples[0];
} else {
type = schema.type;
if (Array.isArray(type) && schema.type.length > 0) {
type = schema.type[0];
}
if (!type) {
type = inferType(schema);
}
Expand Down
18 changes: 18 additions & 0 deletions test/integration.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,31 @@ describe('Integration', function() {
expect(result).to.deep.equal(expected);
});

it('should support type array', function() {
schema = {
'type': ['string', 'number']
};
result = JSONSchemaSampler.sample(schema);
expected = 'string';
expect(result).to.deep.equal(expected);
});

it('should use null if type is not specified', function() {
schema = {
};
result = OpenAPISampler.sample(schema);
expected = null;
expect(result).to.deep.equal(expected);
});

it('should use null if type array is empty', function() {
schema = {
type: []
};
result = JSONSchemaSampler.sample(schema);
expected = null;
expect(result).to.deep.equal(expected);
});
});

describe('Objects', function() {
Expand Down

0 comments on commit 7bdeccd

Please sign in to comment.