11import React from "react"
22import PropTypes from "prop-types"
33import ImPropTypes from "react-immutable-proptypes"
4- import { OrderedMap } from "immutable"
4+ import { getSampleSchema } from "core/utils"
5+ import Im , { Map , OrderedMap , List } from "immutable"
56
67const RequestBody = ( {
78 requestBody,
9+ requestBodyValue,
810 getComponent,
911 getConfigs,
1012 specSelectors,
@@ -13,6 +15,10 @@ const RequestBody = ({
1315 specPath,
1416 onChange
1517} ) => {
18+ const handleFile = ( e ) => {
19+ onChange ( e . target . files [ 0 ] )
20+ }
21+
1622 const Markdown = getComponent ( "Markdown" )
1723 const ModelExample = getComponent ( "modelExample" )
1824 const RequestBodyEditor = getComponent ( "RequestBodyEditor" )
@@ -23,10 +29,80 @@ const RequestBody = ({
2329
2430 const mediaTypeValue = requestBodyContent . get ( contentType )
2531
32+ const isObjectContent = mediaTypeValue . getIn ( [ "schema" , "type" ] ) === "object"
33+
2634 if ( ! mediaTypeValue ) {
2735 return null
2836 }
2937
38+ if ( contentType === "application/octet-stream" ) {
39+ const Input = getComponent ( "Input" )
40+
41+ if ( ! isExecute ) {
42+ return < i >
43+ Example values are not available for < code > application/octet-stream</ code > media types.
44+ </ i >
45+ }
46+
47+ return < Input type = { "file" } onChange = { handleFile } />
48+ }
49+
50+ if (
51+ isObjectContent &&
52+ ( contentType === "application/x-www-form-urlencoded"
53+ || contentType . indexOf ( "multipart/" ) === 0 ) )
54+ {
55+ const JsonSchemaForm = getComponent ( "JsonSchemaForm" )
56+ const HighlightCode = getComponent ( "highlightCode" )
57+ const bodyProperties = requestBody . getIn ( [ "content" , contentType , "schema" , "properties" ] , OrderedMap ( ) )
58+ requestBodyValue = Map . isMap ( requestBodyValue ) ? requestBodyValue : OrderedMap ( )
59+
60+ return < div className = "table-container" >
61+ < table >
62+ < tbody >
63+ {
64+ bodyProperties . map ( ( prop , key ) => {
65+ const required = prop . get ( "required" )
66+ const type = prop . get ( "type" )
67+ const format = prop . get ( "format" )
68+
69+ const isFile = type === "string" && ( format === "binary" || format === "base64" )
70+
71+ return < tr key = { key } className = "parameters" >
72+ < td className = "col parameters-col_name" >
73+ < div className = { required ? "parameter__name required" : "parameter__name" } >
74+ { key }
75+ { ! required ? null : < span style = { { color : "red" } } > *</ span > }
76+ </ div >
77+ < div className = "parameter__type" >
78+ { type }
79+ { format && < span className = "prop-format" > (${ format } )</ span > }
80+ </ div >
81+ < div className = "parameter__deprecated" >
82+ { prop . get ( "deprecated" ) ? "deprecated" : null }
83+ </ div >
84+ </ td >
85+ < td className = "col parameters-col_description" >
86+ { isExecute ?
87+ < JsonSchemaForm
88+ dispatchInitialValue = { ! isFile }
89+ schema = { prop }
90+ getComponent = { getComponent }
91+ value = { requestBodyValue . get ( key ) || getSampleSchema ( prop ) }
92+ onChange = { ( value ) => {
93+ onChange ( value , [ key ] )
94+ } }
95+ />
96+ : < HighlightCode className = "example" value = { getSampleSchema ( prop ) } /> }
97+ </ td >
98+ </ tr >
99+ } )
100+ }
101+ </ tbody >
102+ </ table >
103+ </ div >
104+ }
105+
30106 return < div >
31107 { requestBodyDescription &&
32108 < Markdown source = { requestBodyDescription } />
@@ -53,6 +129,7 @@ const RequestBody = ({
53129
54130RequestBody . propTypes = {
55131 requestBody : ImPropTypes . orderedMap . isRequired ,
132+ requestBodyValue : ImPropTypes . orderedMap . isRequired ,
56133 getComponent : PropTypes . func . isRequired ,
57134 getConfigs : PropTypes . func . isRequired ,
58135 specSelectors : PropTypes . object . isRequired ,
0 commit comments