Skip to content

Replace SpringDeclarativeConfigProperties copy with a coercing decorator - #20587

Merged
trask merged 7 commits into
open-telemetry:mainfrom
colamong:feat/spring-declarative-config-decorator
Oct 15, 2026
Merged

trask merged 7 commits into
open-telemetry:mainfrom
colamong:feat/spring-declarative-config-decorator

Conversation

@colamong

@colamong colamong commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Resolves #20433.

SpringDeclarativeConfigProperties was a full copy of YamlDeclarativeConfigProperties with coercing getters, kept because wrapping the real one logged a type-mismatch warning whenever a getter guessed the wrong type (TODO: Revisit after open-telemetry/opentelemetry-java#8101). SDK 1.67 adds is<Type> predicates (open-telemetry/opentelemetry-java#8883), so this replaces the copy with a decorator.

How it works

  • Storage, structure, getPropertyKeys and getComponentLoader come from YamlDeclarativeConfigProperties.
  • Scalar getters ask the delegate what it holds via is<Type> and read only that, then convert, so the delegate never logs a mismatch.
  • Scalar list elements have no predicate, so the decorator keeps the values it was created from and getScalarList converts each element from its original type with the same rules as the scalar getters.
  • getStructured / getStructuredList wrap children, so nested config converts too.
  • Conversion follows the DeclarativeConfigProperties javadoc: Boolean.parseBoolean rules for booleans, a native Long narrowed with Long.intValue() for getInt (Strings still parsed with Integer.parseInt), numeric strings parsed, any scalar to String, Long widened to Double, and unconvertible list elements dropped. The old copy differed on the first two (it returned null for non-true/false strings and for out-of-int-range native Long values); that is aligned here per review.

Testing

The first commit extends SpringDeclarativeConfigPropertiesTest and passes against the old copy; later commits change behavior only where review aligned it with the javadoc, as listed above. I also ran a throwaway differential check of the old copy against the decorator over 534 getter/input combinations: every difference is one of those javadoc alignments, and YamlDeclarativeConfigProperties logged nothing. Coverage:

  • string and typed scalars in both directions, null and missing keys, list element conversion, and nested maps and lists of maps
  • an @AfterEach check that YamlDeclarativeConfigProperties logs no warning in any test

:instrumentation:spring:spring-boot-autoconfigure:check passes.

@colamong
colamong requested a review from a team as a code owner October 14, 2026 01:29
Uses the is<Type> predicates added in opentelemetry-java#8883 to check
what YamlDeclarativeConfigProperties holds before reading it, so the
delegate does not log type mismatches for values this class converts.
Scalar list elements are stored as Strings on creation, since list
elements have no such predicate.
@colamong
colamong force-pushed the feat/spring-declarative-config-decorator branch from 2994cc3 to d685d3b Compare October 14, 2026 01:31
@opentelemetry-pr-dashboard

opentelemetry-pr-dashboard Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

Pull request dashboard status

Merged · refreshed 2026-10-15 17:24 UTC

Status above doesn't look right?
  • Anything look wrong? Report it with what you expected; it helps us improve the dashboard.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The decorator exposes a mutable structured-list result where the previous implementation returned an unmodifiable collection.

1 open finding
What changed in this PR

Replaces the copied YAML configuration implementation with a coercing decorator backed by the SDK implementation.

Changes:

  • Delegates storage and structure handling to YamlDeclarativeConfigProperties.
  • Preserves scalar coercion and adds nested/list coverage.
  • Verifies coercion does not emit type-mismatch warnings.
File Description
SpringDeclarativeConfigProperties.java Implements the coercing decorator.
SpringDeclarativeConfigPropertiesTest.java Expands coercion and warning tests.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

The decorator preserves tested behavior while removing duplicated SDK implementation code.

0 open findings

1 resolved since last review

🧠 Review effort: Balanced

private static Double doubleOrNull(@Nullable Object value) {
if (value instanceof Double) {
return (Double) value;
private static Boolean parseBoolean(@Nullable String value) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

javadoc of getBoolean says that

Implementations should use the same rules as {@link Boolean#parseBoolean(String)} for handling the values.

Boolean#parseBoolean is implemented as return "true".equalsIgnoreCase(s);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks — switched to Boolean.parseBoolean in f0261a6, for lists too. That makes any non-"true" string a valid false, so I also flipped the isBoolean("string_key_with_env_quoted") assertion in DeclarativeConfigTest (added with the 1.67 bump) to match the contract.

return (int) longValue;
}
Long value = getLong(name);
if (value == null || value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

javadoc says that

If the underlying config property is {@link Long}, it is converted to {@link Integer} with {@link Long#intValue()} which may result in loss of precision.

based on that the range check shouldn't be necessary.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed — removed the range check in f0261a6; a Long is now narrowed with Long.intValue() as documented.

getBoolean uses Boolean.parseBoolean rules and getInt narrows a Long
with Long.intValue(), as the interface documents.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

The decorator preserves the required configuration behavior and is comprehensively covered by focused tests.

0 open findings

🧠 Review effort: Balanced

@trask trask left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks!

AI Findings

}
return null;
Long value = getLong(name);
return value == null ? null : value.intValue();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

[P2] [Config] String-to-int conversion now silently wraps out-of-range values.

Routing every value through getLong() before narrowing means a Spring-resolved String "4294967296" now produces getInt() == 0 and isInt() == true. Previously, Integer.parseInt() rejected it, allowing the configured default to apply. The SDK Javadoc permits narrowing an underlying Long, but this also narrows Strings that are not valid integers.

Please delegate native integer/Long reads to delegate.getInt() and retain Integer.parseInt() for Strings. Add positive and negative String-overflow cases alongside the existing native-Long test.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Right — I applied the Long narrowing too broadly. Fixed in 5f1f90f: native Integer/Long go through delegate.getInt(), Strings still use Integer.parseInt(). Added "2147483648" / "-2147483649" (null, isInt false) plus the MAX_VALUE / MIN_VALUE boundaries next to the native-Long case.

}
List<?> list = (List<?>) value;
if (list.stream().allMatch(SpringDeclarativeConfigProperties::isScalar)) {
return list.stream().map(String::valueOf).collect(toList());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

[P2] [Config] Stringifying list elements turns native numbers into booleans.

This discards every scalar list element's original type. Reading [true, 42, false] as a Boolean list now returns [true, false, false]; previously, the numeric element was dropped. A numeric-only list now returns false entries instead of null, preventing default-list fallback. This affects typed values supplied by declarative model customizers and disagrees with the scalar getter, which still rejects numeric-to-Boolean conversion.

Please preserve original element types for typed-list conversion and apply Boolean.parseBoolean() only to originally String-valued elements. Add coverage for mixed Boolean/numeric lists and numeric-only lists.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed, stringifying lost the element types. Fixed in 5f1f90f: the decorator keeps the values it was created from and getScalarList converts each element with the same rules as the scalar getters, so Boolean.parseBoolean applies only to String elements. Added [true, 42, false] → [true, false] and [1, 2] → null.

getInt narrows only a native Long and still parses Strings with
Integer.parseInt, so an out-of-range String falls back to the default.
Scalar lists convert from the original values rather than stringified
copies, so Boolean.parseBoolean applies only to String elements.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No blocking issues remain, and regression coverage addresses the intentional conversion changes and resolved collection-contract issue.

0 open findings

🧠 Review effort: Balanced

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

The implementation and tests support the intended conversion behavior, with no unresolved blocking findings.

0 open findings

🧠 Review effort: Balanced

Comment on lines +246 to +249
if (element instanceof Number) {
return ((Number) element).doubleValue();
}
return element instanceof String ? parseDouble((String) element) : null;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

maybe it would be clearer if this was surrounded with if (scalarType == Double.class) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed — done in 8aac25a; the fall-through now returns null, which getScalarList never reaches since it checks the supported types first.

@laurit laurit added this to the v3.0.0 milestone Oct 15, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

The focused refactor covers intentional behavior changes and resolves the prior structured-list mutability issue, with no unresolved findings.

0 open findings

🧠 Review effort: Balanced

@trask
trask merged commit ba817d5 into open-telemetry:main Oct 15, 2026
69 checks passed
@otelbot

otelbot Bot commented Oct 15, 2026

Copy link
Copy Markdown
Contributor

Thank you for your contribution @colamong! 🎉 We would like to hear from you about your experience contributing to OpenTelemetry by taking a few minutes to fill out this survey.

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.

Replace SpringDeclarativeConfigProperties copy with a decorator once SDK ships is<Type>

4 participants