diff --git a/src/core/components/param-body.jsx b/src/core/components/param-body.jsx index e64a65cc905..48373f5fc97 100644 --- a/src/core/components/param-body.jsx +++ b/src/core/components/param-body.jsx @@ -69,7 +69,9 @@ export default class ParamBody extends PureComponent { let { param, fn:{inferSchema} } = this.props let schema = inferSchema(param.toJS()) - return getSampleSchema(schema, xml) + return getSampleSchema(schema, xml, { + includeWriteOnly: true + }) } onChange = (value, { isEditBox, isXml }) => { diff --git a/src/core/components/response.jsx b/src/core/components/response.jsx index 9d30849d8a7..8c0a8bf11bf 100644 --- a/src/core/components/response.jsx +++ b/src/core/components/response.jsx @@ -83,11 +83,16 @@ export default class Response extends React.Component { if(isOAS3()) { let oas3SchemaForContentType = response.getIn(["content", this.state.responseContentType, "schema"]) - sampleResponse = oas3SchemaForContentType ? getSampleSchema(oas3SchemaForContentType.toJS(), this.state.responseContentType, { includeReadOnly: true }) : null + sampleResponse = oas3SchemaForContentType ? getSampleSchema(oas3SchemaForContentType.toJS(), this.state.responseContentType, { + includeReadOnly: true + }) : null schema = oas3SchemaForContentType ? inferSchema(oas3SchemaForContentType.toJS()) : null } else { schema = inferSchema(response.toJS()) - sampleResponse = schema ? getSampleSchema(schema, contentType, { includeReadOnly: true }) : null + sampleResponse = schema ? getSampleSchema(schema, contentType, { + includeReadOnly: true, + includeWriteOnly: true // writeOnly has no filtering effect in swagger 2.0 + }) : null } let example = getExampleComponent( sampleResponse, examples, HighlightCode ) diff --git a/src/core/plugins/oas3/components/request-body.jsx b/src/core/plugins/oas3/components/request-body.jsx index cce3b9f3f48..2cbf491a2e5 100644 --- a/src/core/plugins/oas3/components/request-body.jsx +++ b/src/core/plugins/oas3/components/request-body.jsx @@ -16,7 +16,9 @@ const RequestBody = ({ requestBody, getComponent, specSelectors, contentType }) const mediaTypeValue = requestBodyContent.get(contentType) - const sampleSchema = getSampleSchema(mediaTypeValue.get("schema").toJS(), contentType) + const sampleSchema = getSampleSchema(mediaTypeValue.get("schema").toJS(), contentType, { + includeWriteOnly: true + }) return
{ requestBodyDescription && diff --git a/src/core/plugins/samples/fn.js b/src/core/plugins/samples/fn.js index d4f46c1b760..9f166f208d6 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) => { @@ -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,16 +46,20 @@ 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], config) } if ( additionalProperties === true ) { obj.additionalProp1 = {} } else if ( additionalProperties ) { let additionalProps = objectify(additionalProperties) - let additionalPropVal = sampleFromSchema(additionalProps, { includeReadOnly: includeReadOnly }) + let additionalPropVal = sampleFromSchema(additionalProps, config) for (let i = 1; i < 4; i++) { obj["additionalProp" + i] = additionalPropVal @@ -65,7 +69,7 @@ export const sampleFromSchema = (schema, config={}) => { } if(type === "array") { - return [ sampleFromSchema(items, { includeReadOnly: includeReadOnly }) ] + return [ sampleFromSchema(items, config) ] } if(schema["enum"]) { @@ -96,7 +100,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 +181,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].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 { - res[displayName].push(t) - } + if ( props[propName].readOnly && !includeReadOnly ) { + continue + } + if ( props[propName].writeOnly && !includeWriteOnly ) { + continue + } + 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 { + res[displayName].push(t) } + } } diff --git a/test/core/plugins/samples/fn.js b/test/core/plugins/samples/fn.js index a2d01876ad2..12ec783f7c7 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 @@ -554,6 +653,69 @@ describe("createXMLExample", function () { expect(sut(definition, { includeReadOnly: false })).toEqual(expected) }) + it("returns object with readonly fields for parameter, with includeReadOnly", function () { + var expected = "\n\n\t0\n\tstring\n" + var definition = { + type: "object", + properties: { + id: { + type: "integer" + }, + dog: { + readOnly: true, + type: "string" + } + }, + xml: { + name: "animals" + } + } + + expect(sut(definition, { includeReadOnly: true })).toEqual(expected) + }) + + it("returns object without writeonly fields for parameter", function () { + var expected = "\n\n\t0\n" + var definition = { + type: "object", + properties: { + id: { + type: "integer" + }, + dog: { + writeOnly: true, + type: "string" + } + }, + xml: { + name: "animals" + } + } + + expect(sut(definition)).toEqual(expected) + }) + + it("returns object with writeonly fields for parameter, with includeWriteOnly", function () { + var expected = "\n\n\t0\n\tstring\n" + var definition = { + type: "object", + properties: { + id: { + type: "integer" + }, + dog: { + writeOnly: true, + type: "string" + } + }, + xml: { + name: "animals" + } + } + + expect(sut(definition, { includeWriteOnly: true })).toEqual(expected) + }) + it("returns object with passed property as attribute", function () { var expected = "\n\n\tstring\n" var definition = {