Skip to content

Commit c9eb45f

Browse files
committed
add format properties in request body parameters
1 parent 11231db commit c9eb45f

File tree

4 files changed

+127
-6
lines changed

4 files changed

+127
-6
lines changed

src/Generator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ private function retrieveFormRules(?ReflectionMethod $actionInstance): array {
540540
$parameters = $actionInstance->getParameters();
541541

542542
foreach ($parameters as $parameter) {
543-
$class = $parameter->getClass();
543+
$class = $parameter->getType();
544544
if (!$class) {
545545
continue;
546546
}

src/Parameters/BodyParametersGenerator.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,19 @@ public function getParameters(): array {
5656
}
5757

5858
Arr::set($schema, 'properties', $properties);
59+
60+
$mediaType = 'application/json'; // or "application/x-www-form-urlencoded"
61+
foreach($properties as $prop) {
62+
if (isset($prop['format']) && $prop['format'] == 'binary') {
63+
$mediaType = 'multipart/form-data';
64+
}
65+
}
66+
67+
5968
return [
60-
'content' => [
61-
'application/json' => [
62-
'schema' => $schema
69+
'content' => [
70+
$mediaType => [
71+
'schema' => $schema
6372
]
6473
]
6574
];
@@ -99,6 +108,10 @@ protected function addToProperties(array & $properties, array $nameTokens, array
99108
if (!Arr::has($properties, $name)) {
100109
$propertyObject = $this->createNewPropertyObject($type, $rules);
101110
Arr::set($properties, $name, $propertyObject);
111+
$extra = $this->getParameterExtra($type, $rules);
112+
foreach($extra as $key => $value) {
113+
Arr::set($properties, $name . '.' . $key, $value);
114+
}
102115
} else {
103116
Arr::set($properties, $name . '.type', $type);
104117
}

src/Parameters/Traits/GeneratesFromRules.php

Lines changed: 88 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,99 @@ protected function getParameterType(array $parameterRules): string {
3434
return 'boolean';
3535
} elseif (in_array('array', $parameterRules)) {
3636
return 'array';
37-
} elseif (in_array('date', $parameterRules)) {
38-
return 'date';
3937
} else {
4038
return 'string';
4139
}
4240
}
4341

42+
/**
43+
* Get parameter format
44+
* @param array $parameterRules
45+
* @return string
46+
*/
47+
protected function getParameterExtra(string $type, array $parameterRules): array {
48+
49+
$extra = [];
50+
51+
if (in_array('nullable', $parameterRules)) {
52+
$extra['nullable'] = true;
53+
}
54+
55+
if (in_array($type, ['numeric', 'integer'])) {
56+
foreach ($parameterRules as $rule) {
57+
if (Str::startsWith($rule, 'min')) {
58+
[$_, $value] = explode(':', $rule);
59+
$extra['minimum'] = intval(trim($value));
60+
}
61+
62+
if (Str::startsWith($rule, 'max')) {
63+
[$_, $value] = explode(':', $rule);
64+
$extra['maximum'] = intval(trim($value));
65+
}
66+
67+
if (Str::startsWith($rule, 'multiple_of')) {
68+
[$_, $value] = explode(':', $rule);
69+
$extra['multipleOf'] = intval(trim($value));
70+
}
71+
}
72+
}
73+
74+
if ($type == 'string') {
75+
foreach ($parameterRules as $rule) {
76+
if (Str::startsWith($rule, 'min')) {
77+
[$_, $value] = explode(':', $rule);
78+
$extra['minLength'] = intval(trim($value));
79+
}
80+
81+
if (Str::startsWith($rule, 'max')) {
82+
[$_, $value] = explode(':', $rule);
83+
$extra['maxLength'] = intval(trim($value));
84+
}
85+
86+
$formatMap = [
87+
'byte' => ['date'],
88+
'binary' => ['file', 'image', 'mimetypes', 'mimes'],
89+
'date' => ['date'],
90+
'password' => ['password'],
91+
92+
// custom, these are not OpenAPI built-in
93+
'email' => ['email'],
94+
'uuid' => ['uuid'],
95+
'uri' => ['url'],
96+
'ip' => ['ip'],
97+
'ipv4' => ['ipv4'],
98+
'ipv6' => ['ipv6'],
99+
'json' => ['json']
100+
];
101+
102+
foreach($formatMap as $format => $formatRules) {
103+
if (at_least_one_in_array($formatRules, $parameterRules)) {
104+
$extra['format'] = $format;
105+
}
106+
}
107+
108+
if (!isset($extra['format'])) {
109+
if (
110+
Str::startsWith($rule, 'after') ||
111+
Str::startsWith($rule, 'after_or_equal') ||
112+
Str::startsWith($rule, 'before') ||
113+
Str::startsWith($rule, 'before_or_equal')
114+
) {
115+
$extra['format'] = 'date';
116+
}
117+
}
118+
119+
if (Str::startsWith($rule, 'regex')) {
120+
[$_, $value] = explode(':', $rule);
121+
$extra['pattern'] = trim($value);
122+
}
123+
}
124+
125+
}
126+
127+
return $extra;
128+
}
129+
44130
/**
45131
* Check whether parameter is required
46132
* @param array $parameterRules

src/helpers.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,28 @@
22

33
use Illuminate\Support\Facades\File;
44

5+
if (!function_exists('at_least_one_in_array')) {
6+
function at_least_one_in_array(array $items, array $haystack, bool $strict = false): bool {
7+
foreach($items as $item) {
8+
if (in_array($item, $haystack, $strict)) {
9+
return true;
10+
}
11+
}
12+
return false;
13+
}
14+
}
15+
16+
if (!function_exists('all_in_array')) {
17+
function all_in_array(array $items, array $haystack, bool $strict = false): bool {
18+
foreach($items as $item) {
19+
if (!in_array($item, $haystack, $strict)) {
20+
return false;
21+
}
22+
}
23+
return true;
24+
}
25+
}
26+
527
if (!function_exists('strip_optional_char')) {
628
function strip_optional_char(string $uri): string {
729
return str_replace('?', '', $uri);

0 commit comments

Comments
 (0)