Skip to content

Commit b12a31f

Browse files
Merge pull request #65 from rcerljenko/master
Added boolean and file attribute type support
2 parents 53014ca + 825f3b4 commit b12a31f

File tree

2 files changed

+48
-8
lines changed

2 files changed

+48
-8
lines changed

resources/views/index.blade.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,9 @@ class="inline-flex text-xs"
289289
@endif
290290
@if (str_contains($rule, 'boolean'))
291291
<span class="inline-flex items-center justify-center px-2 py-1 text-xs font-bold leading-none text-gray-800 bg-green-400 rounded">Boolean</span>
292+
@endif
293+
@if (str_contains($rule, 'file') || str_contains($rule, 'image'))
294+
<span class="inline-flex items-center justify-center px-2 py-1 text-xs font-bold leading-none text-gray-800 bg-green-400 rounded">File</span>
292295
@endif
293296
@endforeach
294297
</td>
@@ -310,7 +313,7 @@ class="inline-flex text-xs"
310313
<div class="font-mono">
311314
@foreach ($rules as $rule)
312315
@foreach (explode('|', $rule) as $r)
313-
@if (!in_array($r, ['required', 'integer', 'string', 'boolean', 'array', 'nullable', 'bail']))
316+
@if (!in_array($r, ['required', 'integer', 'string', 'boolean', 'array', 'nullable', 'bail', 'file', 'image']))
314317
{{$r}}
315318
@if (!$loop->last)
316319
<span class="text-gray-900 font-bold">|</span>
@@ -524,13 +527,17 @@ class="my-prism-editor"
524527
isArray: null,
525528
isDate: null,
526529
isIn: null,
530+
isBoolean: null,
531+
isFile: null,
527532
value: '',
528533
}
529534
rules.map(function(rule) {
530535
validations.isRequired = rule.match(/required/)
531536
validations.isDate = rule.match(/date/)
532537
validations.isArray = rule.match(/array/)
533538
validations.isString = rule.match(/string/)
539+
validations.isBoolean = rule.match(/boolean/)
540+
validations.isFile = rule.match(/file|image/)
534541
if (rule.match(/integer/)) {
535542
validations.isInteger = true
536543
}
@@ -557,6 +564,12 @@ class="my-prism-editor"
557564
if (validations.isDate) {
558565
validations.value = new Date(faker.date.between(new Date(), new Date())).toISOString().slice(0, 10)
559566
}
567+
if (validations.isBoolean) {
568+
validations.value = true
569+
}
570+
if (validations.isFile) {
571+
validations.value = {}
572+
}
560573
561574
return validations
562575
}

src/LaravelRequestDocsToOpenApi.php

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ private function docsToOpenApi(array $docs)
3333
{
3434
$this->openApi['paths'] = [];
3535
foreach ($docs as $doc) {
36+
$requestHasFile = false;
3637
$httpMethod = strtolower($doc['httpMethod']);
3738
$isGet = $httpMethod == 'get';
3839
$isPost = $httpMethod == 'post';
@@ -44,26 +45,45 @@ private function docsToOpenApi(array $docs)
4445

4546
$this->openApi['paths'][$doc['uri']][$httpMethod]['responses'] = config('request-docs.open_api.responses', []);
4647

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) {
4863
$this->openApi['paths'][$doc['uri']][$httpMethod]['parameters'] = [];
4964
}
5065
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);
5267
}
68+
5369
foreach ($doc['rules'] as $attribute => $rules) {
5470
foreach ($rules as $rule) {
5571
if ($isGet) {
5672
$parameter = $this->makeQueryParameterItem($attribute, $rule);
5773
$this->openApi['paths'][$doc['uri']][$httpMethod]['parameters'][] = $parameter;
5874
}
5975
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);
6177
}
6278
}
6379
}
6480
}
6581
}
6682

83+
protected function attributeIsFile(string $rule)
84+
{
85+
return str_contains($rule, 'file') || str_contains($rule, 'image');
86+
}
6787

6888
protected function makeQueryParameterItem(string $attribute, string $rule): array
6989
{
@@ -80,12 +100,12 @@ protected function makeQueryParameterItem(string $attribute, string $rule): arra
80100
return $parameter;
81101
}
82102

83-
protected function makeRequestBodyItem(): array
103+
protected function makeRequestBodyItem(string $contentType): array
84104
{
85105
$requestBody = [
86106
'description' => "Request body",
87107
'content' => [
88-
'application/json' => [
108+
$contentType => [
89109
'schema' => [
90110
'type' => 'object',
91111
'properties' => [],
@@ -98,22 +118,29 @@ protected function makeRequestBodyItem(): array
98118

99119
protected function makeRequestBodyContentPropertyItem(string $rule): array
100120
{
121+
$type = $this->getAttributeType($rule);
122+
101123
return [
102-
'type' => $this->getAttributeType($rule),
124+
'type' => $type,
125+
'nullable' => str_contains($rule, 'nullable'),
126+
'format' => $this->attributeIsFile($rule) ? 'binary' : $type,
103127
];
104128
}
105129

106130

107131
protected function getAttributeType(string $rule): string
108132
{
109-
if (str_contains($rule, 'string')) {
133+
if (str_contains($rule, 'string') || $this->attributeIsFile($rule)) {
110134
return 'string';
111135
}
112136
if (str_contains($rule, 'array')) {
113137
return 'array';
114138
}
115139
if (str_contains($rule, 'integer')) {
116140
return 'integer';
141+
}
142+
if (str_contains($rule, 'boolean')) {
143+
return 'boolean';
117144
}
118145
return "object";
119146
}

0 commit comments

Comments
 (0)