Add writeOnly support to sampleFromSchema; add tests for the same
This commit is contained in:
@@ -27,7 +27,7 @@ const primitive = (schema) => {
|
||||
|
||||
export const sampleFromSchema = (schema, config={}) => {
|
||||
let { type, example, properties, additionalProperties, items } = objectify(schema)
|
||||
let { includeReadOnly } = config
|
||||
let { includeReadOnly, includeWriteOnly } = config
|
||||
|
||||
if(example !== undefined)
|
||||
return example
|
||||
@@ -46,9 +46,13 @@ export const sampleFromSchema = (schema, config={}) => {
|
||||
let props = objectify(properties)
|
||||
let obj = {}
|
||||
for (var name in props) {
|
||||
if ( !props[name].readOnly || includeReadOnly ) {
|
||||
obj[name] = sampleFromSchema(props[name], { includeReadOnly: includeReadOnly })
|
||||
if ( props[name].readOnly && !includeReadOnly ) {
|
||||
continue
|
||||
}
|
||||
if ( props[name].writeOnly && !includeWriteOnly ) {
|
||||
continue
|
||||
}
|
||||
obj[name] = sampleFromSchema(props[name], { includeReadOnly: includeReadOnly })
|
||||
}
|
||||
|
||||
if ( additionalProperties === true ) {
|
||||
|
||||
@@ -1,6 +1,105 @@
|
||||
import { createXMLExample } from "corePlugins/samples/fn"
|
||||
import { createXMLExample, sampleFromSchema } from "corePlugins/samples/fn"
|
||||
import expect from "expect"
|
||||
|
||||
describe("sampleFromSchema", function() {
|
||||
it("returns object with no readonly fields for parameter", function () {
|
||||
var definition = {
|
||||
type: "object",
|
||||
properties: {
|
||||
id: {
|
||||
type: "integer"
|
||||
},
|
||||
readOnlyDog: {
|
||||
readOnly: true,
|
||||
type: "string"
|
||||
}
|
||||
},
|
||||
xml: {
|
||||
name: "animals"
|
||||
}
|
||||
}
|
||||
|
||||
var expected = {
|
||||
id: 0
|
||||
}
|
||||
|
||||
expect(sampleFromSchema(definition, { includeReadOnly: false })).toEqual(expected)
|
||||
})
|
||||
|
||||
it("returns object with readonly fields for parameter, with includeReadOnly", function () {
|
||||
var definition = {
|
||||
type: "object",
|
||||
properties: {
|
||||
id: {
|
||||
type: "integer"
|
||||
},
|
||||
readOnlyDog: {
|
||||
readOnly: true,
|
||||
type: "string"
|
||||
}
|
||||
},
|
||||
xml: {
|
||||
name: "animals"
|
||||
}
|
||||
}
|
||||
|
||||
var expected = {
|
||||
id: 0,
|
||||
readOnlyDog: "string"
|
||||
}
|
||||
|
||||
expect(sampleFromSchema(definition, { includeReadOnly: true })).toEqual(expected)
|
||||
})
|
||||
|
||||
it("returns object without writeonly fields for parameter", function () {
|
||||
var definition = {
|
||||
type: "object",
|
||||
properties: {
|
||||
id: {
|
||||
type: "integer"
|
||||
},
|
||||
writeOnlyDog: {
|
||||
writeOnly: true,
|
||||
type: "string"
|
||||
}
|
||||
},
|
||||
xml: {
|
||||
name: "animals"
|
||||
}
|
||||
}
|
||||
|
||||
var expected = {
|
||||
id: 0
|
||||
}
|
||||
|
||||
expect(sampleFromSchema(definition)).toEqual(expected)
|
||||
})
|
||||
|
||||
it("returns object with writeonly fields for parameter, with includeWriteOnly", function () {
|
||||
var definition = {
|
||||
type: "object",
|
||||
properties: {
|
||||
id: {
|
||||
type: "integer"
|
||||
},
|
||||
writeOnlyDog: {
|
||||
writeOnly: true,
|
||||
type: "string"
|
||||
}
|
||||
},
|
||||
xml: {
|
||||
name: "animals"
|
||||
}
|
||||
}
|
||||
|
||||
var expected = {
|
||||
id: 0,
|
||||
writeOnlyDog: "string"
|
||||
}
|
||||
|
||||
expect(sampleFromSchema(definition, { includeWriteOnly: true })).toEqual(expected)
|
||||
})
|
||||
})
|
||||
|
||||
describe("createXMLExample", function () {
|
||||
var sut = createXMLExample
|
||||
|
||||
Reference in New Issue
Block a user