@@ -33,6 +33,7 @@ private function docsToOpenApi(array $docs)
33
33
{
34
34
$ this ->openApi ['paths ' ] = [];
35
35
foreach ($ docs as $ doc ) {
36
+ $ requestHasFile = false ;
36
37
$ httpMethod = strtolower ($ doc ['httpMethod ' ]);
37
38
$ isGet = $ httpMethod == 'get ' ;
38
39
$ isPost = $ httpMethod == 'post ' ;
@@ -44,26 +45,45 @@ private function docsToOpenApi(array $docs)
44
45
45
46
$ this ->openApi ['paths ' ][$ doc ['uri ' ]][$ httpMethod ]['responses ' ] = config ('request-docs.open_api.responses ' , []);
46
47
47
- if ($ isGet ) {
48
+ foreach ($ doc ['rules ' ] as $ attribute => $ rules ) {
49
+ foreach ($ rules as $ rule ) {
50
+ if ($ isPost || $ isPut || $ isDelete ) {
51
+ $ requestHasFile = $ this ->attributeIsFile ($ rule );
52
+
53
+ if ($ requestHasFile ) {
54
+ break 2 ;
55
+ }
56
+ }
57
+ }
58
+ }
59
+
60
+ $ contentType = $ requestHasFile ? 'multipart/form-data ' : 'application/json ' ;
61
+
62
+ if ($ isGet ) {
48
63
$ this ->openApi ['paths ' ][$ doc ['uri ' ]][$ httpMethod ]['parameters ' ] = [];
49
64
}
50
65
if ($ isPost || $ isPut || $ isDelete ) {
51
- $ this ->openApi ['paths ' ][$ doc ['uri ' ]][$ httpMethod ]['requestBody ' ] = $ this ->makeRequestBodyItem ();
66
+ $ this ->openApi ['paths ' ][$ doc ['uri ' ]][$ httpMethod ]['requestBody ' ] = $ this ->makeRequestBodyItem ($ contentType );
52
67
}
68
+
53
69
foreach ($ doc ['rules ' ] as $ attribute => $ rules ) {
54
70
foreach ($ rules as $ rule ) {
55
71
if ($ isGet ) {
56
72
$ parameter = $ this ->makeQueryParameterItem ($ attribute , $ rule );
57
73
$ this ->openApi ['paths ' ][$ doc ['uri ' ]][$ httpMethod ]['parameters ' ][] = $ parameter ;
58
74
}
59
75
if ($ isPost || $ isPut || $ isDelete ) {
60
- $ this ->openApi ['paths ' ][$ doc ['uri ' ]][$ httpMethod ]['requestBody ' ]['content ' ][' application/json ' ]['schema ' ]['properties ' ][$ attribute ] = $ this ->makeRequestBodyContentPropertyItem ($ rule );
76
+ $ this ->openApi ['paths ' ][$ doc ['uri ' ]][$ httpMethod ]['requestBody ' ]['content ' ][$ contentType ]['schema ' ]['properties ' ][$ attribute ] = $ this ->makeRequestBodyContentPropertyItem ($ rule );
61
77
}
62
78
}
63
79
}
64
80
}
65
81
}
66
82
83
+ protected function attributeIsFile (string $ rule )
84
+ {
85
+ return str_contains ($ rule , 'file ' ) || str_contains ($ rule , 'image ' );
86
+ }
67
87
68
88
protected function makeQueryParameterItem (string $ attribute , string $ rule ): array
69
89
{
@@ -80,12 +100,12 @@ protected function makeQueryParameterItem(string $attribute, string $rule): arra
80
100
return $ parameter ;
81
101
}
82
102
83
- protected function makeRequestBodyItem (): array
103
+ protected function makeRequestBodyItem (string $ contentType ): array
84
104
{
85
105
$ requestBody = [
86
106
'description ' => "Request body " ,
87
107
'content ' => [
88
- ' application/json ' => [
108
+ $ contentType => [
89
109
'schema ' => [
90
110
'type ' => 'object ' ,
91
111
'properties ' => [],
@@ -98,22 +118,29 @@ protected function makeRequestBodyItem(): array
98
118
99
119
protected function makeRequestBodyContentPropertyItem (string $ rule ): array
100
120
{
121
+ $ type = $ this ->getAttributeType ($ rule );
122
+
101
123
return [
102
- 'type ' => $ this ->getAttributeType ($ rule ),
124
+ 'type ' => $ type ,
125
+ 'nullable ' => str_contains ($ rule , 'nullable ' ),
126
+ 'format ' => $ this ->attributeIsFile ($ rule ) ? 'binary ' : $ type ,
103
127
];
104
128
}
105
129
106
130
107
131
protected function getAttributeType (string $ rule ): string
108
132
{
109
- if (str_contains ($ rule , 'string ' )) {
133
+ if (str_contains ($ rule , 'string ' ) || $ this -> attributeIsFile ( $ rule ) ) {
110
134
return 'string ' ;
111
135
}
112
136
if (str_contains ($ rule , 'array ' )) {
113
137
return 'array ' ;
114
138
}
115
139
if (str_contains ($ rule , 'integer ' )) {
116
140
return 'integer ' ;
141
+ }
142
+ if (str_contains ($ rule , 'boolean ' )) {
143
+ return 'boolean ' ;
117
144
}
118
145
return "object " ;
119
146
}
0 commit comments