Skip to content

Commit 67c8b20

Browse files
Merge pull request #270 from hafijul233/master
Open API 3 Path Parameter & Delete Standard Rule Check Failed Issue Fixed
2 parents 1c96608 + 669a033 commit 67c8b20

File tree

4 files changed

+40
-8
lines changed

4 files changed

+40
-8
lines changed

config/request-docs.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@
6464
'license' => 'Apache 2.0',
6565
'license_url' => 'https://www.apache.org/licenses/LICENSE-2.0.html',
6666
'server_url' => env('APP_URL', 'http://localhost'),
67-
67+
//openapi 3.0.x doesn't support request body for delete operation
68+
//ref: https://github.com/OAI/OpenAPI-Specification/pull/2117
69+
'delete_with_body' => false,
70+
//exclude http methods that will be excluded from openapi export
71+
'exclude_http_methods' => [],
6872
// for now putting default responses for all. This can be changed later based on specific needs
6973
'responses' => [
7074
'200' => [

src/LaravelRequestDocs.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function getDocs(
4747
Request::METHOD_PATCH => $showPatch,
4848
Request::METHOD_DELETE => $showDelete,
4949
Request::METHOD_HEAD => $showHead,
50-
], fn(bool $shouldShow) => $shouldShow);
50+
], fn (bool $shouldShow) => $shouldShow);
5151

5252
/** @var string[] $methods */
5353
$methods = array_keys($filteredMethods);
@@ -402,7 +402,7 @@ private function customParamsDocComment(string $docComment): array
402402
$comments = $this->multiExplode([' ', '|'], $comment);
403403

404404
if (count($comments) > 0) {
405-
$params[$comments[0]] = array_values(array_filter($comments, fn($item) => $item !== $comments[0]));
405+
$params[$comments[0]] = array_values(array_filter($comments, fn ($item) => $item !== $comments[0]));
406406
}
407407
}
408408

src/LaravelRequestDocsServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function configurePackage(Package $package): void
2020
->hasConfigFile('request-docs')
2121
// ->hasAssets()
2222
->hasViews();
23-
// ->hasAssets();
23+
// ->hasAssets();
2424
// publish resources/dist/_astro to public/
2525
$this->publishes([
2626
__DIR__.'/../resources/dist/_astro' => public_path('request-docs/_astro'),

src/LaravelRequestDocsToOpenApi.php

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,18 @@ public function openApi(array $docs): LaravelRequestDocsToOpenApi
3333
private function docsToOpenApi(array $docs): void
3434
{
3535
$this->openApi['paths'] = [];
36+
$deleteWithBody = config('request-docs.open_api.delete_with_body', false);
37+
$excludeHttpMethods = array_map(fn ($item) => strtolower($item), config('request-docs.open_api.exclude_http_methods', []));
38+
3639
foreach ($docs as $doc) {
37-
$requestHasFile = false;
40+
3841
$httpMethod = strtolower($doc->getHttpMethod());
42+
43+
if (in_array($httpMethod, $excludeHttpMethods)) {
44+
continue;
45+
}
46+
47+
$requestHasFile = false;
3948
$isGet = $httpMethod == 'get';
4049
$isPost = $httpMethod == 'post';
4150
$isPut = $httpMethod == 'put';
@@ -46,7 +55,7 @@ private function docsToOpenApi(array $docs): void
4655
$this->openApi['paths'][$uriLeadingSlash][$httpMethod]['parameters'] = [];
4756

4857
foreach ($doc->getPathParameters() as $parameter => $rule) {
49-
$this->openApi['paths'][$uriLeadingSlash][$httpMethod]['parameters'][] = $this->makeQueryParameterItem($parameter, $rule);
58+
$this->openApi['paths'][$uriLeadingSlash][$httpMethod]['parameters'][] = $this->makePathParameterItem($parameter, $rule);
5059
}
5160

5261
$this->openApi['paths'][$uriLeadingSlash][$httpMethod]['responses'] = config('request-docs.open_api.responses', []);
@@ -65,7 +74,7 @@ private function docsToOpenApi(array $docs): void
6574

6675
$contentType = $requestHasFile ? 'multipart/form-data' : 'application/json';
6776

68-
if ($isPost || $isPut || $isDelete) {
77+
if ($isPost || $isPut || ($isDelete && $deleteWithBody)) {
6978
$this->openApi['paths'][$uriLeadingSlash][$httpMethod]['requestBody'] = $this->makeRequestBodyItem($contentType);
7079
}
7180

@@ -75,7 +84,7 @@ private function docsToOpenApi(array $docs): void
7584
$parameter = $this->makeQueryParameterItem($attribute, $rule);
7685
$this->openApi['paths'][$uriLeadingSlash][$httpMethod]['parameters'][] = $parameter;
7786
}
78-
if ($isPost || $isPut || $isDelete) {
87+
if ($isPost || $isPut || ($isDelete && $deleteWithBody)) {
7988
$this->openApi['paths'][$uriLeadingSlash][$httpMethod]['requestBody']['content'][$contentType]['schema']['properties'][$attribute] = $this->makeRequestBodyContentPropertyItem($rule);
8089
}
8190
}
@@ -106,6 +115,25 @@ protected function makeQueryParameterItem(string $attribute, $rule): array
106115
return $parameter;
107116
}
108117

118+
protected function makePathParameterItem(string $attribute, $rule): array
119+
{
120+
if (is_array($rule)) {
121+
$rule = implode('|', $rule);
122+
}
123+
124+
$parameter = [
125+
'name' => $attribute,
126+
'description' => $rule,
127+
'in' => 'path',
128+
'style' => 'simple',
129+
'required' => str_contains($rule, 'required'),
130+
'schema' => [
131+
'type' => $this->getAttributeType($rule),
132+
],
133+
];
134+
return $parameter;
135+
}
136+
109137
protected function makeRequestBodyItem(string $contentType): array
110138
{
111139
$requestBody = [

0 commit comments

Comments
 (0)