From 9af5d2dc66045ef04e832be29beef842d6cc1e83 Mon Sep 17 00:00:00 2001 From: Kyle Shockey Date: Thu, 3 Aug 2017 17:23:16 -0700 Subject: [PATCH] Add writeOnly support to sampleFromSchema; add tests for the same --- src/core/plugins/samples/fn.js | 10 +++- test/core/plugins/samples/fn.js | 101 +++++++++++++++++++++++++++++++- 2 files changed, 107 insertions(+), 4 deletions(-) diff --git a/src/core/plugins/samples/fn.js b/src/core/plugins/samples/fn.js index f2233554..a729600c 100644 --- a/src/core/plugins/samples/fn.js +++ b/src/core/plugins/samples/fn.js @@ -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 ) { diff --git a/test/core/plugins/samples/fn.js b/test/core/plugins/samples/fn.js index e5032b50..12ec783f 100644 --- a/test/core/plugins/samples/fn.js +++ b/test/core/plugins/samples/fn.js @@ -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