Add ability to ignore configuration properties
Properties which should be ignored can be specified in the additional-spring-configuration-metadata.json file. The ignored properties section is copied into the final spring-configuration-metadata.json file, and the ignored properties are removed from the properties element in the final file. Closes gh-2421
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2024 the original author or authors.
|
||||
* Copyright 2012-2025 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.
|
||||
@@ -22,6 +22,7 @@ import java.time.temporal.ChronoUnit;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.boot.configurationprocessor.metadata.ConfigurationMetadata;
|
||||
import org.springframework.boot.configurationprocessor.metadata.ItemIgnore;
|
||||
import org.springframework.boot.configurationprocessor.metadata.ItemMetadata;
|
||||
import org.springframework.boot.configurationprocessor.metadata.Metadata;
|
||||
import org.springframework.boot.configurationsample.deprecation.Dbcp2Configuration;
|
||||
@@ -38,6 +39,7 @@ import org.springframework.boot.configurationsample.simple.DescriptionProperties
|
||||
import org.springframework.boot.configurationsample.simple.HierarchicalProperties;
|
||||
import org.springframework.boot.configurationsample.simple.HierarchicalPropertiesGrandparent;
|
||||
import org.springframework.boot.configurationsample.simple.HierarchicalPropertiesParent;
|
||||
import org.springframework.boot.configurationsample.simple.IgnoredProperties;
|
||||
import org.springframework.boot.configurationsample.simple.InnerClassWithPrivateConstructor;
|
||||
import org.springframework.boot.configurationsample.simple.NotAnnotated;
|
||||
import org.springframework.boot.configurationsample.simple.SimpleArrayProperties;
|
||||
@@ -570,4 +572,24 @@ class ConfigurationMetadataAnnotationProcessorTests extends AbstractMetadataGene
|
||||
.withDescription("last description in Javadoc"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldIgnoreProperties() {
|
||||
String additionalMetadata = """
|
||||
{
|
||||
"ignored": {
|
||||
"properties": [
|
||||
{
|
||||
"name": "ignored.prop3"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
""";
|
||||
ConfigurationMetadata metadata = compile(additionalMetadata, IgnoredProperties.class);
|
||||
assertThat(metadata).has(Metadata.withProperty("ignored.prop1", String.class));
|
||||
assertThat(metadata).has(Metadata.withProperty("ignored.prop2", String.class));
|
||||
assertThat(metadata).doesNotHave(Metadata.withProperty("ignored.prop3", String.class));
|
||||
assertThat(metadata.getIgnored()).containsExactly(ItemIgnore.forProperty("ignored.prop3"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2024 the original author or authors.
|
||||
* Copyright 2012-2025 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.
|
||||
@@ -34,6 +34,7 @@ import static org.assertj.core.api.Assertions.assertThatException;
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Stephane Nicoll
|
||||
* @author Moritz Halbritter
|
||||
*/
|
||||
class JsonMarshallerTests {
|
||||
|
||||
@@ -174,14 +175,36 @@ class JsonMarshallerTests {
|
||||
"\"java.lang.Boolean\"", "\"com.example.bravo.aaa\"", "\"java.lang.Integer\"", "\"com.example.Bar");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReadIgnoredProperties() throws Exception {
|
||||
String json = """
|
||||
{
|
||||
"ignored": {
|
||||
"properties": [
|
||||
{
|
||||
"name": "prop1"
|
||||
},
|
||||
{
|
||||
"name": "prop2"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
""";
|
||||
ConfigurationMetadata metadata = read(json);
|
||||
assertThat(metadata.getIgnored()).containsExactly(ItemIgnore.forProperty("prop1"),
|
||||
ItemIgnore.forProperty("prop2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldCheckRootFields() {
|
||||
String json = """
|
||||
{
|
||||
"groups": [], "properties": [], "hints": [], "dummy": []
|
||||
"groups": [], "properties": [], "hints": [], "ignored": {}, "dummy": []
|
||||
}""";
|
||||
assertThatException().isThrownBy(() -> read(json))
|
||||
.withMessage("Expected only keys [groups, hints, properties], but found additional keys [dummy]. Path: .");
|
||||
.withMessage(
|
||||
"Expected only keys [groups, hints, ignored, properties], but found additional keys [dummy]. Path: .");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -324,9 +347,41 @@ class JsonMarshallerTests {
|
||||
"Expected only keys [name, parameters], but found additional keys [dummy]. Path: .hints.[0].providers.[0]");
|
||||
}
|
||||
|
||||
private void read(String json) throws Exception {
|
||||
@Test
|
||||
void shouldCheckIgnoreFields() {
|
||||
String json = """
|
||||
{
|
||||
"ignored": {
|
||||
"properties": [],
|
||||
"dummy": {}
|
||||
}
|
||||
}
|
||||
""";
|
||||
assertThatException().isThrownBy(() -> read(json))
|
||||
.withMessage("Expected only keys [properties], but found additional keys [dummy]. Path: .ignored");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldCheckIgnorePropertiesFields() {
|
||||
String json = """
|
||||
{
|
||||
"ignored": {
|
||||
"properties": [
|
||||
{
|
||||
"name": "prop1",
|
||||
"dummy": true
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
""";
|
||||
assertThatException().isThrownBy(() -> read(json))
|
||||
.withMessage("Expected only keys [name], but found additional keys [dummy]. Path: .ignored.properties.[0]");
|
||||
}
|
||||
|
||||
private ConfigurationMetadata read(String json) throws Exception {
|
||||
JsonMarshaller marshaller = new JsonMarshaller();
|
||||
marshaller.read(new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8)));
|
||||
return marshaller.read(new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8)));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Copyright 2012-2025 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.configurationsample.simple;
|
||||
|
||||
import org.springframework.boot.configurationsample.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* Configuration properties where some of them are being ignored.
|
||||
*
|
||||
* @author Moritz Halbritter
|
||||
*/
|
||||
@ConfigurationProperties("ignored")
|
||||
public class IgnoredProperties {
|
||||
|
||||
private String prop1;
|
||||
|
||||
private String prop2;
|
||||
|
||||
private String prop3;
|
||||
|
||||
public String getProp1() {
|
||||
return this.prop1;
|
||||
}
|
||||
|
||||
public void setProp1(String prop1) {
|
||||
this.prop1 = prop1;
|
||||
}
|
||||
|
||||
public String getProp2() {
|
||||
return this.prop2;
|
||||
}
|
||||
|
||||
public void setProp2(String prop2) {
|
||||
this.prop2 = prop2;
|
||||
}
|
||||
|
||||
public String getProp3() {
|
||||
return this.prop3;
|
||||
}
|
||||
|
||||
public void setProp3(String prop3) {
|
||||
this.prop3 = prop3;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user