Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Allow "json:" env var processor to accept null value #26498

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

Merged
merged 1 commit into from
Mar 27, 2018

Conversation

mcfedr
Copy link
Contributor

@mcfedr mcfedr commented Mar 12, 2018

Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets
License MIT
Doc PR Currently no docs for this feature

Edits the EnvVarProcessor so that it allows any type of variable in JSON encoded vars.

Previously it was only possible to use %env(json:ENV)% for array types, but this seems to be an arbitrary restriction, when I could have any other data type in that JSON.

Valid JSON that was previously rejected:

  • 1
  • null
  • "string"

@nicolas-grekas
Copy link
Member

There are reasons for the current restriction:

  • having a single type (array) makes it easier to validate types, eg for chained processors or config validation
  • the other JSON types can easily be achieved using other processors (ie int, bool, string, null)

@nicolas-grekas nicolas-grekas added this to the 4.1 milestone Mar 12, 2018
@mcfedr
Copy link
Contributor Author

mcfedr commented Mar 12, 2018

I wondered if this might be what's happening. I have a case where in passing either an array or null.
What if I changed this to allow null? I feel like this might be a useful case.

@nicolas-grekas
Copy link
Member

if you want to set null, I think you can already achieve it with the const processor: const:null

@javiereguiluz
Copy link
Member

@nicolas-grekas but I think he's saying that an external piece of software generates this config that usually is a JSON-encoded array but it can be null when the config is empty (Symfony does the same and we use array|null everywhere instead of array|empty array). But I guess you'll need to change that external software to encode an empty JSON array when the config is empty.

@mcfedr
Copy link
Contributor Author

mcfedr commented Mar 12, 2018

Exactly, I need to accept either array or null.

@mcfedr mcfedr changed the title Allow "json:" env var processor to parse any json value Allow "json:" env var processor to accept null value Mar 12, 2018
@nicolas-grekas
Copy link
Member

would you mind adding a test case please?

@mcfedr mcfedr force-pushed the json_env_any branch 2 times, most recently from 05ec676 to fdee150 Compare March 15, 2018 11:29
@mcfedr
Copy link
Contributor Author

mcfedr commented Mar 15, 2018

Sure, also added some more general testing around EnvVarProcessor as there don't seem to exist any at the moment.

use Symfony\Component\DependencyInjection\EnvVarProcessor;
use Symfony\Component\DependencyInjection\Exception\LogicException;

class EnvVarProcessorTest extends TestCase
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would you mind opening a separate PR against branch 3.4 for this test class?

Copy link
Contributor Author

@mcfedr mcfedr Mar 15, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I moved to #26542, but had to remove the case for json:null because it will fail in 3.4, and i cannot add it here until that PR lands in master. I can probably make another PR later with more cases.

@@ -129,7 +129,7 @@ public function getEnv($prefix, $name, \Closure $getEnv)
throw new RuntimeException(sprintf('Invalid JSON in env var "%s": '.json_last_error_msg(), $name));
}

if (!is_array($env)) {
if (null !== $env && !is_array($env)) {
throw new RuntimeException(sprintf('Invalid JSON env var "%s": array expected, %s given.', $name, gettype($env)));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message should be updated: array expected ... -> array or null expected ...

@mcfedr mcfedr force-pushed the json_env_any branch 2 times, most recently from d523a2c to 98b1ae5 Compare March 19, 2018 10:31
@mcfedr
Copy link
Contributor Author

mcfedr commented Mar 19, 2018

Rebased on master to see if it would fix the tests, but now seems to fail on some other doctrine tests

nicolas-grekas added a commit that referenced this pull request Mar 19, 2018
This PR was squashed before being merged into the 3.4 branch (closes #26542).

Discussion
----------

[DI] Add tests for EnvVarProcessor

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        | n/a

Add tests for the `EnvVarProcessor` as it doesn't have any at the moment.

Originally from this PR against master, #26498

Commits
-------

2992bb3 [DI] Add tests for EnvVarProcessor
@nicolas-grekas
Copy link
Member

@mcfedr you said had more tests in 26542 before moving it to 3.4. Did you add them back here after the rebase?

@mcfedr
Copy link
Contributor Author

mcfedr commented Mar 19, 2018

I was thinking it would make sense for the tests in #26542 in to be expanded to include this specific case of paring "null" as valid json, as this commit is based on master I cannot add that here until that commit d818636 is merged into master.

@nicolas-grekas
Copy link
Member

Indeed. Merge done, rebase unlocked on your side.

@mcfedr
Copy link
Contributor Author

mcfedr commented Mar 19, 2018

Ok, rebased with extra test cases

Amend EnvVarProcessorTest to include all possible json values
@fabpot
Copy link
Member

fabpot commented Mar 27, 2018

Thank you @mcfedr.

@fabpot fabpot merged commit abc7480 into symfony:master Mar 27, 2018
fabpot added a commit that referenced this pull request Mar 27, 2018
…mcfedr)

This PR was merged into the 4.1-dev branch.

Discussion
----------

Allow "json:" env var processor to accept null value

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        | Currently no docs for this feature

Edits the EnvVarProcessor so that it allows any type of variable in JSON encoded vars.

Previously it was only possible to use `%env(json:ENV)%` for array types, but this seems to be an arbitrary restriction, when I could have any other data type in that JSON.

Valid JSON that was previously rejected:
- `1`
- `null`
- `"string"`

Commits
-------

abc7480 Allow "json:" env var processor to parse null values
@ro0NL
Copy link
Contributor

ro0NL commented Mar 27, 2018

@mcfedr
Copy link
Contributor Author

mcfedr commented Mar 27, 2018

@ro0NL I don't see anything specific to this change, this PR makes it possible for the json: processor to return either an array or null, so it can still never be an int.

@mcfedr mcfedr deleted the json_env_any branch March 27, 2018 14:02
@fabpot fabpot mentioned this pull request May 7, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants