diff --git a/src/core/json-schema-components.js b/src/core/json-schema-components.js index a1d38771..63a4dbfc 100644 --- a/src/core/json-schema-components.js +++ b/src/core/json-schema-components.js @@ -111,7 +111,7 @@ export class JsonSchema_array extends PureComponent { constructor(props, context) { super(props, context) - this.state = {value: props.value} + this.state = { value: valueOrEmptyList(props.value)} } componentWillReceiveProps(props) { @@ -135,7 +135,7 @@ export class JsonSchema_array extends PureComponent { addItem = () => { this.setState(state => { - state.value = state.value || List() + state.value = valueOrEmptyList(state.value) return { value: state.value.push("") } @@ -174,7 +174,7 @@ export class JsonSchema_array extends PureComponent { return (
- { !value || value.count() < 1 ? null : + { !value || !value.count || value.count() < 1 ? null : value.map( (item,i) => { let schema = Object.assign({}, itemSchema) if ( errors.length ) { @@ -264,3 +264,7 @@ export class JsonSchema_object extends PureComponent { } } + +function valueOrEmptyList(value) { + return List.isList(value) ? value : List() +} \ No newline at end of file diff --git a/src/core/plugins/oas3/components/request-body.jsx b/src/core/plugins/oas3/components/request-body.jsx index 93923bb1..9685d8dd 100644 --- a/src/core/plugins/oas3/components/request-body.jsx +++ b/src/core/plugins/oas3/components/request-body.jsx @@ -10,6 +10,7 @@ const RequestBody = ({ getComponent, getConfigs, specSelectors, + fn, contentType, isExecute, specPath, @@ -85,6 +86,7 @@ const RequestBody = ({ {isExecute ? { return memoizedCreateXMLExample(schema, config) } - return JSON.stringify(memoizedSampleFromSchema(schema, config), null, 2) + const res = memoizedSampleFromSchema(schema, config) + + return typeof res === "object" ? JSON.stringify(res, null, 2) : res } export const parseSearch = () => { diff --git a/test/.eslintrc b/test/.eslintrc index 3067bb51..52cbdcef 100644 --- a/test/.eslintrc +++ b/test/.eslintrc @@ -3,3 +3,4 @@ env: rules: "react/prop-types": 1 # bah humbug "no-unused-vars": 1 # unused vars in tests can be useful for indicating a full signature + "no-global-assign": 1 diff --git a/test/core/utils.js b/test/core/utils.js index b180b09a..8383c0fe 100644 --- a/test/core/utils.js +++ b/test/core/utils.js @@ -24,7 +24,8 @@ import { getCommonExtensions, sanitizeUrl, extractFileNameFromContentDispositionHeader, - deeplyStripKey + deeplyStripKey, + getSampleSchema } from "core/utils" import win from "core/window" @@ -1186,5 +1187,30 @@ describe("utils", function() { expect(sanitizeUrl({})).toEqual("") }) }) + describe("getSampleSchema", function() { + const oriDate = Date + before(function() { + Date = function () { + this.toISOString = function () { + return "2018-07-07T07:07:05.189Z" + } + } + }) + + after(function() { + Date = oriDate + }) + + it("should not unnecessarily stringify non-object values", function() { + // Given + const res = getSampleSchema({ + type: "string", + format: "date-time" + }) + + // Then + expect(res).toEqual(new Date().toISOString()) + }) + }) })