egoettelmann / spring-configuration-extensions

Generate Spring configuration properties metadata from `@Value` annotations
Apache License 2.0
5 stars 2 forks source link

Issues with booleans without explicit default value #17

Closed ciis0 closed 4 months ago

ciis0 commented 4 months ago

first, thank for this project!

in certain constellations, I am running into problems with boolean values during (html) report generation using the Maven plugin:

In that case then the aggregation seems to store the default value obtained from Java in metadata JSON as a actual boolean and then report generation fails with the error below.

A workaround is to declare default value in application.yml (not .yaml!) or application.properties, then it's stored as string in metadata and FreeMarker is happy.

I am open to look into providing a fix PR. :)

SEVERE: Error executing FreeMarker template
FreeMarker template error:
Can't convert boolean to string automatically, because the "boolean_format" setting was "true,false", which is the legacy deprecated default, and we treat it as if no format was set. This is the default configuration; you should provide the format explicitly for each place where you print a boolean.

----
Tip: Write something like myBool?string('yes', 'no') to specify boolean formatting in place.
----
Tip: If you want "true"/"false" result as you are generating computer-language output (not for direct human consumption), then use "?c", like ${myBool?c}. (If you always generate computer-language output, then it's might be reasonable to set the "boolean_format" setting to "c" instead.)
----
Tip: If you need the same two values on most places, the programmers can set the "boolean_format" setting to something like "yes,no". However, then it will be easy to unwillingly format booleans like that.
----

----
FTL stack trace ("~" means nesting-related):
    - Failed at: ${(property.defaultValue?j_string)!}  [in template "default.ftl" at line 113, column 35]
----

Java stack trace (for programmers):
----
freemarker.core._MiscTemplateException: [... Exception message was already printed; see it above ...]
    [...]
    at com.github.egoettelmann.spring.configuration.extensions.aggregator.maven.components.reporting.writers.HtmlReportWriter.write(HtmlReportWriter.java:60)
    at com.github.egoettelmann.spring.configuration.extensions.aggregator.maven.components.reporting.DefaultReportingService.save(DefaultReportingService.java:74)
    at com.github.egoettelmann.spring.configuration.extensions.aggregator.maven.ReporterMojo.execute(ReporterMojo.java:88)
[...]
egoettelmann commented 4 months ago

Many thanks for your detailed investigations and the suggested fix.

I slightly reworked your PR to prevent creating an additional sample module. The scenario has been integrated in Sample1.

The suggested fix looks good to me. Even if the setBooleanFormat is recommended by the Freemarker documentation to be left empty, it is quite reasonable to define it for the intended usage of this library .

Regarding your other comments/remarks:

If the changes performed on develop are fine for you, I will release a new version ASAP.

Again, many thanks for your input.

ciis0 commented 4 months ago

LGTM!

I am thinking about whether "Boolean value without any default value" doc comment could be confusing (because it does have an implicit default value, false). As the problem was with the type (actual Boolean, not String), maybe rather "Boolean value with native default"?

ciis0 commented 4 months ago

integrated with some modifications in develop.

ciis0 commented 4 months ago

oops, that close should have been the PR, not the issue... ^^

ciis0 commented 4 months ago

On which OS are you working ?

I am working in Ubuntu WSL.

``` cschulz@DESKTOP-...:~/IdeaProjects/spring-configuration-extensions [develop= 87edbd4]$2 uname -a Linux DESKTOP-... 5.15.153.1-microsoft-standard-WSL2 #1 SMP Fri Mar 29 23:14:13 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux cschulz@DESKTOP-...:~/IdeaProjects/spring-configuration-extensions [develop= 87edbd4]$2 mvn --version Apache Maven 3.9.5 (57804ffe001d7215b5e7bcb531cf83df38f93546) Maven home: /home/cschulz/.local/opt/apache-maven-3.9.5 Java version: 21.0.3, vendor: Ubuntu, runtime: /usr/lib/jvm/java-21-openjdk-amd64 Default locale: en_GB, platform encoding: UTF-8 OS name: "linux", version: "5.15.153.1-microsoft-standard-wsl2", arch: "amd64", family: "unix" cschulz@DESKTOP-...:~/IdeaProjects/spring-configuration-extensions [develop= 87edbd4]$2 tail -n+0 /etc/*-release ==> /etc/lsb-release <== DISTRIB_ID=Ubuntu DISTRIB_RELEASE=22.04 DISTRIB_CODENAME=jammy DISTRIB_DESCRIPTION="Ubuntu 22.04.3 LTS" ==> /etc/os-release <== PRETTY_NAME="Ubuntu 22.04.3 LTS" NAME="Ubuntu" VERSION_ID="22.04" VERSION="22.04.3 LTS (Jammy Jellyfish)" VERSION_CODENAME=jammy ID=ubuntu ID_LIKE=debian HOME_URL="https://www.ubuntu.com/" SUPPORT_URL="https://help.ubuntu.com/" BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/" PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy" UBUNTU_CODENAME=jammy ```
egoettelmann commented 4 months ago

I changed the Javadoc into: Boolean value without any explicit default value.

The fix is released and is available in version 0.2.1: https://central.sonatype.com/artifact/com.github.egoettelmann/spring-value-annotation-processor