Skip to content

Commit dbd4e06

Browse files
authored
Merge pull request #37 from amsross/master
Handle examples where the component type is "array"
2 parents 6f779d5 + d6c8b45 commit dbd4e06

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

lib/example-data-extractor.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ ExampleDataExtractor.prototype.extract = function(component, root, options) {
4646
reduced = this.extract(root, root, options);
4747
} else if (component.properties) {
4848
reduced = this.mapPropertiesToExamples(component.properties, root, options);
49+
} else if (component.type && component.type === "array" ) {
50+
var minItems = component.minItems || 1;
51+
var maxItems = component.maxItems || 1;
52+
reduced = [];
53+
_.range(_.random(minItems, maxItems)).forEach(function(i) {
54+
reduced.push( this.extract(component.items, root, options) );
55+
}.bind(this));
4956
}
5057
// Optionally merge in additional properties
5158
// @TODO: Determine if this is the right thing to do

test/fixtures/schema1.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,28 @@
142142
"targetSchema": {
143143
"rel": "self"
144144
}
145+
},
146+
{
147+
"title": "Get many foos",
148+
"href": "/fixtures/foos",
149+
"method": "GET",
150+
"schema": {
151+
"type": "object",
152+
"description": "Queriable properties",
153+
"properties": {
154+
"foo": {
155+
"$ref": "#/definitions/foo_prop"
156+
}
157+
}
158+
},
159+
"targetSchema": {
160+
"type": "array",
161+
"minItems": 2,
162+
"maxItems": 5,
163+
"items": {
164+
"rel": "self"
165+
}
166+
}
145167
}
146168
]
147169
}

test/lib/transformer.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,5 +156,33 @@ describe('Schema Transformer', function() {
156156
plus_one: 'bar'
157157
});
158158
});
159+
160+
it('should handle rel=self references as an array', function() {
161+
var data = this.transformer.generateExample(this.schema1.links[3].targetSchema, this.schema1);
162+
expect(data).to.be.an('array');
163+
expect(data.length).to.be.gte(2);
164+
expect(data.length).to.be.lte(5);
165+
expect(data[0]).to.deep.equal({
166+
id: 123,
167+
foo: 'bar',
168+
baz: 'boo',
169+
array_prop: ['bar'],
170+
boo: {
171+
attribute_one: 'One'
172+
},
173+
nested_object: {
174+
baz: 'boo',
175+
foo: 'bar'
176+
},
177+
composite: {
178+
attribute_one: 'One',
179+
attribute_two: 'Two'
180+
},
181+
option: {
182+
attribute_two: 'Two'
183+
},
184+
plus_one: 'bar'
185+
});
186+
});
159187
});
160188
});

0 commit comments

Comments
 (0)