Browse Source

Add writeOnly support to sampleFromSchema; add tests for the same

bubble
Kyle Shockey 7 years ago
parent
commit
9af5d2dc66
No known key found for this signature in database GPG Key ID: DC20D559FFBC0D36
2 changed files with 107 additions and 4 deletions
  1. +7
    -3
      src/core/plugins/samples/fn.js
  2. +100
    -1
      test/core/plugins/samples/fn.js

+ 7
- 3
src/core/plugins/samples/fn.js View File

@@ -27,7 +27,7 @@ const primitive = (schema) => {


export const sampleFromSchema = (schema, config={}) => { export const sampleFromSchema = (schema, config={}) => {
let { type, example, properties, additionalProperties, items } = objectify(schema) let { type, example, properties, additionalProperties, items } = objectify(schema)
let { includeReadOnly } = config
let { includeReadOnly, includeWriteOnly } = config


if(example !== undefined) if(example !== undefined)
return example return example
@@ -46,9 +46,13 @@ export const sampleFromSchema = (schema, config={}) => {
let props = objectify(properties) let props = objectify(properties)
let obj = {} let obj = {}
for (var name in props) { 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 ) { if ( additionalProperties === true ) {


+ 100
- 1
test/core/plugins/samples/fn.js View File

@@ -1,6 +1,105 @@
import { createXMLExample } from "corePlugins/samples/fn"
import { createXMLExample, sampleFromSchema } from "corePlugins/samples/fn"
import expect from "expect" 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 () { describe("createXMLExample", function () {
var sut = createXMLExample var sut = createXMLExample


Loading…
Cancel
Save