-
Notifications
You must be signed in to change notification settings - Fork 108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot use Symfony arrays in config #148
Comments
Did you try |
@Incubbus yes, but this variable is still a string, it's converted to array later, so it does not throw Exception as above, but it just doesn't work |
I have the same error here. |
In my case, I have a fixed number of allowed origins, so I was able to workaround this limitation using this procedure: First in parameters:
computed_absolute_url: "%router.request_context.scheme%://%router.request_context.host%%env(ABSOLUTE_URL_PORT)%%router.request_context.base_url%"
# Allowed CORS origins.
env(NELMIO_CORS_ORIGIN_FRONTEND): "http://localhost:8080/"
env(NELMIO_CORS_ORIGIN_BACKEND): "%computed_absolute_url%"
nelmio_cors_origin_frontend: "%env(resolve:NELMIO_CORS_ORIGIN_FRONTEND)%"
nelmio_cors_origin_backend: "%env(resolve:NELMIO_CORS_ORIGIN_BACKEND)%" Please note that there are a few custom env vars hanging in Then in nelmio_cors:
defaults:
allow_origin: ["%nelmio_cors_origin_frontend%", "%nelmio_cors_origin_backend%"]
# ...
paths:
# ... |
The error we get is mostly due to the fact that the extension does some work around the defaults, but when reaching the extension being called, env variables are not resolved. There might be a few options to make it work: either resolve those at runtime (ie. upon service initialisation during request) or may be trying in a compiler pass ? But that second option probably wouldn't work, althought it might worth it to at least try. |
Any updates on fixing this issue? I would like to have only one configuration that reads the list of allowed origins from an environment variable instead of having multiple hard-coded configurations and bandaids. This is definitely a bundle configuration issue. |
If someone can send a PR for this it'd be welcome. I'm not super familiar with env var params. I guess this might need to allow strings in the Configuration class check at runtime once env vars are resolved that we have an array? |
Is there a decent workaround that is possible? |
same issue here.. I've encountered this issue and started debugging because I didn't understand what was going wrong and I still don't. Symfony version: 5.4.16 Steps to reproduce In my parameters.yaml I've got the following content: env(TEST2): '["http://test1.localhost/","http://test2.localhost/"]'
test: ["http://test1.localhost/","http://test2.localhost/"]
test2: '%env(json:TEST2)%' When I dump how
I bind the two params I've created to some params I can inject in a controller in services.yaml services:
_defaults:
autowire: true
autoconfigure: true
bind:
$test: '%test%'
$test2: '%test2%' I create some controller method: /** @Route(path="/test", methods={"GET"}) */
public function test(): Response
{
return new JsonResponse(['test' => $this->test, 'test2' => $this->test2]);
} and the response I get is when calling the endpoint via an HTTP request is: {
"test": [
"http://test1.localhost/",
"http://test2.localhost/"
],
"test2": [
"http://test1.localhost/",
"http://test2.localhost/"
]
} When I use '%test% as param in nelmio.defaults.allow_origin everything works as expected
The value which comes in as allow_origin NelmioCorsExtension is the following:
So far so good.. When I now use '%test2% as param the following happens: nelmio_cors:
defaults:
allow_credentials: true
origin_regex: true
allow_origin: '%test2%'
allow_methods: ['GET', 'OPTIONS', 'POST', 'PUT', 'PATCH', 'DELETE']
allow_headers: ['Content-Type', 'Authorization']
expose_headers: ['Link', 'Content-Disposition']
max_age: 3600
paths:
'^/': null The value which comes in as allow_origin in NelmioCorsExtionsion is now:
and this will throw the error:
when trying to do
in NelmioCorsExtension::L49 When I now do the following in nelmio_cors_.yaml:
I expect it passes NelmioCorsExtension because the value is now an array:
But it now fails with the message:
in BaseNode.php::L573: exception trace:
My guess is that the env variables are only processed after the bundle extension is loaded because env variables are only loaded at runtime. Also interesting: When I remove all checks in NelmioCorsExtension::load() which already expects an array. The configuration processor which is default is still failing because an allow_origin is defined as an ArrayNode. In other words, I expects this is a bigger issue in Symfony itself. Because it impossible to use
|
Yes, env params are only available at runtime |
But they are indeed available at runtime, to set up your application. So is there maybe a way to tell the bundle to avoid the check on compile time, and do a check on runtime? |
We probably will have to wait for symfony/symfony#40906? 🤔 |
Hey! The same problem, does anybody find workaround? |
We write our own solution. 🤣 |
In the latest version I've been trying to use:
where CORS_ALLOW_ORIGIN_CSV is a CSV value, but NelmioCorsBundle recognizes it as a string value instead of array and returns an error
Fatal error: Uncaught ErrorException: Warning: in_array() expects parameter 2 to be array, string given in /var/www/pimcore/vendor/nelmio/cors-bundle/DependencyInjection/NelmioCorsExtension.php:60
The text was updated successfully, but these errors were encountered: