Skip to content

Commit a159f7b

Browse files
authored
Merge pull request #92 from RonasIT/90-prefix
feat: removed unnecessary grouping by prefix
2 parents ea0c5b5 + 4c5dcb4 commit a159f7b

File tree

5 files changed

+342
-5
lines changed

5 files changed

+342
-5
lines changed

src/Services/SwaggerService.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -666,11 +666,17 @@ public function saveConsume()
666666

667667
public function saveTags()
668668
{
669-
$tagIndex = 1;
669+
$globalPrefix = config('auto-doc.global_prefix');
670+
$globalPrefix = Str::after($globalPrefix, '/');
670671

671672
$explodedUri = explode('/', $this->uri);
673+
$explodedUri = array_filter($explodedUri);
672674

673-
$tag = Arr::get($explodedUri, $tagIndex);
675+
$tag = array_shift($explodedUri);
676+
677+
if ($globalPrefix === $tag) {
678+
$tag = array_shift($explodedUri);
679+
}
674680

675681
$this->item['tags'] = [$tag];
676682
}

src/Validators/SwaggerSpecValidator.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ protected function validatePaths(): void
137137
$this->validateParameters($operation, $path, $operationId);
138138

139139
if (!empty($operation['requestBody'])) {
140-
$this->validateRequestBody($operation, $path, $operationId);
140+
$this->validateRequestBody($operation, $operationId);
141141
}
142142

143143
foreach ($operation['responses'] as $statusCode => $response) {
@@ -242,7 +242,7 @@ protected function validateParameters(array $operation, string $path, string $op
242242
$this->validateBodyParameters($parameters, $operationId);
243243
}
244244

245-
protected function validateRequestBody(array $operation, string $path, string $operationId): void
245+
protected function validateRequestBody(array $operation, string $operationId): void
246246
{
247247
$requestBody = Arr::get($operation, 'requestBody', []);
248248

tests/SwaggerServiceTest.php

+52-1
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ public function testAddDataPostRequest()
607607
type: 'post',
608608
uri: 'users',
609609
data: [
610-
'users' => [1,2],
610+
'users' => [1, 2],
611611
'query' => null,
612612
],
613613
headers: [
@@ -620,6 +620,57 @@ public function testAddDataPostRequest()
620620
$service->addData($request, $response);
621621
}
622622

623+
public function testAddDataGlobalPostRequest()
624+
{
625+
$this->addGlobalPrefix();
626+
627+
config(['auto-doc.security' => 'jwt']);
628+
629+
$this->mockDriverGetEmptyAndSaveTmpData($this->getJsonFixture('tmp_data_global_post_user_request'));
630+
631+
$service = app(SwaggerService::class);
632+
633+
$request = $this->generateRequest(
634+
type: 'post',
635+
uri: '/global/users',
636+
data: [
637+
'users' => [1, 2],
638+
'query' => null,
639+
],
640+
headers: ['authorization' => 'Bearer some_token'],
641+
);
642+
643+
$response = $this->generateResponse('example_success_users_post_response.json');
644+
645+
$service->addData($request, $response);
646+
}
647+
648+
public function testAddDataGlobalPostGlobalURIRequest()
649+
{
650+
$this->addGlobalPrefix();
651+
652+
config(['auto-doc.security' => 'jwt']);
653+
654+
$this->mockDriverGetEmptyAndSaveTmpData($this->getJsonFixture('tmp_data_global_post_global_uri_request'));
655+
656+
$service = app(SwaggerService::class);
657+
658+
$request = $this->generateRequest(
659+
type: 'post',
660+
uri: '/global/global/',
661+
data: [
662+
'users' => [1, 2],
663+
'query' => null,
664+
],
665+
headers: [
666+
'authorization' => 'Bearer some_token',
667+
]);
668+
669+
$response = $this->generateResponse('example_success_users_post_response.json');
670+
671+
$service->addData($request, $response);
672+
}
673+
623674
public function testAddDataToEarlyGeneratedDoc()
624675
{
625676
config(['auto-doc.security' => 'jwt']);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
{
2+
"openapi": "3.1.0",
3+
"servers": [
4+
{
5+
"url": "http:\/\/localhost"
6+
}
7+
],
8+
"paths": {
9+
"/global/global": {
10+
"post": {
11+
"tags": [
12+
"global"
13+
],
14+
"consumes": [
15+
"application/x-www-form-urlencoded"
16+
],
17+
"produces": [
18+
"application/json"
19+
],
20+
"parameters": [],
21+
"requestBody": {
22+
"required": true,
23+
"description": "",
24+
"content": {
25+
"application/x-www-form-urlencoded": {
26+
"schema": {
27+
"$ref": "#/components/schemas/globalglobalObject"
28+
}
29+
}
30+
}
31+
},
32+
"responses": {
33+
"200": {
34+
"description": "Operation successfully done",
35+
"content": {
36+
"application/json": {
37+
"schema": {
38+
"$ref": "#/components/schemas/postGlobalglobal200ResponseObject",
39+
"type": "object"
40+
},
41+
"example": [
42+
{
43+
"id": 1,
44+
"name": "admin",
45+
"users": [
46+
{
47+
"id": 1,
48+
"name": "admin"
49+
}
50+
]
51+
},
52+
{
53+
"id": 2,
54+
"name": "client",
55+
"users": [
56+
{
57+
"id": 2,
58+
"name": "first_client"
59+
},
60+
{
61+
"id": 3,
62+
"name": "second_client"
63+
}
64+
]
65+
}
66+
]}
67+
}
68+
}
69+
},
70+
"security": [
71+
{
72+
"jwt": []
73+
}
74+
],
75+
"description": "",
76+
"summary": "test",
77+
"deprecated": false
78+
}
79+
}
80+
},
81+
"components": {
82+
"schemas": {
83+
"globalglobalObject": {
84+
"type": "object",
85+
"properties": {
86+
"query": {
87+
"type": "string",
88+
"description": ""
89+
},
90+
"user_id": {
91+
"type": "integer",
92+
"description": "with_to_array_rule_string_name"
93+
},
94+
"is_email_enabled": {
95+
"type": "string",
96+
"description": "test_rule_without_to_string"
97+
}
98+
},
99+
"example": {
100+
"users": [
101+
1,
102+
2
103+
],
104+
"query": null
105+
},
106+
"required": [
107+
"query"
108+
]
109+
},
110+
"postGlobalglobal200ResponseObject": {
111+
"type": "array",
112+
"properties": {
113+
"items": {
114+
"allOf": [
115+
{
116+
"type": "array"
117+
}
118+
]
119+
}
120+
}
121+
}
122+
}
123+
},
124+
"info": {
125+
"description": "This is automatically collected documentation",
126+
"version": "0.0.0",
127+
"title": "Name of Your Application",
128+
"termsOfService": "",
129+
"contact": {
130+
"email": "[email protected]"
131+
}
132+
},
133+
"securityDefinitions": {
134+
"jwt": {
135+
"type": "apiKey",
136+
"name": "authorization",
137+
"in": "header"
138+
}
139+
}
140+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
{
2+
"openapi": "3.1.0",
3+
"servers": [
4+
{
5+
"url": "http:\/\/localhost"
6+
}
7+
],
8+
"paths": {
9+
"/global/users": {
10+
"post": {
11+
"tags": [
12+
"users"
13+
],
14+
"consumes": [
15+
"application/x-www-form-urlencoded"
16+
],
17+
"produces": [
18+
"application/json"
19+
],
20+
"parameters": [],
21+
"requestBody": {
22+
"required": true,
23+
"description": "",
24+
"content": {
25+
"application/x-www-form-urlencoded": {
26+
"schema": {
27+
"$ref": "#/components/schemas/globalusersObject"
28+
}
29+
}
30+
}
31+
},
32+
"responses": {
33+
"200": {
34+
"description": "Operation successfully done",
35+
"content": {
36+
"application/json": {
37+
"schema": {
38+
"$ref": "#/components/schemas/postGlobalusers200ResponseObject",
39+
"type": "object"
40+
},
41+
"example": [
42+
{
43+
"id": 1,
44+
"name": "admin",
45+
"users": [
46+
{
47+
"id": 1,
48+
"name": "admin"
49+
}
50+
]
51+
},
52+
{
53+
"id": 2,
54+
"name": "client",
55+
"users": [
56+
{
57+
"id": 2,
58+
"name": "first_client"
59+
},
60+
{
61+
"id": 3,
62+
"name": "second_client"
63+
}
64+
]
65+
}
66+
]}
67+
}
68+
}
69+
},
70+
"security": [
71+
{
72+
"jwt": []
73+
}
74+
],
75+
"description": "",
76+
"summary": "test",
77+
"deprecated": false
78+
}
79+
}
80+
},
81+
"components": {
82+
"schemas": {
83+
"globalusersObject": {
84+
"type": "object",
85+
"properties": {
86+
"query": {
87+
"type": "string",
88+
"description": ""
89+
},
90+
"user_id": {
91+
"type": "integer",
92+
"description": "with_to_array_rule_string_name"
93+
},
94+
"is_email_enabled": {
95+
"type": "string",
96+
"description": "test_rule_without_to_string"
97+
}
98+
},
99+
"example": {
100+
"users": [
101+
1,
102+
2
103+
],
104+
"query": null
105+
},
106+
"required": [
107+
"query"
108+
]
109+
},
110+
"postGlobalusers200ResponseObject": {
111+
"type": "array",
112+
"properties": {
113+
"items": {
114+
"allOf": [
115+
{
116+
"type": "array"
117+
}
118+
]
119+
}
120+
}
121+
}
122+
}
123+
},
124+
"info": {
125+
"description": "This is automatically collected documentation",
126+
"version": "0.0.0",
127+
"title": "Name of Your Application",
128+
"termsOfService": "",
129+
"contact": {
130+
"email": "[email protected]"
131+
}
132+
},
133+
"securityDefinitions": {
134+
"jwt": {
135+
"type": "apiKey",
136+
"name": "authorization",
137+
"in": "header"
138+
}
139+
}
140+
}

0 commit comments

Comments
 (0)