|
20 | 20 | use ApiPlatform\JsonSchema\TypeFactoryInterface;
|
21 | 21 | use ApiPlatform\Metadata\ApiResource;
|
22 | 22 | use ApiPlatform\Metadata\CollectionOperationInterface;
|
| 23 | +use ApiPlatform\Metadata\HeaderParameterInterface; |
23 | 24 | use ApiPlatform\Metadata\HttpOperation;
|
24 | 25 | use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
|
25 | 26 | use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
|
@@ -274,29 +275,57 @@ private function collectPaths(ApiResource $resource, ResourceMetadataCollection
|
274 | 275 | }
|
275 | 276 |
|
276 | 277 | // Set up parameters
|
| 278 | + $openapiParameters = $openapiOperation->getParameters(); |
277 | 279 | foreach ($operation->getUriVariables() ?? [] as $parameterName => $uriVariable) {
|
278 | 280 | if ($uriVariable->getExpandedValue() ?? false) {
|
279 | 281 | continue;
|
280 | 282 | }
|
281 | 283 |
|
282 |
| - $parameter = new Parameter($parameterName, 'path', "$resourceShortName identifier", true, false, false, ['type' => 'string']); |
283 |
| - if ($this->hasParameter($openapiOperation, $parameter)) { |
| 284 | + $parameter = new Parameter($parameterName, 'path', $uriVariable->getDescription() ?? "$resourceShortName identifier", $uriVariable->getRequired() ?? true, false, false, $uriVariable->getSchema() ?? ['type' => 'string']); |
| 285 | + |
| 286 | + if ($linkParameter = $uriVariable->getOpenApi()) { |
| 287 | + $parameter = $this->mergeParameter($parameter, $linkParameter); |
| 288 | + } |
| 289 | + |
| 290 | + if ([$i, $operationParameter] = $this->hasParameter($openapiOperation, $parameter)) { |
| 291 | + $openapiParameters[$i] = $this->mergeParameter($parameter, $operationParameter); |
284 | 292 | continue;
|
285 | 293 | }
|
286 | 294 |
|
287 |
| - $openapiOperation = $openapiOperation->withParameter($parameter); |
| 295 | + $openapiParameters[] = $parameter; |
288 | 296 | }
|
289 | 297 |
|
| 298 | + $openapiOperation = $openapiOperation->withParameters($openapiParameters); |
| 299 | + |
290 | 300 | if ($operation instanceof CollectionOperationInterface && 'POST' !== $method) {
|
291 | 301 | foreach (array_merge($this->getPaginationParameters($operation), $this->getFiltersParameters($operation)) as $parameter) {
|
292 |
| - if ($this->hasParameter($openapiOperation, $parameter)) { |
| 302 | + if ($operationParameter = $this->hasParameter($openapiOperation, $parameter)) { |
293 | 303 | continue;
|
294 | 304 | }
|
295 | 305 |
|
296 | 306 | $openapiOperation = $openapiOperation->withParameter($parameter);
|
297 | 307 | }
|
298 | 308 | }
|
299 | 309 |
|
| 310 | + $openapiParameters = $openapiOperation->getParameters(); |
| 311 | + foreach ($operation->getParameters() ?? [] as $key => $p) { |
| 312 | + $in = $p instanceof HeaderParameterInterface ? 'header' : 'query'; |
| 313 | + $parameter = new Parameter($key, $in, $p->getDescription() ?? "$resourceShortName $key", $p->getRequired() ?? false, false, false, $p->getSchema() ?? ['type' => 'string']); |
| 314 | + |
| 315 | + if ($linkParameter = $p->getOpenApi()) { |
| 316 | + $parameter = $this->mergeParameter($parameter, $linkParameter); |
| 317 | + } |
| 318 | + |
| 319 | + if ([$i, $operationParameter] = $this->hasParameter($openapiOperation, $parameter)) { |
| 320 | + $openapiParameters[$i] = $this->mergeParameter($parameter, $operationParameter); |
| 321 | + continue; |
| 322 | + } |
| 323 | + |
| 324 | + $openapiParameters[] = $parameter; |
| 325 | + } |
| 326 | + |
| 327 | + $openapiOperation = $openapiOperation->withParameters($openapiParameters); |
| 328 | + |
300 | 329 | $existingResponses = $openapiOperation?->getResponses() ?: [];
|
301 | 330 | $overrideResponses = $operation->getExtraProperties()[self::OVERRIDE_OPENAPI_RESPONSES] ?? $this->openApiOptions->getOverrideResponses();
|
302 | 331 | if ($overrideResponses || !$existingResponses) {
|
@@ -712,14 +741,47 @@ private function appendSchemaDefinitions(\ArrayObject $schemas, \ArrayObject $de
|
712 | 741 | }
|
713 | 742 | }
|
714 | 743 |
|
715 |
| - private function hasParameter(Model\Operation $operation, Parameter $parameter): bool |
| 744 | + /** |
| 745 | + * @return array{0: int, 1: Parameter}|null |
| 746 | + */ |
| 747 | + private function hasParameter(Model\Operation $operation, Parameter $parameter): ?array |
716 | 748 | {
|
717 |
| - foreach ($operation->getParameters() as $existingParameter) { |
| 749 | + foreach ($operation->getParameters() as $key => $existingParameter) { |
718 | 750 | if ($existingParameter->getName() === $parameter->getName() && $existingParameter->getIn() === $parameter->getIn()) {
|
719 |
| - return true; |
| 751 | + return [$key, $existingParameter]; |
| 752 | + } |
| 753 | + } |
| 754 | + |
| 755 | + return null; |
| 756 | + } |
| 757 | + |
| 758 | + private function mergeParameter(Parameter $actual, Parameter $defined): Parameter |
| 759 | + { |
| 760 | + foreach ([ |
| 761 | + 'name', |
| 762 | + 'in', |
| 763 | + 'description', |
| 764 | + 'required', |
| 765 | + 'deprecated', |
| 766 | + 'allowEmptyValue', |
| 767 | + 'style', |
| 768 | + 'explode', |
| 769 | + 'allowReserved', |
| 770 | + 'example', |
| 771 | + ] as $method) { |
| 772 | + $newValue = $defined->{"get$method"}(); |
| 773 | + if (null !== $newValue && $actual->{"get$method"}() !== $newValue) { |
| 774 | + $actual = $actual->{"with$method"}($newValue); |
| 775 | + } |
| 776 | + } |
| 777 | + |
| 778 | + foreach (['examples', 'content', 'schema'] as $method) { |
| 779 | + $newValue = $defined->{"get$method"}(); |
| 780 | + if ($newValue && \count($newValue) > 0 && $actual->{"get$method"}() !== $newValue) { |
| 781 | + $actual = $actual->{"with$method"}($newValue); |
720 | 782 | }
|
721 | 783 | }
|
722 | 784 |
|
723 |
| - return false; |
| 785 | + return $actual; |
724 | 786 | }
|
725 | 787 | }
|
0 commit comments