Skip to content

fix(loader): preserve non-env {{...}} placeholders in ConfigLoader (#81)#132

Open
owen-pengtao wants to merge 1 commit into
contextforge-org:0.1.xfrom
owen-pengtao:fix/configloader-preserve-nonenv-placeholders
Open

fix(loader): preserve non-env {{...}} placeholders in ConfigLoader (#81)#132
owen-pengtao wants to merge 1 commit into
contextforge-org:0.1.xfrom
owen-pengtao:fix/configloader-preserve-nonenv-placeholders

Conversation

@owen-pengtao

Copy link
Copy Markdown

Summary

Fixes #81ConfigLoader.load_config blanks non-env {{...}} placeholders in the plugin config.

load_config renders the entire config.yaml through one jinja pass with the default (empty) Undefined:

jinja_env = SandboxedEnvironment(loader=jinja2.BaseLoader(), autoescape=True)
rendered_template = jinja_env.from_string(template).render(env=os.environ)

Because the render only defines env, any {{...}} that is not an env reference is silently rendered to "" at load time — including placeholders a plugin legitimately stores in its config to render itself later, at runtime. The reported symptom: WebhookNotification's default_template

{ "event": "{{event}}", "timestamp": "{{timestamp}}", "violation": {{violation}}, "metadata": {{metadata}} }

is blanked to { "event": "", "timestamp": "", "violation": , ... } at load, so the plugin posts an all-empty, invalid-JSON body.

Fix

Render with jinja2.DebugUndefined (issue #81's suggested option 1) so undefined names — everything except the env.* values passed to render() — survive verbatim as {{ name }} instead of being blanked, while env.* references are still substituted.

jinja_env = SandboxedEnvironment(loader=jinja2.BaseLoader(), autoescape=True, undefined=jinja2.DebugUndefined)

Behavior note

A bare {{ env.MISSING }} for an unset env var now renders literally as {{ env.MISSING }} instead of "". Configs that want an empty fallback should use the default filter ({{ env.X | default('') }}), which continues to work. This is the documented trade-off of the DebugUndefined approach.

Tests

Adds tests/unit/cpex/framework/loader/test_config_loader.py:

Verified against the real 0.1.x ConfigLoader: current behavior blanks the placeholders; with this change the three tests pass and the WebhookNotification-style default_template survives load.

Signed-off-by: Owen Peng tao.peng@tibco.com

…ontextforge-org#81)

ConfigLoader.load_config renders the whole plugin config.yaml through a single
jinja pass with the default (empty) Undefined, so any {{...}} that is not an env
reference is silently blanked to "". A plugin that stores a runtime template in
its config (e.g. WebhookNotification's default_template) is therefore emptied at
load time and later emits an all-empty, invalid body.

Render with jinja2.DebugUndefined so undefined names (everything except the
env.* values passed to render()) survive verbatim as {{ name }} instead of being
blanked, while env.* references are still substituted. Adds regression tests
covering env substitution, non-env placeholder preservation, and use_jinja=False.

Fixes contextforge-org#81.

Signed-off-by: Owen Peng <tao.peng@tibco.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant