Skip to content

Commit 8e8aa8e

Browse files
committed
Generate query parameters information
1 parent 7ef8984 commit 8e8aa8e

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

src/Doc.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,14 @@ public function setResponses(array $responses): void
325325
$this->responses = $responses;
326326
}
327327

328+
/**
329+
* @return array<string, string>
330+
*/
331+
public function getPathParameters(): array
332+
{
333+
return $this->pathParameters;
334+
}
335+
328336
public function clone(): Doc
329337
{
330338
return clone $this;

src/LaravelRequestDocsToOpenApi.php

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,22 @@ private function docsToOpenApi(array $docs): void
3434
{
3535
$this->openApi['paths'] = [];
3636
foreach ($docs as $doc) {
37-
$requestHasFile = false;
38-
$httpMethod = strtolower($doc->getHttpMethod());
39-
$isGet = $httpMethod == 'get';
40-
$isPost = $httpMethod == 'post';
41-
$isPut = $httpMethod == 'put';
42-
$isDelete = $httpMethod == 'delete';
43-
44-
$this->openApi['paths'][$doc->getUri()][$httpMethod]['description'] = $doc->getDocBlock();
45-
$this->openApi['paths'][$doc->getUri()][$httpMethod]['parameters'] = [];
37+
$requestHasFile = false;
38+
$httpMethod = strtolower($doc->getHttpMethod());
39+
$isGet = $httpMethod == 'get';
40+
$isPost = $httpMethod == 'post';
41+
$isPut = $httpMethod == 'put';
42+
$isDelete = $httpMethod == 'delete';
43+
$uriLeadingSlash = '/' . $doc->getUri();
44+
45+
$this->openApi['paths'][$uriLeadingSlash][$httpMethod]['description'] = $doc->getDocBlock();
46+
$this->openApi['paths'][$uriLeadingSlash][$httpMethod]['parameters'] = [];
47+
48+
foreach ($doc->getPathParameters() as $parameter => $rule) {
49+
$this->openApi['paths'][$uriLeadingSlash][$httpMethod]['parameters'][] = $this->makeQueryParameterItem($parameter, $rule);
50+
}
4651

47-
$this->openApi['paths'][$doc->getUri()][$httpMethod]['responses'] = config('request-docs.open_api.responses', []);
52+
$this->openApi['paths'][$uriLeadingSlash][$httpMethod]['responses'] = config('request-docs.open_api.responses', []);
4853

4954
foreach ($doc->getRules() as $attribute => $rules) {
5055
foreach ($rules as $rule) {
@@ -60,21 +65,18 @@ private function docsToOpenApi(array $docs): void
6065

6166
$contentType = $requestHasFile ? 'multipart/form-data' : 'application/json';
6267

63-
if ($isGet) {
64-
$this->openApi['paths'][$doc->getUri()][$httpMethod]['parameters'] = [];
65-
}
6668
if ($isPost || $isPut || $isDelete) {
67-
$this->openApi['paths'][$doc->getUri()][$httpMethod]['requestBody'] = $this->makeRequestBodyItem($contentType);
69+
$this->openApi['paths'][$uriLeadingSlash][$httpMethod]['requestBody'] = $this->makeRequestBodyItem($contentType);
6870
}
6971

7072
foreach ($doc->getRules() as $attribute => $rules) {
7173
foreach ($rules as $rule) {
7274
if ($isGet) {
73-
$parameter = $this->makeQueryParameterItem($attribute, $rule);
74-
$this->openApi['paths'][$doc->getUri()][$httpMethod]['parameters'][] = $parameter;
75+
$parameter = $this->makeQueryParameterItem($attribute, $rule);
76+
$this->openApi['paths'][$uriLeadingSlash][$httpMethod]['parameters'][] = $parameter;
7577
}
7678
if ($isPost || $isPut || $isDelete) {
77-
$this->openApi['paths'][$doc->getUri()][$httpMethod]['requestBody']['content'][$contentType]['schema']['properties'][$attribute] = $this->makeRequestBodyContentPropertyItem($rule);
79+
$this->openApi['paths'][$uriLeadingSlash][$httpMethod]['requestBody']['content'][$contentType]['schema']['properties'][$attribute] = $this->makeRequestBodyContentPropertyItem($rule);
7880
}
7981
}
8082
}

0 commit comments

Comments
 (0)