Skip to content

Commit b13b05e

Browse files
PavelStefanovshockey
authored andcommitted
improve: support for oneOf and anyOf in array sample (#4136)
* Fixed oneOf and anyOf in array example * Added tests for sampleFromSchema for array type * Removed return example for array item
1 parent feef20d commit b13b05e

File tree

2 files changed

+175
-0
lines changed
  • src/core/plugins/samples
  • test/core/plugins/samples

2 files changed

+175
-0
lines changed

src/core/plugins/samples/fn.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ export const sampleFromSchema = (schema, config={}) => {
6969
}
7070

7171
if(type === "array") {
72+
if(Array.isArray(items.anyOf)) {
73+
return items.anyOf.map(i => sampleFromSchema(i, config))
74+
}
75+
76+
if(Array.isArray(items.oneOf)) {
77+
return items.oneOf.map(i => sampleFromSchema(i, config))
78+
}
79+
7280
return [ sampleFromSchema(items, config) ]
7381
}
7482

test/core/plugins/samples/fn.js

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,173 @@ describe("sampleFromSchema", function() {
9999

100100
expect(sampleFromSchema(definition, { includeWriteOnly: true })).toEqual(expected)
101101
})
102+
103+
describe("for array type", function() {
104+
it("returns array with sample of array type", function() {
105+
var definition = {
106+
type: "array",
107+
items: {
108+
type: "integer"
109+
}
110+
}
111+
112+
var expected = [ 0 ]
113+
114+
expect(sampleFromSchema(definition)).toEqual(expected)
115+
})
116+
117+
it("returns array of examples for array that has example", function() {
118+
var definition = {
119+
type: "array",
120+
items: {
121+
type: "string"
122+
},
123+
example: "dog"
124+
}
125+
126+
var expected = [ "dog" ]
127+
128+
expect(sampleFromSchema(definition)).toEqual(expected)
129+
})
130+
131+
it("returns array of examples for array that has examples", function() {
132+
var definition = {
133+
type: "array",
134+
items: {
135+
type: "string",
136+
},
137+
example: [ "dog", "cat" ]
138+
}
139+
140+
var expected = [ "dog", "cat" ]
141+
142+
expect(sampleFromSchema(definition)).toEqual(expected)
143+
})
144+
145+
it("returns array of samples for oneOf type", function() {
146+
var definition = {
147+
type: "array",
148+
items: {
149+
type: "string",
150+
oneOf: [
151+
{
152+
type: "integer"
153+
}
154+
]
155+
}
156+
}
157+
158+
var expected = [ 0 ]
159+
160+
expect(sampleFromSchema(definition)).toEqual(expected)
161+
})
162+
163+
it("returns array of samples for oneOf types", function() {
164+
var definition = {
165+
type: "array",
166+
items: {
167+
type: "string",
168+
oneOf: [
169+
{
170+
type: "string"
171+
},
172+
{
173+
type: "integer"
174+
}
175+
]
176+
}
177+
}
178+
179+
var expected = [ "string", 0 ]
180+
181+
expect(sampleFromSchema(definition)).toEqual(expected)
182+
})
183+
184+
it("returns array of samples for oneOf examples", function() {
185+
var definition = {
186+
type: "array",
187+
items: {
188+
type: "string",
189+
oneOf: [
190+
{
191+
type: "string",
192+
example: "dog"
193+
},
194+
{
195+
type: "integer",
196+
example: 1
197+
}
198+
]
199+
}
200+
}
201+
202+
var expected = [ "dog", 1 ]
203+
204+
expect(sampleFromSchema(definition)).toEqual(expected)
205+
})
206+
207+
it("returns array of samples for anyOf type", function() {
208+
var definition = {
209+
type: "array",
210+
items: {
211+
type: "string",
212+
anyOf: [
213+
{
214+
type: "integer"
215+
}
216+
]
217+
}
218+
}
219+
220+
var expected = [ 0 ]
221+
222+
expect(sampleFromSchema(definition)).toEqual(expected)
223+
})
224+
225+
it("returns array of samples for anyOf types", function() {
226+
var definition = {
227+
type: "array",
228+
items: {
229+
type: "string",
230+
anyOf: [
231+
{
232+
type: "string"
233+
},
234+
{
235+
type: "integer"
236+
}
237+
]
238+
}
239+
}
240+
241+
var expected = [ "string", 0 ]
242+
243+
expect(sampleFromSchema(definition)).toEqual(expected)
244+
})
245+
246+
it("returns array of samples for anyOf examples", function() {
247+
var definition = {
248+
type: "array",
249+
items: {
250+
type: "string",
251+
anyOf: [
252+
{
253+
type: "string",
254+
example: "dog"
255+
},
256+
{
257+
type: "integer",
258+
example: 1
259+
}
260+
]
261+
}
262+
}
263+
264+
var expected = [ "dog", 1 ]
265+
266+
expect(sampleFromSchema(definition)).toEqual(expected)
267+
})
268+
})
102269
})
103270

104271
describe("createXMLExample", function () {

0 commit comments

Comments
 (0)