Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/core/components/param-body.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) => {
Expand Down
9 changes: 7 additions & 2 deletions src/core/components/response.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 )

Expand Down
4 changes: 3 additions & 1 deletion src/core/plugins/oas3/components/request-body.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 <div>
{ requestBodyDescription &&
Expand Down
61 changes: 35 additions & 26 deletions src/core/plugins/samples/fn.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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
Expand All @@ -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
Expand All @@ -65,7 +69,7 @@ export const sampleFromSchema = (schema, config={}) => {
}

if(type === "array") {
return [ sampleFromSchema(items, { includeReadOnly: includeReadOnly }) ]
return [ sampleFromSchema(items, config) ]
}

if(schema["enum"]) {
Expand Down Expand Up @@ -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 = {}
Expand Down Expand Up @@ -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)
}

}
}

Expand Down
164 changes: 163 additions & 1 deletion test/core/plugins/samples/fn.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<animals>\n\t<id>0</id>\n\t<dog>string</dog>\n</animals>"
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 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<animals>\n\t<id>0</id>\n</animals>"
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 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<animals>\n\t<id>0</id>\n\t<dog>string</dog>\n</animals>"
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 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<animals id=\"0\">\n\t<dog>string</dog>\n</animals>"
var definition = {
Expand Down