Skip to content
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
@@ -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.
Expand All @@ -23,6 +23,7 @@
import java.util.Map;

import org.springframework.boot.configurationprocessor.json.JSONArray;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.boot.configurationprocessor.metadata.ItemMetadata.ItemType;

Expand All @@ -36,7 +37,7 @@ class JsonConverter {

private static final ItemMetadataComparator ITEM_COMPARATOR = new ItemMetadataComparator();

JSONArray toJsonArray(ConfigurationMetadata metadata, ItemType itemType) throws Exception {
JSONArray toJsonArray(ConfigurationMetadata metadata, ItemType itemType) throws JSONException {
JSONArray jsonArray = new JSONArray();
List<ItemMetadata> items = metadata.getItems()
.stream()
Expand All @@ -51,15 +52,15 @@ JSONArray toJsonArray(ConfigurationMetadata metadata, ItemType itemType) throws
return jsonArray;
}

JSONArray toJsonArray(Collection<ItemHint> hints) throws Exception {
JSONArray toJsonArray(Collection<ItemHint> hints) throws JSONException {
JSONArray jsonArray = new JSONArray();
for (ItemHint hint : hints) {
jsonArray.put(toJsonObject(hint));
}
return jsonArray;
}

JSONObject toJsonObject(ItemMetadata item) throws Exception {
JSONObject toJsonObject(ItemMetadata item) throws JSONException {
JSONObject jsonObject = new JSONObject();
jsonObject.put("name", item.getName());
jsonObject.putOpt("type", item.getType());
Expand All @@ -73,25 +74,30 @@ JSONObject toJsonObject(ItemMetadata item) throws Exception {
ItemDeprecation deprecation = item.getDeprecation();
if (deprecation != null) {
jsonObject.put("deprecated", true); // backward compatibility
JSONObject deprecationJsonObject = new JSONObject();
if (deprecation.getLevel() != null) {
deprecationJsonObject.put("level", deprecation.getLevel());
}
if (deprecation.getReason() != null) {
deprecationJsonObject.put("reason", deprecation.getReason());
}
if (deprecation.getReplacement() != null) {
deprecationJsonObject.put("replacement", deprecation.getReplacement());
}
if (deprecation.getSince() != null) {
deprecationJsonObject.put("since", deprecation.getSince());
}
JSONObject deprecationJsonObject = getDeprecationJsonObject(deprecation);
jsonObject.put("deprecation", deprecationJsonObject);
}
return jsonObject;
}

private JSONObject toJsonObject(ItemHint hint) throws Exception {
private static JSONObject getDeprecationJsonObject(ItemDeprecation deprecation) throws JSONException {
JSONObject deprecationJsonObject = new JSONObject();
if (deprecation.getLevel() != null) {
deprecationJsonObject.put("level", deprecation.getLevel());
}
if (deprecation.getReason() != null) {
deprecationJsonObject.put("reason", deprecation.getReason());
}
if (deprecation.getReplacement() != null) {
deprecationJsonObject.put("replacement", deprecation.getReplacement());
}
if (deprecation.getSince() != null) {
deprecationJsonObject.put("since", deprecation.getSince());
}
return deprecationJsonObject;
}

private JSONObject toJsonObject(ItemHint hint) throws JSONException {
JSONObject jsonObject = new JSONObject();
jsonObject.put("name", hint.getName());
if (!hint.getValues().isEmpty()) {
Expand All @@ -103,30 +109,30 @@ private JSONObject toJsonObject(ItemHint hint) throws Exception {
return jsonObject;
}

private JSONArray getItemHintValues(ItemHint hint) throws Exception {
private JSONArray getItemHintValues(ItemHint hint) throws JSONException {
JSONArray values = new JSONArray();
for (ItemHint.ValueHint value : hint.getValues()) {
values.put(getItemHintValue(value));
}
return values;
}

private JSONObject getItemHintValue(ItemHint.ValueHint value) throws Exception {
private JSONObject getItemHintValue(ItemHint.ValueHint value) throws JSONException {
JSONObject result = new JSONObject();
putHintValue(result, value.getValue());
result.putOpt("description", value.getDescription());
return result;
}

private JSONArray getItemHintProviders(ItemHint hint) throws Exception {
private JSONArray getItemHintProviders(ItemHint hint) throws JSONException {
JSONArray providers = new JSONArray();
for (ItemHint.ValueProvider provider : hint.getProviders()) {
providers.put(getItemHintProvider(provider));
}
return providers;
}

private JSONObject getItemHintProvider(ItemHint.ValueProvider provider) throws Exception {
private JSONObject getItemHintProvider(ItemHint.ValueProvider provider) throws JSONException {
JSONObject result = new JSONObject();
result.put("name", provider.getName());
if (provider.getParameters() != null && !provider.getParameters().isEmpty()) {
Expand All @@ -139,12 +145,12 @@ private JSONObject getItemHintProvider(ItemHint.ValueProvider provider) throws E
return result;
}

private void putHintValue(JSONObject jsonObject, Object value) throws Exception {
private void putHintValue(JSONObject jsonObject, Object value) throws JSONException {
Object hintValue = extractItemValue(value);
jsonObject.put("value", hintValue);
}

private void putDefaultValue(JSONObject jsonObject, Object value) throws Exception {
private void putDefaultValue(JSONObject jsonObject, Object value) throws JSONException {
Object defaultValue = extractItemValue(value);
jsonObject.put("defaultValue", defaultValue);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.junit.jupiter.api.Test;

import org.springframework.boot.configurationprocessor.json.JSONArray;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.boot.configurationprocessor.metadata.ConfigurationMetadata;
import org.springframework.boot.configurationprocessor.metadata.ItemDeprecation;
Expand All @@ -47,7 +48,7 @@
class MergeMetadataGenerationTests extends AbstractMetadataGenerationTests {

@Test
void mergingOfAdditionalProperty() throws Exception {
void mergingOfAdditionalProperty() throws JSONException {
ItemMetadata property = ItemMetadata.newProperty(null, "foo", "java.lang.String",
AdditionalMetadata.class.getName(), null, null, null, null);
String additionalMetadata = buildAdditionalMetadata(property);
Expand All @@ -57,7 +58,7 @@ void mergingOfAdditionalProperty() throws Exception {
}

@Test
void mergingOfAdditionalPropertyMatchingGroup() throws Exception {
void mergingOfAdditionalPropertyMatchingGroup() throws JSONException {
ItemMetadata property = ItemMetadata.newProperty(null, "simple", "java.lang.String", null, null, null, null,
null);
String additionalMetadata = buildAdditionalMetadata(property);
Expand All @@ -67,7 +68,7 @@ void mergingOfAdditionalPropertyMatchingGroup() throws Exception {
}

@Test
void mergeExistingPropertyDefaultValue() throws Exception {
void mergeExistingPropertyDefaultValue() throws JSONException {
ItemMetadata property = ItemMetadata.newProperty("simple", "flag", null, null, null, null, true, null);
String additionalMetadata = buildAdditionalMetadata(property);
ConfigurationMetadata metadata = compile(additionalMetadata, SimpleProperties.class);
Expand All @@ -80,7 +81,7 @@ void mergeExistingPropertyDefaultValue() throws Exception {
}

@Test
void mergeExistingPropertyWithSeveralCandidates() throws Exception {
void mergeExistingPropertyWithSeveralCandidates() throws JSONException {
ItemMetadata property = ItemMetadata.newProperty("simple", "flag", Boolean.class.getName(), null, null, null,
true, null);
String additionalMetadata = buildAdditionalMetadata(property);
Expand Down Expand Up @@ -111,7 +112,7 @@ void mergeExistingPropertyWithSeveralCandidates() throws Exception {
}

@Test
void mergeExistingPropertyDescription() throws Exception {
void mergeExistingPropertyDescription() throws JSONException {
ItemMetadata property = ItemMetadata.newProperty("simple", "comparator", null, null, null, "A nice comparator.",
null, null);
String additionalMetadata = buildAdditionalMetadata(property);
Expand All @@ -123,7 +124,7 @@ void mergeExistingPropertyDescription() throws Exception {
}

@Test
void mergeExistingPropertyDeprecation() throws Exception {
void mergeExistingPropertyDeprecation() throws JSONException {
ItemMetadata property = ItemMetadata.newProperty("simple", "comparator", null, null, null, null, null,
new ItemDeprecation("Don't use this.", "simple.complex-comparator", "1.2.3", "error"));
String additionalMetadata = buildAdditionalMetadata(property);
Expand All @@ -135,7 +136,7 @@ void mergeExistingPropertyDeprecation() throws Exception {
}

@Test
void mergeExistingPropertyDeprecationOverride() throws Exception {
void mergeExistingPropertyDeprecationOverride() throws JSONException {
ItemMetadata property = ItemMetadata.newProperty("singledeprecated", "name", null, null, null, null, null,
new ItemDeprecation("Don't use this.", "single.name", "1.2.3"));
String additionalMetadata = buildAdditionalMetadata(property);
Expand All @@ -147,7 +148,7 @@ void mergeExistingPropertyDeprecationOverride() throws Exception {
}

@Test
void mergeExistingPropertyDeprecationOverrideLevel() throws Exception {
void mergeExistingPropertyDeprecationOverrideLevel() throws JSONException {
ItemMetadata property = ItemMetadata.newProperty("singledeprecated", "name", null, null, null, null, null,
new ItemDeprecation(null, null, null, "error"));
String additionalMetadata = buildAdditionalMetadata(property);
Expand All @@ -167,7 +168,7 @@ void mergeOfInvalidAdditionalMetadata() {
}

@Test
void mergingOfSimpleHint() throws Exception {
void mergingOfSimpleHint() throws JSONException {
String hints = buildAdditionalHints(ItemHint.newHint("simple.the-name",
new ItemHint.ValueHint("boot", "Bla bla"), new ItemHint.ValueHint("spring", null)));
ConfigurationMetadata metadata = compile(hints, SimpleProperties.class);
Expand All @@ -181,7 +182,7 @@ void mergingOfSimpleHint() throws Exception {
}

@Test
void mergingOfHintWithNonCanonicalName() throws Exception {
void mergingOfHintWithNonCanonicalName() throws JSONException {
String hints = buildAdditionalHints(
ItemHint.newHint("simple.theName", new ItemHint.ValueHint("boot", "Bla bla")));
ConfigurationMetadata metadata = compile(hints, SimpleProperties.class);
Expand All @@ -194,7 +195,7 @@ void mergingOfHintWithNonCanonicalName() throws Exception {
}

@Test
void mergingOfHintWithProvider() throws Exception {
void mergingOfHintWithProvider() throws JSONException {
String hints = buildAdditionalHints(new ItemHint("simple.theName", Collections.emptyList(),
Arrays.asList(new ItemHint.ValueProvider("first", Collections.singletonMap("target", "org.foo")),
new ItemHint.ValueProvider("second", null))));
Expand All @@ -209,7 +210,7 @@ void mergingOfHintWithProvider() throws Exception {
}

@Test
void mergingOfAdditionalDeprecation() throws Exception {
void mergingOfAdditionalDeprecation() throws JSONException {
String deprecations = buildPropertyDeprecations(
ItemMetadata.newProperty("simple", "wrongName", "java.lang.String", null, null, null, null,
new ItemDeprecation("Lame name.", "simple.the-name", "1.2.3")));
Expand All @@ -219,7 +220,7 @@ void mergingOfAdditionalDeprecation() throws Exception {
}

@Test
void mergingOfAdditionalMetadata() throws Exception {
void mergingOfAdditionalMetadata() throws JSONException {
JSONObject property = new JSONObject();
property.put("name", "foo");
property.put("type", "java.lang.String");
Expand All @@ -234,7 +235,7 @@ void mergingOfAdditionalMetadata() throws Exception {
assertThat(metadata).has(Metadata.withProperty("foo", String.class).fromSource(AdditionalMetadata.class));
}

private String buildAdditionalMetadata(ItemMetadata... metadata) throws Exception {
private String buildAdditionalMetadata(ItemMetadata... metadata) throws JSONException {
TestJsonConverter converter = new TestJsonConverter();
JSONObject additionalMetadata = new JSONObject();
JSONArray properties = new JSONArray();
Expand All @@ -245,14 +246,14 @@ private String buildAdditionalMetadata(ItemMetadata... metadata) throws Exceptio
return additionalMetadata.toString();
}

private String buildAdditionalHints(ItemHint... hints) throws Exception {
private String buildAdditionalHints(ItemHint... hints) throws JSONException {
TestJsonConverter converter = new TestJsonConverter();
JSONObject additionalMetadata = new JSONObject();
additionalMetadata.put("hints", converter.toJsonArray(Arrays.asList(hints)));
return additionalMetadata.toString();
}

private String buildPropertyDeprecations(ItemMetadata... items) throws Exception {
private String buildPropertyDeprecations(ItemMetadata... items) throws JSONException {
JSONArray propertiesArray = new JSONArray();
for (ItemMetadata item : items) {
JSONObject jsonObject = new JSONObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Collection;

import org.springframework.boot.configurationprocessor.json.JSONArray;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.boot.configurationprocessor.metadata.ItemMetadata.ItemType;

Expand All @@ -30,17 +31,17 @@
public class TestJsonConverter extends JsonConverter {

@Override
public JSONArray toJsonArray(ConfigurationMetadata metadata, ItemType itemType) throws Exception {
public JSONArray toJsonArray(ConfigurationMetadata metadata, ItemType itemType) throws JSONException {
return super.toJsonArray(metadata, itemType);
}

@Override
public JSONArray toJsonArray(Collection<ItemHint> hints) throws Exception {
public JSONArray toJsonArray(Collection<ItemHint> hints) throws JSONException {
return super.toJsonArray(hints);
}

@Override
public JSONObject toJsonObject(ItemMetadata item) throws Exception {
public JSONObject toJsonObject(ItemMetadata item) throws JSONException {
return super.toJsonObject(item);
}

Expand Down
Loading