1
- import { OperationObject , ParameterObject } from "../types" ;
2
- import { comment } from "../utils" ;
1
+ import { OperationObject , ParameterObject , ReferenceObject } from "../types" ;
2
+ import { comment , transformRef } from "../utils" ;
3
3
import { transformParametersArray } from "./parameters" ;
4
4
import { transformResponsesObj } from "./responses" ;
5
5
import { transformSchemaObj } from "./schema" ;
@@ -18,20 +18,31 @@ export function transformOperationObj(
18
18
output += ` responses: {\n ${ transformResponsesObj ( operation . responses ) } \n }\n` ;
19
19
}
20
20
21
- if ( operation . requestBody && operation . requestBody . content ) {
22
- if ( operation . requestBody . description ) output += comment ( operation . requestBody . description ) ;
21
+ if ( operation . requestBody ) {
22
+ if ( isRef ( operation . requestBody ) ) {
23
+ output += ` requestBody: ${ transformRef ( operation . requestBody . $ref ) } ;\n` ;
24
+ } else {
25
+ const { description, content} = operation . requestBody
23
26
24
- if ( Object . keys ( operation . requestBody . content ) . length ) {
25
- output += ` requestBody: {\n content: {\n` ; // open requestBody
27
+ if ( description ) output += comment ( description ) ;
28
+
29
+ if ( content && Object . keys ( content ) . length ) {
30
+ output += ` requestBody: {\n content: {\n` ; // open requestBody
31
+
32
+ Object . entries ( content ) . forEach ( ( [ k , v ] ) => {
33
+ output += ` "${ k } ": ${ transformSchemaObj ( v . schema ) } ;\n` ;
34
+ } ) ;
35
+ output += ` }\n }\n` ; // close requestBody
36
+ } else {
37
+ output += ` requestBody: unknown;\n` ;
38
+ }
26
39
27
- Object . entries ( operation . requestBody . content ) . forEach ( ( [ k , v ] ) => {
28
- output += ` "${ k } ": ${ transformSchemaObj ( v . schema ) } ;\n` ;
29
- } ) ;
30
- output += ` }\n }\n` ; // close requestBody
31
- } else {
32
- output += ` requestBody: unknown;\n` ;
33
40
}
34
41
}
35
42
36
43
return output ;
37
44
}
45
+
46
+ export function isRef ( obj : any ) : obj is ReferenceObject {
47
+ return ! ! obj . $ref
48
+ }
0 commit comments