1515
1616use ApiPlatform \Metadata \HttpOperation ;
1717use ApiPlatform \Metadata \Operation ;
18+ use ApiPlatform \Metadata \Parameters ;
1819use ApiPlatform \State \Exception \ParameterNotSupportedException ;
1920use ApiPlatform \State \Exception \ProviderNotFoundException ;
2021use ApiPlatform \State \ParameterNotFound ;
22+ use ApiPlatform \State \ParameterProvider \ReadLinkParameterProvider ;
2123use ApiPlatform \State \ProviderInterface ;
2224use ApiPlatform \State \Util \ParameterParserTrait ;
2325use ApiPlatform \State \Util \RequestParser ;
@@ -50,25 +52,41 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
5052 $ request ->attributes ->set ('_api_header_parameters ' , $ request ->headers ->all ());
5153 }
5254
53- $ parameters = $ operation ->getParameters ();
55+ if ($ request && null === $ request ->attributes ->get ('_api_path_parameters ' )) {
56+ $ request ->attributes ->set ('_api_path_parameters ' , $ request ->attributes ->all ());
57+ }
58+
59+ $ parameters = $ operation ->getParameters () ?? new Parameters ();
5460
55- if ($ operation instanceof HttpOperation && true === $ operation ->getStrictQueryParameterValidation ()) {
56- $ keys = [];
57- foreach ($ parameters as $ parameter ) {
58- $ keys [] = $ parameter ->getKey ();
61+ if ($ operation instanceof HttpOperation) {
62+ // TODO: this should return Parameters but its a BC, prepare that change in 4.3
63+ foreach ($ operation ->getUriVariables () ?? [] as $ key => $ uriVariable ) {
64+ if ($ uriVariable ->getSecurity () && !$ uriVariable ->getProvider ()) {
65+ $ uriVariable = $ uriVariable ->withProvider (ReadLinkParameterProvider::class);
66+ }
67+
68+ $ parameters ->add ($ key , $ uriVariable ->withKey ($ key ));
5969 }
6070
61- foreach (array_keys ($ request ->attributes ->get ('_api_query_parameters ' )) as $ key ) {
62- if (!\in_array ($ key , $ keys , true )) {
63- throw new ParameterNotSupportedException ($ key );
71+ if (true === $ operation ->getStrictQueryParameterValidation ()) {
72+ $ keys = [];
73+ foreach ($ parameters as $ parameter ) {
74+ $ keys [] = $ parameter ->getKey ();
75+ }
76+
77+ foreach (array_keys ($ request ->attributes ->get ('_api_query_parameters ' )) as $ key ) {
78+ if (!\in_array ($ key , $ keys , true )) {
79+ throw new ParameterNotSupportedException ($ key );
80+ }
6481 }
6582 }
6683 }
6784
68- foreach ($ parameters ?? [] as $ parameter ) {
69- $ extraProperties = $ parameter ->getExtraProperties ();
70- unset($ extraProperties ['_api_values ' ]);
71- $ parameters ->add ($ parameter ->getKey (), $ parameter = $ parameter ->withExtraProperties ($ extraProperties ));
85+ foreach ($ parameters as $ parameter ) {
86+ // we force API Platform's value extraction, use _api_query_parameters or _api_header_parameters if you need to set a value
87+ if (isset ($ parameter ->getExtraProperties ()['_api_values ' ])) {
88+ unset($ parameter ->getExtraProperties ()['_api_values ' ]);
89+ }
7290
7391 $ context = ['operation ' => $ operation ] + $ context ;
7492 $ values = $ this ->getParameterValues ($ parameter , $ request , $ context );
@@ -78,14 +96,12 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
7896 $ value = $ default ;
7997 }
8098
99+ $ parameter ->setValue ($ value );
100+
81101 if ($ value instanceof ParameterNotFound) {
82102 continue ;
83103 }
84104
85- $ parameters ->add ($ parameter ->getKey (), $ parameter = $ parameter ->withExtraProperties (
86- $ parameter ->getExtraProperties () + ['_api_values ' => $ value ]
87- ));
88-
89105 if (null === ($ provider = $ parameter ->getProvider ())) {
90106 continue ;
91107 }
@@ -111,7 +127,7 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
111127 }
112128 }
113129
114- if ($ parameters ) {
130+ if (\count ( $ parameters) ) {
115131 $ operation = $ operation ->withParameters ($ parameters );
116132 }
117133 $ request ?->attributes->set ('_api_operation ' , $ operation );
0 commit comments