Skip to content

Conversation

@wied03
Copy link
Contributor

@wied03 wied03 commented Sep 15, 2025

When a JSON FTL is specified, detect which variables are sent to render the template vs. which are used.

This eliminates a rather large amount of cruft in applications using this for tests.

@wied03 wied03 requested review from bhalsey and robotdan September 15, 2025 18:32
build.savant Outdated
testngVersion = "7.8.0"

project(group: "org.primeframework", name: "prime-mvc", version: "5.5.1", licenses: ["ApacheV2_0"]) {
project(group: "org.primeframework", name: "prime-mvc", version: "5.6.1", licenses: ["ApacheV2_0"]) {
Copy link
Member

Choose a reason for hiding this comment

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

If we bump the minor version, assuming we want to reset the patch to 0 to make this 5.6.0?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes - 709459f

* @throws IOException If the template could not be loaded, parsed or executed.
*/
public static String processTemplateWithMap(Path path, Map<String, Object> values, boolean createMissingTemplates)
public static String processTemplateWithMap(Path path, DetectionMap values, boolean createMissingTemplates)
Copy link
Member

Choose a reason for hiding this comment

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

Looks like we are not using createMissingTemplates and just always creating the new file, so we could delete this arg?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep - b791241

Set<Object> unusedVariables = values.getUnusedVariables("_",
"actual");
if (!unusedVariables.isEmpty()) {
throw new IllegalArgumentException("Variables %s are not used in the [%s] template. If it's acceptable for the variable to not be used, wrap it in an Optional".formatted(unusedVariables.stream()
Copy link
Member

Choose a reason for hiding this comment

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

Suggestion:

Unused values [%s] found in the [%s] template. If it's acceptable for the variable to be unused, wrap it in an Optional."

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Pushed as f5963f6


// if an Optional is used in a template variable, we don't want it to be considered an unused value
// and we want to evaluate it so FreeMarker does not have to
if (value instanceof Optional<?> opt && opt.isPresent()) {
Copy link
Member

Choose a reason for hiding this comment

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

What is the use case for passing in a parameter to BodyTools that we don't expect in the template? Should we just fail to force the test author to use the correct template?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are a couple cases, like data driven tests. Rather than force the tests to really be re-written. my first offer is, if the test author wraps the value in Optional, then this doesn't complain if that value isn't used since it might be used on the next permutation. I think that's a decent compromise. It still catches cruft. but if you deliberately say "no i want this" by using Optional, it leaves you alone.

@wied03 wied03 requested a review from robotdan September 15, 2025 18:59
@wied03 wied03 merged commit 3eef5df into main Sep 15, 2025
1 check failed
@wied03 wied03 deleted the wied03/ENG-2375/unused_vars branch September 15, 2025 19:26
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.

4 participants