fix: preserve parent data context for primitive optional blocks#91
Open
yashhzd wants to merge 1 commit intoaccordproject:mainfrom
Open
Conversation
When an optional block guards a primitive value (string, number,
boolean), the runtime incorrectly navigated into the property scope,
causing named variables like {{age}} inside {{#optional age}} to
resolve as $['age']['age'] instead of $['age']. This also affected
the pre-processing step which tried to treat primitive values as
objects.
Changes:
- getJsonPath: skip path navigation for primitive OptionalDefinition
nodes so variables resolve from the parent data context
- generateOptionalBlocks: skip pre-processing for primitive optional
values and let the normal traverse handle them
- Update test templates to use named variables instead of {{this}}
for primitive optionals
- Add conditional-variables test template
Closes accordproject/template-playground#2
Signed-off-by: Yash Goel <162511050+yashhzd@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes a bug where named variables inside
{{#optional}}blocks with primitive types (string, number, boolean) would fail at runtime because the JSON path resolver incorrectly navigated into the property scope.Root cause:
OptionalDefinitionis classified as aNAVIGATION_NODE, sogetJsonPathwould append the property name to the path. For{{age}}inside{{#optional age}}, this produced$['age']['age']instead of the correct$['age']. Additionally,generateOptionalBlockstried to treat primitive values as objects during pre-processing.Closes accordproject/template-playground#2
Changes
TemplateMarkInterpreter.ts
getJsonPath— Skip path navigation forOptionalDefinitionnodes that guard primitive types (detected viaModelUtil.isPrimitiveType). This ensures variables inside primitive optional blocks resolve from the parent data context.generateOptionalBlocks— Skip pre-processing for primitive optional values (typeof value !== 'object'). Primitive values don't need recursive agreement generation — the normal traverse handles them correctly with the full parent data context.Test Updates
optional_comprehensive/template.mdandoptional-nested/template.mdto use named variables (e.g.,{{age}}) instead of{{this}}for primitive optional blocksconditional-variablestest template with model, template, and data filesAll 28 tests pass (27 existing + 1 new). The 3 pre-existing failures in
TemplateArchiveProcessor.test.tsare unrelated (missing--experimental-vm-modulesflag).Related
This fix works in conjunction with a companion PR on markdown-transform that addresses the type-checking (parsing) side of the same issue.
Author Checklist