Skip to content
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

Clarify Environment Variable mapping #774

Merged
merged 1 commit into from
Jun 29, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions documentation/src/main/docs/config/environment-variables.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Environment Variables

Environment variable names follow the conversion rules specified by [MicroProfile Config](https://github.com/eclipse/microprofile-config/blob/master/spec/src/main/asciidoc/configsources.asciidoc#default-configsources).
Environment Variable names follow the conversion rules specified by [MicroProfile Config](https://github.com/eclipse/microprofile-config/blob/master/spec/src/main/asciidoc/configsources.asciidoc#default-configsources).

SmallRye Config specifies additional conversion rules:

Expand All @@ -13,22 +13,38 @@ with `_`: `FOO__BAR__BAZ`

!!! danger

Environment variables format cannot represent the entire spectrum of common property names.
Environment Variables format cannot represent the entire spectrum of common property names.

By default, the underscore `_` of an environment variable name always maps to a dot `.`. If the property name
contains a dash or some other special character, that property name needs to be specified in one of the Config
sources. It will provide additional information to SmallRye Config on converting the environment variable name.
The lookup of configuration values from Environment Variables will always use the dotted format name. For
instance, the lookup of the Environment Variable `FOO_BAR` value, requires the property name `foo.bar`:

```java
ConfigProvider.getConfig().getValue("foo.bar", String.class);
```

When SmallRyeConfig performs the lookup on the Environment Variables Config Source, it applies the conversion rules to
find the matching property name and retrieve the value.

In some situations, looking up the exact property name is impossible. For instance, when SmallRye Config has to look up
a configuration that is part of a `Map`, and the property name contains a dynamic segment (a `Map` key). In this case,
SmallRye Config relies upon each source's list of property names. These must be converted back to their most likely
dotted format for Environment Variables.

By default, the underscore `_` of an Environment Variable name always maps to a dot `.`. If the property name
contains a dash or some other special character, that property name can be specified in another Config
Source, with the expected dotted format. It will provide additional information to SmallRye Config to perform a
two-way conversion and match the property names.

For instance:

_Config A_
**Config A**
```console
FOO_BAR_BAZ=VALUE
```

Will map to `foo.bar.baz` and value `value`.

_Config B_
**Config B**
```console
FOO_BAR_BAZ=VALUE
```
Expand All @@ -37,3 +53,9 @@ foo.bar-baz=default
```

Will map to `foo.bar-baz` and value `value`.

!!! note

The property name in dotted format needs to exist somewhere to provide this additional information. It can be set in a
low ordinal source, even without value. The Environment Variables source will override the value and map the correct
configuration name.