-
Notifications
You must be signed in to change notification settings - Fork 9.1k
/
Copy pathparameter-row.jsx
430 lines (363 loc) · 15.2 KB
/
parameter-row.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
import React, { Component } from "react"
import { Map, List, fromJS } from "immutable"
import PropTypes from "prop-types"
import ImPropTypes from "react-immutable-proptypes"
import win from "core/window"
import { getExtensions, getCommonExtensions, numberToString, stringify, isEmptyValue, immutableToJS } from "core/utils"
import getParameterSchema from "core/utils/get-parameter-schema.js"
export default class ParameterRow extends Component {
static propTypes = {
onChange: PropTypes.func.isRequired,
param: PropTypes.object.isRequired,
rawParam: PropTypes.object.isRequired,
getComponent: PropTypes.func.isRequired,
fn: PropTypes.object.isRequired,
isExecute: PropTypes.bool,
onChangeConsumes: PropTypes.func.isRequired,
specSelectors: PropTypes.object.isRequired,
specActions: PropTypes.object.isRequired,
pathMethod: PropTypes.array.isRequired,
getConfigs: PropTypes.func.isRequired,
specPath: ImPropTypes.list.isRequired,
oas3Actions: PropTypes.object.isRequired,
oas3Selectors: PropTypes.object.isRequired,
}
constructor(props, context) {
super(props, context)
this.setDefaultValue()
}
UNSAFE_componentWillReceiveProps(props) {
let { specSelectors, pathMethod, rawParam } = props
let isOAS3 = specSelectors.isOAS3()
let parameterWithMeta = specSelectors.parameterWithMetaByIdentity(pathMethod, rawParam) || new Map()
// fallback, if the meta lookup fails
parameterWithMeta = parameterWithMeta.isEmpty() ? rawParam : parameterWithMeta
let enumValue
if(isOAS3) {
let { schema } = getParameterSchema(parameterWithMeta, { isOAS3 })
enumValue = schema ? schema.get("enum") : undefined
} else {
enumValue = parameterWithMeta ? parameterWithMeta.get("enum") : undefined
}
let paramValue = parameterWithMeta ? parameterWithMeta.get("value") : undefined
let value
if ( paramValue !== undefined ) {
value = paramValue
} else if ( rawParam.get("required") && enumValue && enumValue.size ) {
value = enumValue.first()
}
if ( value !== undefined && value !== paramValue ) {
this.onChangeWrapper(numberToString(value))
}
// todo: could check if schema here; if not, do not call. impact?
this.setDefaultValue()
}
onChangeWrapper = (value, isXml = false) => {
let { onChange, rawParam } = this.props
let valueForUpstream
// Coerce empty strings and empty Immutable objects to null
if(value === "" || (value && value.size === 0)) {
valueForUpstream = null
} else {
valueForUpstream = value
}
return onChange(rawParam, valueForUpstream, isXml)
}
_onExampleSelect = (key, /* { isSyntheticChange } = {} */) => {
this.props.oas3Actions.setActiveExamplesMember({
name: key,
pathMethod: this.props.pathMethod,
contextType: "parameters",
contextName: this.getParamKey()
})
}
onChangeIncludeEmpty = (newValue) => {
let { specActions, param, pathMethod } = this.props
const paramName = param.get("name")
const paramIn = param.get("in")
return specActions.updateEmptyParamInclusion(pathMethod, paramName, paramIn, newValue)
}
setDefaultValue = () => {
let { specSelectors, pathMethod, rawParam, oas3Selectors, fn } = this.props
const paramWithMeta = specSelectors.parameterWithMetaByIdentity(pathMethod, rawParam) || Map()
let { schema } = getParameterSchema(paramWithMeta, { isOAS3: specSelectors.isOAS3() })
const parameterMediaType = paramWithMeta
.get("content", Map())
.keySeq()
.first()
// getSampleSchema could return null
const generatedSampleValue = schema ? fn.getSampleSchema(schema.toJS(), parameterMediaType, {
includeWriteOnly: true
}) : null
if (!paramWithMeta || paramWithMeta.get("value") !== undefined) {
return
}
if( paramWithMeta.get("in") !== "body" ) {
let initialValue
//// Find an initial value
if (specSelectors.isSwagger2()) {
initialValue =
paramWithMeta.get("x-example") !== undefined
? paramWithMeta.get("x-example")
: paramWithMeta.getIn(["schema", "example"]) !== undefined
? paramWithMeta.getIn(["schema", "example"])
: (schema && schema.getIn(["default"]))
} else if (specSelectors.isOAS3()) {
schema = this.composeJsonSchema(schema)
const currentExampleKey = oas3Selectors.activeExamplesMember(...pathMethod, "parameters", this.getParamKey())
initialValue =
paramWithMeta.getIn(["examples", currentExampleKey, "value"]) !== undefined
? paramWithMeta.getIn(["examples", currentExampleKey, "value"])
: paramWithMeta.getIn(["content", parameterMediaType, "example"]) !== undefined
? paramWithMeta.getIn(["content", parameterMediaType, "example"])
: paramWithMeta.get("example") !== undefined
? paramWithMeta.get("example")
: (schema && schema.get("example")) !== undefined
? (schema && schema.get("example"))
: (schema && schema.get("default")) !== undefined
? (schema && schema.get("default"))
: paramWithMeta.get("default") // ensures support for `parameterMacro`
}
//// Process the initial value
if(initialValue !== undefined && !List.isList(initialValue)) {
// Stringify if it isn't a List
initialValue = stringify(initialValue)
}
//// Dispatch the initial value
const type = fn.getSchemaObjectTypeLabel(immutableToJS(schema?.get("type")))
const itemType = fn.getSchemaObjectTypeLabel(immutableToJS(schema?.getIn(["items", "type"])))
if(initialValue !== undefined) {
this.onChangeWrapper(initialValue)
} else if(
type === "object"
&& generatedSampleValue
&& !paramWithMeta.get("examples")
) {
// Object parameters get special treatment.. if the user doesn't set any
// default or example values, we'll provide initial values generated from
// the schema.
// However, if `examples` exist for the parameter, we won't do anything,
// so that the appropriate `examples` logic can take over.
this.onChangeWrapper(
List.isList(generatedSampleValue) ? (
generatedSampleValue
) : (
stringify(generatedSampleValue)
)
)
}
else if (
type === "array"
&& itemType === "object"
&& generatedSampleValue
&& !paramWithMeta.get("examples")
) {
this.onChangeWrapper(
List.isList(generatedSampleValue) ? (
generatedSampleValue
) : (
List(JSON.parse(generatedSampleValue))
)
)
}
}
}
getParamKey() {
const { param } = this.props
if(!param) return null
return `${param.get("name")}-${param.get("in")}`
}
composeJsonSchema(schema) {
const { fn } = this.props
const oneOf = schema.get("oneOf")?.get(0)?.toJS()
const anyOf = schema.get("anyOf")?.get(0)?.toJS()
return fromJS(fn.mergeJsonSchema(schema.toJS(), oneOf ?? anyOf ?? {}))
}
render() {
let {param, rawParam, getComponent, getConfigs, isExecute, fn, onChangeConsumes, specSelectors, pathMethod, specPath, oas3Selectors} = this.props
let isOAS3 = specSelectors.isOAS3()
const { showExtensions, showCommonExtensions } = getConfigs()
if(!param) {
param = rawParam
}
if(!rawParam) return null
// const onChangeWrapper = (value) => onChange(param, value)
const JsonSchemaForm = getComponent("JsonSchemaForm")
const ParamBody = getComponent("ParamBody")
let inType = param.get("in")
let bodyParam = inType !== "body" ? null
: <ParamBody getComponent={getComponent}
getConfigs={ getConfigs }
fn={fn}
param={param}
consumes={ specSelectors.consumesOptionsFor(pathMethod) }
consumesValue={ specSelectors.contentTypeValues(pathMethod).get("requestContentType") }
onChange={this.onChangeWrapper}
onChangeConsumes={onChangeConsumes}
isExecute={ isExecute }
specSelectors={ specSelectors }
pathMethod={ pathMethod }
/>
const ModelExample = getComponent("modelExample")
const Markdown = getComponent("Markdown", true)
const ParameterExt = getComponent("ParameterExt")
const ParameterIncludeEmpty = getComponent("ParameterIncludeEmpty")
const ExamplesSelectValueRetainer = getComponent("ExamplesSelectValueRetainer")
const Example = getComponent("Example")
let { schema } = getParameterSchema(param, { isOAS3 })
let paramWithMeta = specSelectors.parameterWithMetaByIdentity(pathMethod, rawParam) || Map()
if (isOAS3) {
schema = this.composeJsonSchema(schema)
}
let format = schema ? schema.get("format") : null
let isFormData = inType === "formData"
let isFormDataSupported = "FormData" in win
let required = param.get("required")
const typeLabel = fn.getSchemaObjectType(immutableToJS(schema))
const type = fn.getSchemaObjectTypeLabel(immutableToJS(schema?.get("type")))
const itemType = fn.getSchemaObjectTypeLabel(immutableToJS(schema?.getIn(["items", "type"])))
const isObject = !bodyParam && type === "object"
const isArrayOfObjects = !bodyParam && itemType === "object"
let value = paramWithMeta ? paramWithMeta.get("value") : ""
let commonExt = showCommonExtensions ? getCommonExtensions(schema) : null
let extensions = showExtensions ? getExtensions(param) : null
let paramItems // undefined
let paramEnum // undefined
let paramDefaultValue // undefined
let paramExample // undefined
let isDisplayParamEnum = false
if ( param !== undefined && schema ) {
paramItems = schema.get("items")
}
if (paramItems !== undefined) {
paramEnum = paramItems.get("enum")
paramDefaultValue = paramItems.get("default")
} else if (schema) {
paramEnum = schema.get("enum")
}
if ( paramEnum && paramEnum.size && paramEnum.size > 0) {
isDisplayParamEnum = true
}
// Default and Example Value for readonly doc
if ( param !== undefined ) {
if (schema) {
paramDefaultValue = schema.get("default")
}
if (paramDefaultValue === undefined) {
paramDefaultValue = param.get("default")
}
paramExample = param.get("example")
if (paramExample === undefined) {
paramExample = param.get("x-example")
}
}
const jsonSchemaForm = bodyParam ? null
: <JsonSchemaForm fn={fn}
getComponent={getComponent}
value={ value }
required={ required }
disabled={!isExecute}
description={param.get("name")}
onChange={ this.onChangeWrapper }
errors={ paramWithMeta.get("errors") }
schema={ schema }
/>
return (
<tr data-param-name={param.get("name")} data-param-in={param.get("in")}>
<td className="parameters-col_name">
<div className={required ? "parameter__name required" : "parameter__name"}>
{ param.get("name") }
{ !required ? null : <span> *</span> }
</div>
<div className="parameter__type">
{ typeLabel }
{ format && <span className="prop-format">(${format})</span>}
</div>
<div className="parameter__deprecated">
{ isOAS3 && param.get("deprecated") ? "deprecated": null }
</div>
<div className="parameter__in">({ param.get("in") })</div>
</td>
<td className="parameters-col_description">
{ param.get("description") ? <Markdown source={ param.get("description") }/> : null }
{ (bodyParam || !isExecute) && isDisplayParamEnum ?
<Markdown className="parameter__enum" source={
"<i>Available values</i> : " + paramEnum.map(function(item) {
return item
}).toArray().map(String).join(", ")}/>
: null
}
{ (bodyParam || !isExecute) && paramDefaultValue !== undefined ?
<Markdown className="parameter__default" source={"<i>Default value</i> : " + paramDefaultValue}/>
: null
}
{ (bodyParam || !isExecute) && paramExample !== undefined ?
<Markdown source={"<i>Example</i> : " + paramExample}/>
: null
}
{(isFormData && !isFormDataSupported) && <div>Error: your browser does not support FormData</div>}
{
isOAS3 && param.get("examples") ? (
<section className="parameter-controls">
<ExamplesSelectValueRetainer
examples={param.get("examples")}
onSelect={this._onExampleSelect}
updateValue={this.onChangeWrapper}
getComponent={getComponent}
defaultToFirstExample={true}
currentKey={oas3Selectors.activeExamplesMember(...pathMethod, "parameters", this.getParamKey())}
currentUserInputValue={value}
/>
</section>
) : null
}
{ (isObject || isArrayOfObjects) ? (
<ModelExample
getComponent={getComponent}
specPath={specPath.push("schema")}
getConfigs={getConfigs}
isExecute={isExecute}
specSelectors={specSelectors}
schema={schema}
example={jsonSchemaForm}
/>
) : jsonSchemaForm
}
{
bodyParam && schema ? <ModelExample getComponent={ getComponent }
specPath={specPath.push("schema")}
getConfigs={ getConfigs }
isExecute={ isExecute }
specSelectors={ specSelectors }
schema={ schema }
example={ bodyParam }
includeWriteOnly={ true }/>
: null
}
{
!bodyParam && isExecute && param.get("allowEmptyValue") ?
<ParameterIncludeEmpty
onChange={this.onChangeIncludeEmpty}
isIncluded={specSelectors.parameterInclusionSettingFor(pathMethod, param.get("name"), param.get("in"))}
isDisabled={!isEmptyValue(value)} />
: null
}
{
isOAS3 && param.get("examples") ? (
<Example
example={param.getIn([
"examples",
oas3Selectors.activeExamplesMember(...pathMethod, "parameters", this.getParamKey())
])}
getComponent={getComponent}
getConfigs={getConfigs}
/>
) : null
}
{ !showCommonExtensions || !commonExt.size ? null : commonExt.entrySeq().map(([key, v]) => <ParameterExt key={`${key}-${v}`} xKey={key} xVal={v} /> )}
{ !showExtensions || !extensions.size ? null : extensions.entrySeq().map(([key, v]) => <ParameterExt key={`${key}-${v}`} xKey={key} xVal={v} /> )}
</td>
</tr>
)
}
}