Skip to content

Commit

Permalink
improve: support for oneOf and anyOf in array sample (#4136)
Browse files Browse the repository at this point in the history
* Fixed oneOf and anyOf in array example

* Added tests for sampleFromSchema for array type

* Removed return example for array item
  • Loading branch information
PavelStefanov authored and shockey committed Mar 3, 2018
1 parent feef20d commit b13b05e
Show file tree
Hide file tree
Showing 2 changed files with 175 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/core/plugins/samples/fn.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ export const sampleFromSchema = (schema, config={}) => {
}

if(type === "array") {
if(Array.isArray(items.anyOf)) {
return items.anyOf.map(i => sampleFromSchema(i, config))
}

if(Array.isArray(items.oneOf)) {
return items.oneOf.map(i => sampleFromSchema(i, config))
}

return [ sampleFromSchema(items, config) ]
}

Expand Down
167 changes: 167 additions & 0 deletions test/core/plugins/samples/fn.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,173 @@ describe("sampleFromSchema", function() {

expect(sampleFromSchema(definition, { includeWriteOnly: true })).toEqual(expected)
})

describe("for array type", function() {
it("returns array with sample of array type", function() {
var definition = {
type: "array",
items: {
type: "integer"
}
}

var expected = [ 0 ]

expect(sampleFromSchema(definition)).toEqual(expected)
})

it("returns array of examples for array that has example", function() {
var definition = {
type: "array",
items: {
type: "string"
},
example: "dog"
}

var expected = [ "dog" ]

expect(sampleFromSchema(definition)).toEqual(expected)
})

it("returns array of examples for array that has examples", function() {
var definition = {
type: "array",
items: {
type: "string",
},
example: [ "dog", "cat" ]
}

var expected = [ "dog", "cat" ]

expect(sampleFromSchema(definition)).toEqual(expected)
})

it("returns array of samples for oneOf type", function() {
var definition = {
type: "array",
items: {
type: "string",
oneOf: [
{
type: "integer"
}
]
}
}

var expected = [ 0 ]

expect(sampleFromSchema(definition)).toEqual(expected)
})

it("returns array of samples for oneOf types", function() {
var definition = {
type: "array",
items: {
type: "string",
oneOf: [
{
type: "string"
},
{
type: "integer"
}
]
}
}

var expected = [ "string", 0 ]

expect(sampleFromSchema(definition)).toEqual(expected)
})

it("returns array of samples for oneOf examples", function() {
var definition = {
type: "array",
items: {
type: "string",
oneOf: [
{
type: "string",
example: "dog"
},
{
type: "integer",
example: 1
}
]
}
}

var expected = [ "dog", 1 ]

expect(sampleFromSchema(definition)).toEqual(expected)
})

it("returns array of samples for anyOf type", function() {
var definition = {
type: "array",
items: {
type: "string",
anyOf: [
{
type: "integer"
}
]
}
}

var expected = [ 0 ]

expect(sampleFromSchema(definition)).toEqual(expected)
})

it("returns array of samples for anyOf types", function() {
var definition = {
type: "array",
items: {
type: "string",
anyOf: [
{
type: "string"
},
{
type: "integer"
}
]
}
}

var expected = [ "string", 0 ]

expect(sampleFromSchema(definition)).toEqual(expected)
})

it("returns array of samples for anyOf examples", function() {
var definition = {
type: "array",
items: {
type: "string",
anyOf: [
{
type: "string",
example: "dog"
},
{
type: "integer",
example: 1
}
]
}
}

var expected = [ "dog", 1 ]

expect(sampleFromSchema(definition)).toEqual(expected)
})
})
})

describe("createXMLExample", function () {
Expand Down

0 comments on commit b13b05e

Please sign in to comment.