|
27 | 27 | use ApiPlatform\Metadata\Exception\RuntimeException; |
28 | 28 | use ApiPlatform\Metadata\HeaderParameterInterface; |
29 | 29 | use ApiPlatform\Metadata\HttpOperation; |
| 30 | +use ApiPlatform\Metadata\Parameter as ApiPlatformParameter; |
30 | 31 | use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface; |
31 | 32 | use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface; |
32 | 33 | use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface; |
@@ -331,12 +332,20 @@ private function collectPaths(ApiResource $resource, ResourceMetadataCollection |
331 | 332 | } |
332 | 333 | } |
333 | 334 |
|
| 335 | + $entityClass = $this->getFilterClass($operation); |
334 | 336 | $openapiParameters = $openapiOperation->getParameters(); |
335 | 337 | foreach ($operation->getParameters() ?? [] as $key => $p) { |
336 | 338 | if (false === $p->getOpenApi()) { |
337 | 339 | continue; |
338 | 340 | } |
339 | 341 |
|
| 342 | + if (($f = $p->getFilter()) && $this->filterLocator->has($f)) { |
| 343 | + $filter = $this->filterLocator->get($f); |
| 344 | + foreach ($filter->getDescription($entityClass) as $name => $description) { |
| 345 | + $parameters[] = $this->getFilterParameter($name, $description, $operation->getShortName()); |
| 346 | + } |
| 347 | + } |
| 348 | + |
340 | 349 | $in = $p instanceof HeaderParameterInterface ? 'header' : 'query'; |
341 | 350 | $parameter = new Parameter($key, $in, $p->getDescription() ?? "$resourceShortName $key", $p->getRequired() ?? false, false, false, $p->getSchema() ?? ['type' => 'string']); |
342 | 351 |
|
@@ -654,92 +663,102 @@ private function getLinks(ResourceMetadataCollection $resourceMetadataCollection |
654 | 663 | private function getFiltersParameters(CollectionOperationInterface|HttpOperation $operation): array |
655 | 664 | { |
656 | 665 | $parameters = []; |
657 | | - |
658 | 666 | $resourceFilters = $operation->getFilters(); |
| 667 | + $entityClass = $this->getFilterClass($operation); |
| 668 | + |
659 | 669 | foreach ($resourceFilters ?? [] as $filterId) { |
660 | 670 | if (!$this->filterLocator->has($filterId)) { |
661 | 671 | continue; |
662 | 672 | } |
663 | 673 |
|
664 | 674 | $filter = $this->filterLocator->get($filterId); |
665 | | - $entityClass = $operation->getClass(); |
666 | | - if ($options = $operation->getStateOptions()) { |
667 | | - if ($options instanceof DoctrineOptions && $options->getEntityClass()) { |
668 | | - $entityClass = $options->getEntityClass(); |
669 | | - } |
670 | | - |
671 | | - if ($options instanceof DoctrineODMOptions && $options->getDocumentClass()) { |
672 | | - $entityClass = $options->getDocumentClass(); |
673 | | - } |
| 675 | + foreach ($filter->getDescription($entityClass) as $name => $description) { |
| 676 | + $parameters[] = $this->getFilterParameter($name, $description, $operation->getShortName()); |
674 | 677 | } |
| 678 | + } |
675 | 679 |
|
676 | | - foreach ($filter->getDescription($entityClass) as $name => $data) { |
677 | | - if (isset($data['swagger'])) { |
678 | | - trigger_deprecation('api-platform/core', '4.0', \sprintf('Using the "swagger" field of the %s::getDescription() (%s) is deprecated.', $filter::class, $operation->getShortName())); |
679 | | - } |
| 680 | + return $parameters; |
| 681 | + } |
680 | 682 |
|
681 | | - if (!isset($data['openapi']) || $data['openapi'] instanceof Parameter) { |
682 | | - $schema = $data['schema'] ?? []; |
| 683 | + private function getFilterClass(HttpOperation $operation): string |
| 684 | + { |
| 685 | + $entityClass = $operation->getClass(); |
| 686 | + if ($options = $operation->getStateOptions()) { |
| 687 | + if ($options instanceof DoctrineOptions && $options->getEntityClass()) { |
| 688 | + return $options->getEntityClass(); |
| 689 | + } |
683 | 690 |
|
684 | | - if (isset($data['type']) && \in_array($data['type'] ?? null, Type::$builtinTypes, true) && !isset($schema['type'])) { |
685 | | - $schema += $this->jsonSchemaTypeFactory ? $this->jsonSchemaTypeFactory->getType(new Type($data['type'], false, null, $data['is_collection'] ?? false)) : $this->getType(new Type($data['type'], false, null, $data['is_collection'] ?? false)); |
686 | | - } |
| 691 | + if ($options instanceof DoctrineODMOptions && $options->getDocumentClass()) { |
| 692 | + return $options->getDocumentClass(); |
| 693 | + } |
| 694 | + } |
687 | 695 |
|
688 | | - if (!isset($schema['type'])) { |
689 | | - $schema['type'] = 'string'; |
690 | | - } |
| 696 | + return $entityClass; |
| 697 | + } |
691 | 698 |
|
692 | | - $style = 'array' === ($schema['type'] ?? null) && \in_array( |
693 | | - $data['type'], |
694 | | - [Type::BUILTIN_TYPE_ARRAY, Type::BUILTIN_TYPE_OBJECT], |
695 | | - true |
696 | | - ) ? 'deepObject' : 'form'; |
| 699 | + private function getFilterParameter(string $name, array $description, string $shortName): ApiPlatformParameter |
| 700 | + { |
| 701 | + if (isset($description['swagger'])) { |
| 702 | + trigger_deprecation('api-platform/core', '4.0', \sprintf('Using the "swagger" field of the %s::getDescription() (%s) is deprecated.', $filter::class, $shortName)); |
| 703 | + } |
697 | 704 |
|
698 | | - $parameter = isset($data['openapi']) && $data['openapi'] instanceof Parameter ? $data['openapi'] : new Parameter(in: 'query', name: $name, style: $style, explode: $data['is_collection'] ?? false); |
| 705 | + if (!isset($description['openapi']) || $description['openapi'] instanceof Parameter) { |
| 706 | + $schema = $description['schema'] ?? []; |
699 | 707 |
|
700 | | - if ('' === $parameter->getDescription() && ($description = $data['description'] ?? '')) { |
701 | | - $parameter = $parameter->withDescription($description); |
702 | | - } |
| 708 | + if (isset($description['type']) && \in_array($description['type'] ?? null, Type::$builtinTypes, true) && !isset($schema['type'])) { |
| 709 | + $schema += $this->jsonSchemaTypeFactory ? $this->jsonSchemaTypeFactory->getType(new Type($description['type'], false, null, $description['is_collection'] ?? false)) : $this->getType(new Type($description['type'], false, null, $description['is_collection'] ?? false)); |
| 710 | + } |
703 | 711 |
|
704 | | - if (false === $parameter->getRequired() && false !== ($required = $data['required'] ?? false)) { |
705 | | - $parameter = $parameter->withRequired($required); |
706 | | - } |
| 712 | + if (!isset($schema['type'])) { |
| 713 | + $schema['type'] = 'string'; |
| 714 | + } |
707 | 715 |
|
708 | | - $parameters[] = $parameter->withSchema($schema); |
709 | | - continue; |
710 | | - } |
| 716 | + $style = 'array' === ($schema['type'] ?? null) && \in_array( |
| 717 | + $description['type'], |
| 718 | + [Type::BUILTIN_TYPE_ARRAY, Type::BUILTIN_TYPE_OBJECT], |
| 719 | + true |
| 720 | + ) ? 'deepObject' : 'form'; |
711 | 721 |
|
712 | | - trigger_deprecation('api-platform/core', '4.0', \sprintf('Not using "%s" on the "openapi" field of the %s::getDescription() (%s) is deprecated.', Parameter::class, $filter::class, $operation->getShortName())); |
713 | | - if ($this->jsonSchemaTypeFactory) { |
714 | | - $schema = $data['schema'] ?? (\in_array($data['type'], Type::$builtinTypes, true) ? $this->jsonSchemaTypeFactory->getType(new Type($data['type'], false, null, $data['is_collection'] ?? false), 'openapi') : ['type' => 'string']); |
715 | | - } else { |
716 | | - $schema = $data['schema'] ?? (\in_array($data['type'], Type::$builtinTypes, true) ? $this->getType(new Type($data['type'], false, null, $data['is_collection'] ?? false)) : ['type' => 'string']); |
717 | | - } |
| 722 | + $parameter = isset($description['openapi']) && $description['openapi'] instanceof Parameter ? $description['openapi'] : new Parameter(in: 'query', name: $name, style: $style, explode: $description['is_collection'] ?? false); |
718 | 723 |
|
719 | | - $parameters[] = new Parameter( |
720 | | - $name, |
721 | | - 'query', |
722 | | - $data['description'] ?? '', |
723 | | - $data['required'] ?? false, |
724 | | - $data['openapi']['deprecated'] ?? false, |
725 | | - $data['openapi']['allowEmptyValue'] ?? true, |
726 | | - $schema, |
727 | | - 'array' === $schema['type'] && \in_array( |
728 | | - $data['type'], |
729 | | - [Type::BUILTIN_TYPE_ARRAY, Type::BUILTIN_TYPE_OBJECT], |
730 | | - true |
731 | | - ) ? 'deepObject' : 'form', |
732 | | - $data['openapi']['explode'] ?? ('array' === $schema['type']), |
733 | | - $data['openapi']['allowReserved'] ?? false, |
734 | | - $data['openapi']['example'] ?? null, |
735 | | - isset( |
736 | | - $data['openapi']['examples'] |
737 | | - ) ? new \ArrayObject($data['openapi']['examples']) : null |
738 | | - ); |
| 724 | + if ('' === $parameter->getDescription() && ($description = $description['description'] ?? '')) { |
| 725 | + $parameter = $parameter->withDescription($description); |
739 | 726 | } |
| 727 | + |
| 728 | + if (false === $parameter->getRequired() && false !== ($required = $description['required'] ?? false)) { |
| 729 | + $parameter = $parameter->withRequired($required); |
| 730 | + } |
| 731 | + |
| 732 | + return $parameter->withSchema($schema); |
740 | 733 | } |
741 | 734 |
|
742 | | - return $parameters; |
| 735 | + trigger_deprecation('api-platform/core', '4.0', \sprintf('Not using "%s" on the "openapi" field of the %s::getDescription() (%s) is deprecated.', Parameter::class, $filter::class, $shortName)); |
| 736 | + if ($this->jsonSchemaTypeFactory) { |
| 737 | + $schema = $description['schema'] ?? (\in_array($description['type'], Type::$builtinTypes, true) ? $this->jsonSchemaTypeFactory->getType(new Type($description['type'], false, null, $description['is_collection'] ?? false), 'openapi') : ['type' => 'string']); |
| 738 | + } else { |
| 739 | + $schema = $description['schema'] ?? (\in_array($description['type'], Type::$builtinTypes, true) ? $this->getType(new Type($description['type'], false, null, $description['is_collection'] ?? false)) : ['type' => 'string']); |
| 740 | + } |
| 741 | + |
| 742 | + return new Parameter( |
| 743 | + $name, |
| 744 | + 'query', |
| 745 | + $description['description'] ?? '', |
| 746 | + $description['required'] ?? false, |
| 747 | + $description['openapi']['deprecated'] ?? false, |
| 748 | + $description['openapi']['allowEmptyValue'] ?? true, |
| 749 | + $schema, |
| 750 | + 'array' === $schema['type'] && \in_array( |
| 751 | + $description['type'], |
| 752 | + [Type::BUILTIN_TYPE_ARRAY, Type::BUILTIN_TYPE_OBJECT], |
| 753 | + true |
| 754 | + ) ? 'deepObject' : 'form', |
| 755 | + $description['openapi']['explode'] ?? ('array' === $schema['type']), |
| 756 | + $description['openapi']['allowReserved'] ?? false, |
| 757 | + $description['openapi']['example'] ?? null, |
| 758 | + isset( |
| 759 | + $description['openapi']['examples'] |
| 760 | + ) ? new \ArrayObject($description['openapi']['examples']) : null |
| 761 | + ); |
743 | 762 | } |
744 | 763 |
|
745 | 764 | private function getPaginationParameters(CollectionOperationInterface|HttpOperation $operation): array |
|
0 commit comments