@@ -191,19 +191,43 @@ export function getQueryParams(
191191 return queries
192192}
193193
194+ function getNamedParamSchema (
195+ param : ParamMetadataArgs
196+ ) : oa . SchemaObject | oa . ReferenceObject {
197+ const { type } = param
198+ if ( type === 'file' ) {
199+ return { type : 'string' , format : 'binary' }
200+ }
201+ if ( type === 'files' ) {
202+ return {
203+ type : 'array' ,
204+ items : {
205+ type : 'string' ,
206+ format : 'binary' ,
207+ } ,
208+ }
209+ }
210+ return getParamSchema ( param )
211+ }
212+
194213/**
195214 * Return OpenAPI requestBody of given route, if it has one.
196215 */
197216export function getRequestBody ( route : IRoute ) : oa . RequestBodyObject | void {
198217 const bodyParamMetas = route . params . filter ( ( d ) => d . type === 'body-param' )
199- const bodyParamsSchema : oa . SchemaObject | null =
200- bodyParamMetas . length > 0
201- ? bodyParamMetas . reduce (
218+ const uploadFileMetas = route . params . filter ( ( d ) =>
219+ [ 'file' , 'files' ] . includes ( d . type )
220+ )
221+ const namedParamMetas = [ ...bodyParamMetas , ...uploadFileMetas ]
222+
223+ const namedParamsSchema : oa . SchemaObject | null =
224+ namedParamMetas . length > 0
225+ ? namedParamMetas . reduce (
202226 ( acc : oa . SchemaObject , d ) => ( {
203227 ...acc ,
204228 properties : {
205229 ...acc . properties ,
206- [ d . name ! ] : getParamSchema ( d ) ,
230+ [ d . name ! ] : getNamedParamSchema ( d ) ,
207231 } ,
208232 required : isRequired ( d , route )
209233 ? [ ...( acc . required || [ ] ) , d . name ! ]
@@ -213,27 +237,29 @@ export function getRequestBody(route: IRoute): oa.RequestBodyObject | void {
213237 )
214238 : null
215239
216- const bodyMeta = route . params . find ( ( d ) => d . type === 'body' )
240+ const contentType =
241+ uploadFileMetas . length > 0 ? 'multipart/form-data' : 'application/json'
217242
243+ const bodyMeta = route . params . find ( ( d ) => d . type === 'body' )
218244 if ( bodyMeta ) {
219245 const bodySchema = getParamSchema ( bodyMeta )
220246 const { $ref } =
221247 'items' in bodySchema && bodySchema . items ? bodySchema . items : bodySchema
222248
223249 return {
224250 content : {
225- 'application/json' : {
226- schema : bodyParamsSchema
227- ? { allOf : [ bodySchema , bodyParamsSchema ] }
251+ [ contentType ] : {
252+ schema : namedParamsSchema
253+ ? { allOf : [ bodySchema , namedParamsSchema ] }
228254 : bodySchema ,
229255 } ,
230256 } ,
231257 description : ( $ref || '' ) . split ( '/' ) . pop ( ) ,
232258 required : isRequired ( bodyMeta , route ) ,
233259 }
234- } else if ( bodyParamsSchema ) {
260+ } else if ( namedParamsSchema ) {
235261 return {
236- content : { 'application/json' : { schema : bodyParamsSchema } } ,
262+ content : { [ contentType ] : { schema : namedParamsSchema } } ,
237263 }
238264 }
239265}
0 commit comments