forked from zircote/swagger-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOperation.php
248 lines (217 loc) · 7.34 KB
/
Operation.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php declare(strict_types=1);
/**
* @license Apache 2.0
*/
namespace OpenApi\Annotations;
use OpenApi\Generator;
/**
* Base class for `@OA\Get`, `@OA\Post`, `@OA\Put`, etc.
*
* Describes a single API operation on a path.
*
* @see [OAI Operation Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#operation-object)
*
* @Annotation
*/
abstract class Operation extends AbstractAnnotation
{
/**
* Key in the OpenApi "Paths Object" for this operation.
*
* @var string
*/
public $path = Generator::UNDEFINED;
/**
* A list of tags for API documentation control.
*
* Tags can be used for logical grouping of operations by resources or any other qualifier.
*
* @var string[]
*/
public $tags = Generator::UNDEFINED;
/**
* Key in the OpenApi "Path Item Object" for this operation.
*
* Allowed values: 'get', 'post', put', 'patch', 'delete', 'options', 'head' and 'trace'.
*
* @var string
*/
public $method = Generator::UNDEFINED;
/**
* A short summary of what the operation does.
*
* @var string
*/
public $summary = Generator::UNDEFINED;
/**
* A verbose explanation of the operation behavior.
*
* CommonMark syntax MAY be used for rich text representation.
*
* @var string
*/
public $description = Generator::UNDEFINED;
/**
* Additional external documentation for this operation.
*
* @var ExternalDocumentation
*/
public $externalDocs = Generator::UNDEFINED;
/**
* Unique string used to identify the operation.
*
* The id must be unique among all operations described in the API.
* Tools and libraries may use the operationId to uniquely identify an operation, therefore, it is recommended to
* follow common programming naming conventions.
*
* @var string
*/
public $operationId = Generator::UNDEFINED;
/**
* A list of parameters that are applicable for this operation.
*
* If a parameter is already defined at the Path Item, the new definition will override it but can never remove it.
* The list must not include duplicated parameters.
*
* A unique parameter is defined by a combination of a name and location.
*
* The list can use the Reference Object to link to parameters that are defined at the OpenAPI Object's
* components/parameters.
*
* @var Parameter[]
*/
public $parameters = Generator::UNDEFINED;
/**
* The request body applicable for this operation.
*
* The requestBody is only supported in HTTP methods where the HTTP 1.1 specification RFC7231 has explicitly
* defined semantics for request bodies. In other cases where the HTTP spec is vague, requestBody shall be ignored
* by consumers.
*
* @var RequestBody
*/
public $requestBody = Generator::UNDEFINED;
/**
* The list of possible responses as they are returned from executing this operation.
*
* @var Response[]
*/
public $responses = Generator::UNDEFINED;
/**
* A map of possible out-of band callbacks related to the parent operation.
*
* The key is a unique identifier for the Callback Object.
*
* Each value in the map is a Callback Object that describes a request that may be initiated by the API provider
* and the expected responses. The key value used to identify the callback object is an expression, evaluated at
* runtime, that identifies a URL to use for the callback operation.
*
* @var array
*/
public $callbacks = Generator::UNDEFINED;
/**
* Declares this operation to be deprecated.
*
* Consumers should refrain from usage of the declared operation.
*
* Default value is false.
*
* @var bool
*/
public $deprecated = Generator::UNDEFINED;
/**
* A declaration of which security mechanisms can be used for this operation.
*
* The list of values includes alternative security requirement objects that can be used.
*
* Only one of the security requirement objects need to be satisfied to authorize a request.
*
* This definition overrides any declared top-level security.
* To remove a top-level security declaration, an empty array can be used.
*
* @var array
*/
public $security = Generator::UNDEFINED;
/**
* An alternative server array to service this operation.
*
* If an alternative server object is specified at the Path Item Object or Root level, it will be overridden by
* this value.
*
* @var Server[]
*/
public $servers = Generator::UNDEFINED;
/**
* @inheritdoc
*/
public static $_required = ['responses'];
/**
* @inheritdoc
*/
public static $_types = [
'path' => 'string',
'method' => 'string',
'tags' => '[string]',
'summary' => 'string',
'description' => 'string',
'deprecated' => 'boolean',
];
/**
* @inheritdoc
*/
public static $_nested = [
Parameter::class => ['parameters'],
PathParameter::class => ['parameters'],
Response::class => ['responses', 'response'],
ExternalDocumentation::class => 'externalDocs',
Server::class => ['servers'],
RequestBody::class => 'requestBody',
Attachable::class => ['attachables'],
];
/**
* @inheritdoc
*/
#[\ReturnTypeWillChange]
public function jsonSerialize()
{
$data = parent::jsonSerialize();
unset($data->method);
unset($data->path);
// ensure security elements are object
if (isset($data->security) && is_array($data->security)) {
foreach ($data->security as $key => $scheme) {
$data->security[$key] = (object) $scheme;
}
}
return $data;
}
/**
* @inheritdoc
*/
public function validate(array $stack = [], array $skip = [], string $ref = '', $context = null): bool
{
if (in_array($this, $skip, true)) {
return true;
}
$valid = parent::validate($stack, $skip, $ref, $context);
if (!Generator::isDefault($this->responses)) {
foreach ($this->responses as $response) {
if (!Generator::isDefault($response->response) && $response->response !== 'default' && preg_match('/^([12345]{1}[0-9]{2})|([12345]{1}XX)$/', (string) $response->response) === 0) {
$this->_context->logger->warning('Invalid value "' . $response->response . '" for ' . $response->_identity([]) . '->response, expecting "default", a HTTP Status Code or HTTP Status Code range definition in ' . $response->_context);
$valid = false;
}
}
}
if (is_object($context) && !Generator::isDefault($this->operationId)) {
if (!property_exists($context, 'operationIds')) {
$context->operationIds = [];
}
if (in_array($this->operationId, $context->operationIds)) {
$this->_context->logger->warning('operationId must be unique. Duplicate value found: "' . $this->operationId . '"');
$valid = false;
}
$context->operationIds[] = $this->operationId;
}
return $valid;
}
}