From 5e96821c0515158b7d1f44ab6fd28b06db0271f7 Mon Sep 17 00:00:00 2001 From: Kyle Shockey Date: Thu, 3 Aug 2017 17:13:38 -0700 Subject: [PATCH] Add writeOnly logic --- src/core/plugins/samples/fn.js | 45 +++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/src/core/plugins/samples/fn.js b/src/core/plugins/samples/fn.js index d4f46c1b..f2233554 100644 --- a/src/core/plugins/samples/fn.js +++ b/src/core/plugins/samples/fn.js @@ -9,7 +9,7 @@ const primitives = { "number": () => 0, "number_float": () => 0.0, "integer": () => 0, - "boolean": (schema) => typeof schema.default === "boolean" ? schema.default : true + "boolean": (schema) => typeof schema.default === "boolean" ? schema.default : true } const primitive = (schema) => { @@ -96,7 +96,7 @@ export const inferSchema = (thing) => { export const sampleXmlFromSchema = (schema, config={}) => { let objectifySchema = objectify(schema) let { type, properties, additionalProperties, items, example } = objectifySchema - let { includeReadOnly } = config + let { includeReadOnly, includeWriteOnly } = config let defaultValue = objectifySchema.default let res = {} let _attr = {} @@ -177,27 +177,32 @@ export const sampleXmlFromSchema = (schema, config={}) => { example = example || {} for (let propName in props) { - if ( !props[propName].readOnly || includeReadOnly ) { - props[propName].xml = props[propName].xml || {} + if ( props[propName].readOnly && !includeReadOnly ) { + continue + } + if ( props[propName].writeOnly && !includeWriteOnly ) { + continue + } - if (props[propName].xml.attribute) { - let enumAttrVal = Array.isArray(props[propName].enum) && props[propName].enum[0] - let attrExample = props[propName].example - let attrDefault = props[propName].default - _attr[props[propName].xml.name || propName] = attrExample!== undefined && attrExample - || example[propName] !== undefined && example[propName] || attrDefault !== undefined && attrDefault - || enumAttrVal || primitive(props[propName]) + props[propName].xml = props[propName].xml || {} + + if (props[propName].xml.attribute) { + let enumAttrVal = Array.isArray(props[propName].enum) && props[propName].enum[0] + let attrExample = props[propName].example + let attrDefault = props[propName].default + _attr[props[propName].xml.name || propName] = attrExample!== undefined && attrExample + || example[propName] !== undefined && example[propName] || attrDefault !== undefined && attrDefault + || enumAttrVal || primitive(props[propName]) + } else { + props[propName].xml.name = props[propName].xml.name || propName + props[propName].example = props[propName].example !== undefined ? props[propName].example : example[propName] + let t = sampleXmlFromSchema(props[propName]) + if (Array.isArray(t)) { + res[displayName] = res[displayName].concat(t) } else { - props[propName].xml.name = props[propName].xml.name || propName - props[propName].example = props[propName].example !== undefined ? props[propName].example : example[propName] - let t = sampleXmlFromSchema(props[propName]) - if (Array.isArray(t)) { - res[displayName] = res[displayName].concat(t) - } else { - res[displayName].push(t) - } - + res[displayName].push(t) } + } }