Browse Source

Fix subName from adapted name with value processor

Previously, when a configuration property name was created by
adapting a source with a value processor, creating sub names from
that property name did not work correctly. This broke binding of
prefixed environment variables to a map as the ancestor checking
did not work.

Fixes gh-43304
pull/43333/head
Andy Wilkinson 1 year ago
parent
commit
7bc709c32f
  1. 10
      spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertyName.java
  2. 18
      spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/ConfigurationPropertyNameTests.java

10
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertyName.java

@ -773,7 +773,7 @@ public final class ConfigurationPropertyName implements Comparable<Configuration @@ -773,7 +773,7 @@ public final class ConfigurationPropertyName implements Comparable<Configuration
ElementType[] type = new ElementType[size];
System.arraycopy(this.type, 0, type, 0, this.size);
System.arraycopy(additional.type, 0, type, this.size, additional.size);
CharSequence[] resolved = newResolved(size);
CharSequence[] resolved = newResolved(0, size);
for (int i = 0; i < additional.size; i++) {
resolved[this.size + i] = additional.get(i);
}
@ -781,13 +781,13 @@ public final class ConfigurationPropertyName implements Comparable<Configuration @@ -781,13 +781,13 @@ public final class ConfigurationPropertyName implements Comparable<Configuration
}
Elements chop(int size) {
CharSequence[] resolved = newResolved(size);
CharSequence[] resolved = newResolved(0, size);
return new Elements(this.source, size, this.start, this.end, this.type, resolved);
}
Elements subElements(int offset) {
int size = this.size - offset;
CharSequence[] resolved = newResolved(size);
CharSequence[] resolved = newResolved(offset, size);
int[] start = new int[size];
System.arraycopy(this.start, offset, start, 0, size);
int[] end = new int[size];
@ -797,10 +797,10 @@ public final class ConfigurationPropertyName implements Comparable<Configuration @@ -797,10 +797,10 @@ public final class ConfigurationPropertyName implements Comparable<Configuration
return new Elements(this.source, size, start, end, type, resolved);
}
private CharSequence[] newResolved(int size) {
private CharSequence[] newResolved(int offset, int size) {
CharSequence[] resolved = new CharSequence[size];
if (this.resolved != null) {
System.arraycopy(this.resolved, 0, resolved, 0, Math.min(size, this.size));
System.arraycopy(this.resolved, offset, resolved, 0, Math.min(size, this.size));
}
return resolved;
}

18
spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/ConfigurationPropertyNameTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -19,6 +19,7 @@ package org.springframework.boot.context.properties.source; @@ -19,6 +19,7 @@ package org.springframework.boot.context.properties.source;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import org.junit.jupiter.api.Test;
@ -492,6 +493,21 @@ class ConfigurationPropertyNameTests { @@ -492,6 +493,21 @@ class ConfigurationPropertyNameTests {
assertThat(name.subName(2)).hasToString("baz");
}
@Test
void subNameOfAdaptedNameWhenOffsetLessThanSizeShouldReturnSubName() {
ConfigurationPropertyName name = ConfigurationPropertyName.adapt("MY_LOGGING_LEVEL_ONE", '_');
assertThat(name.subName(1)).hasToString("logging.level.one");
assertThat(name.subName(2)).hasToString("level.one");
}
@Test
void subNameOfAdaptedNameWithValueProcessorWhenOffsetLessThanSizeShouldReturnSubName() {
ConfigurationPropertyName name = ConfigurationPropertyName.adapt("MY_LOGGING_LEVEL_ONE", '_',
(value) -> value.toString().toLowerCase(Locale.ENGLISH));
assertThat(name.subName(1)).hasToString("logging.level.one");
assertThat(name.subName(2)).hasToString("level.one");
}
@Test
void subNameWhenOffsetZeroShouldReturnName() {
ConfigurationPropertyName name = ConfigurationPropertyName.of("foo.bar.baz");

Loading…
Cancel
Save