Skip to content

Failure to discover default value for a primitive should not lead to document its default value #46551

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ private Object getValue(String variableType, ExpressionTree expression, Object d
}
return null;
}
return defaultValue;
return null;
}

private Object getFactoryValue(ExpressionTree expression, Object factoryValue) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ private void putHintValue(JSONObject jsonObject, Object value) throws Exception
}

private void putDefaultValue(JSONObject jsonObject, Object value) throws Exception {
if (value == null) {
return;
}
Object defaultValue = extractItemValue(value);
jsonObject.put("defaultValue", defaultValue);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.springframework.boot.configurationprocessor.metadata.Metadata;
import org.springframework.boot.configurationprocessor.test.CompiledMetadataReader;
import org.springframework.boot.configurationprocessor.test.TestConfigurationMetadataAnnotationProcessor;
import org.springframework.boot.configurationsample.arithmetic.ArithmeticExpressionProperties;
import org.springframework.boot.configurationsample.deprecation.Dbcp2Configuration;
import org.springframework.boot.configurationsample.method.NestedPropertiesMethod;
import org.springframework.boot.configurationsample.record.ExampleRecord;
Expand Down Expand Up @@ -118,6 +119,7 @@
* @author Pavel Anisimov
* @author Scott Frederick
* @author Moritz Halbritter
* @author Hyeon Jae Kim
*/
class ConfigurationMetadataAnnotationProcessorTests extends AbstractMetadataGenerationTests {

Expand Down Expand Up @@ -627,6 +629,21 @@ void shouldIgnoreProperties() {
assertThat(metadata.getIgnored()).containsExactly(ItemIgnore.forProperty("ignored.prop3"));
}

@Test
void arithmeticExpressionPropertiesShouldOmitUnknownDefaultValues(){
ConfigurationMetadata metadata = compile(ArithmeticExpressionProperties.class);
assertThat(metadata).has(Metadata.withProperty("arithmetic.calculated", Integer.class)
.fromSource(ArithmeticExpressionProperties.class));
assertThat(metadata).has(Metadata.withProperty("arithmetic.literal", Integer.class)
.fromSource(ArithmeticExpressionProperties.class)
.withDefaultValue(100));
assertThat(metadata).has(Metadata.withProperty("arithmetic.simple-flag", Boolean.class)
.fromSource(ArithmeticExpressionProperties.class)
.withDefaultValue(true));
assertThat(metadata).has(Metadata.withProperty("arithmetic.complex-flag", Boolean.class)
.fromSource(ArithmeticExpressionProperties.class));
}

@Nested
class SourceTests {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright 2012-present 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.arithmetic;

import org.springframework.boot.configurationsample.ConfigurationProperties;

/**
* Configuration properties with arithmetic expressions to test metadata generation for
* values that cannot be inferred at compile time. Used to verify that primitive types
* with unknown default values omit the defaultValue field in the generated metadata.
*
* @author Hyeon Jae Kim
*/
@ConfigurationProperties(prefix = "arithmetic")
public class ArithmeticExpressionProperties {

/**
* A value calculated using arithmetic expression that cannot be inferred.
*/
private int calculated = 10 * 10;

/**
* A literal value that can be inferred.
*/
private int literal = 100;

/**
* A boolean expression that cannot be inferred.
*/
private boolean complexFlag = !false;

/**
* A simple boolean literal.
*/
private boolean simpleFlag = true;

public int getCalculated() {
return this.calculated;
}

public void setCalculated(int calculated) {
this.calculated = calculated;
}

public int getLiteral() {
return this.literal;
}

public void setLiteral(int literal) {
this.literal = literal;
}

public boolean isComplexFlag() {
return this.complexFlag;
}

public void setComplexFlag(boolean complexFlag) {
this.complexFlag = complexFlag;
}

public boolean isSimpleFlag() {
return this.simpleFlag;
}

public void setSimpleFlag(boolean simpleFlag) {
this.simpleFlag = simpleFlag;
}

}