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
This commit is contained in:
Pavel Stefanov
2018-03-03 03:42:51 +03:00
committed by kyle
parent feef20dd67
commit b13b05e416
2 changed files with 175 additions and 0 deletions

View File

@@ -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 () {