Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 3859 Added support for oneOf and anyOf in array sample #4136

Merged
merged 14 commits into from
Mar 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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