Skip to content

Commit 8f65483

Browse files
authored
fix: handle urlencoded array data correctly + don't stringify non-object sample values (#4704)
* fix: handle urlencoded array data correctly * fix: don't stringify non-object sample values * fix linter
1 parent 875caab commit 8f65483

File tree

5 files changed

+41
-5
lines changed

5 files changed

+41
-5
lines changed

src/core/json-schema-components.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ export class JsonSchema_array extends PureComponent {
111111

112112
constructor(props, context) {
113113
super(props, context)
114-
this.state = {value: props.value}
114+
this.state = { value: valueOrEmptyList(props.value)}
115115
}
116116

117117
componentWillReceiveProps(props) {
@@ -135,7 +135,7 @@ export class JsonSchema_array extends PureComponent {
135135

136136
addItem = () => {
137137
this.setState(state => {
138-
state.value = state.value || List()
138+
state.value = valueOrEmptyList(state.value)
139139
return {
140140
value: state.value.push("")
141141
}
@@ -174,7 +174,7 @@ export class JsonSchema_array extends PureComponent {
174174

175175
return (
176176
<div>
177-
{ !value || value.count() < 1 ? null :
177+
{ !value || !value.count || value.count() < 1 ? null :
178178
value.map( (item,i) => {
179179
let schema = Object.assign({}, itemSchema)
180180
if ( errors.length ) {
@@ -264,3 +264,7 @@ export class JsonSchema_object extends PureComponent {
264264

265265
}
266266
}
267+
268+
function valueOrEmptyList(value) {
269+
return List.isList(value) ? value : List()
270+
}

src/core/plugins/oas3/components/request-body.jsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const RequestBody = ({
1010
getComponent,
1111
getConfigs,
1212
specSelectors,
13+
fn,
1314
contentType,
1415
isExecute,
1516
specPath,
@@ -85,6 +86,7 @@ const RequestBody = ({
8586
<td className="col parameters-col_description">
8687
{isExecute ?
8788
<JsonSchemaForm
89+
fn={fn}
8890
dispatchInitialValue={!isFile}
8991
schema={prop}
9092
getComponent={getComponent}
@@ -132,6 +134,7 @@ RequestBody.propTypes = {
132134
requestBodyValue: ImPropTypes.orderedMap.isRequired,
133135
getComponent: PropTypes.func.isRequired,
134136
getConfigs: PropTypes.func.isRequired,
137+
fn: PropTypes.object.isRequired,
135138
specSelectors: PropTypes.object.isRequired,
136139
contentType: PropTypes.string,
137140
isExecute: PropTypes.bool.isRequired,

src/core/utils.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,9 @@ export const getSampleSchema = (schema, contentType="", config={}) => {
628628
return memoizedCreateXMLExample(schema, config)
629629
}
630630

631-
return JSON.stringify(memoizedSampleFromSchema(schema, config), null, 2)
631+
const res = memoizedSampleFromSchema(schema, config)
632+
633+
return typeof res === "object" ? JSON.stringify(res, null, 2) : res
632634
}
633635

634636
export const parseSearch = () => {

test/.eslintrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ env:
33
rules:
44
"react/prop-types": 1 # bah humbug
55
"no-unused-vars": 1 # unused vars in tests can be useful for indicating a full signature
6+
"no-global-assign": 1

test/core/utils.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ import {
2424
getCommonExtensions,
2525
sanitizeUrl,
2626
extractFileNameFromContentDispositionHeader,
27-
deeplyStripKey
27+
deeplyStripKey,
28+
getSampleSchema
2829
} from "core/utils"
2930
import win from "core/window"
3031

@@ -1186,5 +1187,30 @@ describe("utils", function() {
11861187
expect(sanitizeUrl({})).toEqual("")
11871188
})
11881189
})
1190+
describe("getSampleSchema", function() {
1191+
const oriDate = Date
11891192

1193+
before(function() {
1194+
Date = function () {
1195+
this.toISOString = function () {
1196+
return "2018-07-07T07:07:05.189Z"
1197+
}
1198+
}
1199+
})
1200+
1201+
after(function() {
1202+
Date = oriDate
1203+
})
1204+
1205+
it("should not unnecessarily stringify non-object values", function() {
1206+
// Given
1207+
const res = getSampleSchema({
1208+
type: "string",
1209+
format: "date-time"
1210+
})
1211+
1212+
// Then
1213+
expect(res).toEqual(new Date().toISOString())
1214+
})
1215+
})
11901216
})

0 commit comments

Comments
 (0)