Revert ConfigUtil::normalizeType and fix null annotations (#4608)

Partly reverts 8e597bde76.

Signed-off-by: Holger Friedrich <mail@holger-friedrich.de>
pull/4618/head
Holger Friedrich 2025-02-18 18:24:48 +01:00 committed by GitHub
parent 91940ef34d
commit 4f64c8d37b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 26 additions and 25 deletions

View File

@ -176,22 +176,20 @@ public class ConfigUtil {
* @return corresponding value as a valid type
* @throws IllegalArgumentException if an invalid type has been given
*/
public static Object normalizeType(Object value, @Nullable ConfigDescriptionParameter configDescriptionParameter) {
Object result = null;
@Nullable
public static Object normalizeType(@Nullable Object value,
@Nullable ConfigDescriptionParameter configDescriptionParameter) {
if (configDescriptionParameter != null) {
Normalizer normalizer = NormalizerFactory.getNormalizer(configDescriptionParameter);
result = normalizer.normalize(value);
return normalizer.normalize(value);
} else if (value instanceof Boolean) {
result = NormalizerFactory.getNormalizer(Type.BOOLEAN).normalize(value);
return NormalizerFactory.getNormalizer(Type.BOOLEAN).normalize(value);
} else if (value instanceof String) {
result = NormalizerFactory.getNormalizer(Type.TEXT).normalize(value);
return NormalizerFactory.getNormalizer(Type.TEXT).normalize(value);
} else if (value instanceof Number) {
result = NormalizerFactory.getNormalizer(Type.DECIMAL).normalize(value);
return NormalizerFactory.getNormalizer(Type.DECIMAL).normalize(value);
} else if (value instanceof Collection collection) {
result = normalizeCollection(collection);
}
if (result != null) {
return result;
return normalizeCollection(collection);
}
throw new IllegalArgumentException(
"Invalid type '{%s}' of configuration value!".formatted(value.getClass().getCanonicalName()));
@ -214,20 +212,21 @@ public class ConfigUtil {
* @return the normalized configuration or null if given configuration was null
* @throws IllegalArgumentException if given config description is null
*/
public static Map<String, Object> normalizeTypes(Map<String, Object> configuration,
public static Map<String, @Nullable Object> normalizeTypes(Map<String, @Nullable Object> configuration,
List<ConfigDescription> configDescriptions) {
if (configDescriptions.isEmpty()) {
throw new IllegalArgumentException("Config description must not be empty.");
}
Map<String, Object> convertedConfiguration = new HashMap<>();
Map<String, @Nullable Object> convertedConfiguration = new HashMap<>();
Map<String, ConfigDescriptionParameter> configParams = new HashMap<>();
for (int i = configDescriptions.size() - 1; i >= 0; i--) {
configParams.putAll(configDescriptions.get(i).toParametersMap());
}
for (Entry<String, Object> parameter : configuration.entrySet()) {
for (Entry<String, @Nullable Object> parameter : configuration.entrySet()) {
String name = parameter.getKey();
@Nullable
Object value = parameter.getValue();
if (!isOSGiConfigParameter(name)) {
ConfigDescriptionParameter configDescriptionParameter = configParams.get(name);
@ -246,7 +245,7 @@ public class ConfigUtil {
* @param value the value to return as normalized type
* @return corresponding value as a valid type
*/
public static @Nullable Object normalizeType(Object value) {
public static @Nullable Object normalizeType(@Nullable Object value) {
return normalizeType(value, null);
}

View File

@ -399,7 +399,7 @@ public class AddonResource implements RESTResource, EventSubscriber {
@ApiResponse(responseCode = "500", description = "Configuration can not be updated due to internal error") })
public Response updateConfiguration(@PathParam("addonId") @Parameter(description = "Add-on id") String addonId,
@QueryParam("serviceId") @Parameter(description = "service ID") @Nullable String serviceId,
@Nullable Map<String, Object> configuration) {
@Nullable Map<String, @Nullable Object> configuration) {
try {
AddonService addonService = (serviceId != null) ? getServiceById(serviceId) : getDefaultService();
if (addonService == null) {
@ -425,8 +425,8 @@ public class AddonResource implements RESTResource, EventSubscriber {
}
}
private @Nullable Map<String, Object> normalizeConfiguration(@Nullable Map<String, Object> properties,
String addonId) {
private @Nullable Map<String, @Nullable Object> normalizeConfiguration(
@Nullable Map<String, @Nullable Object> properties, String addonId) {
if (properties == null || properties.isEmpty()) {
return properties;
}

View File

@ -224,7 +224,7 @@ public class ConfigurableServiceResource implements RESTResource {
public Response updateConfiguration(
@HeaderParam("Accept-Language") @Parameter(description = "language") @Nullable String language,
@PathParam("serviceId") @Parameter(description = "service ID") String serviceId,
@Nullable Map<String, Object> configuration) {
@Nullable Map<String, @Nullable Object> configuration) {
Locale locale = localeService.getLocale(language);
try {
Configuration oldConfiguration = configurationService.get(serviceId);
@ -238,8 +238,8 @@ public class ConfigurableServiceResource implements RESTResource {
}
}
private @Nullable Map<String, Object> normalizeConfiguration(@Nullable Map<String, Object> properties,
String serviceId, Locale locale) {
private @Nullable Map<String, @Nullable Object> normalizeConfiguration(
@Nullable Map<String, @Nullable Object> properties, String serviceId, Locale locale) {
if (properties == null || properties.isEmpty()) {
return properties;
}

View File

@ -501,7 +501,7 @@ public class ThingResource implements RESTResource {
public Response updateConfiguration(
@HeaderParam(HttpHeaders.ACCEPT_LANGUAGE) @Parameter(description = "language") @Nullable String language,
@PathParam("thingUID") @Parameter(description = "thing") String thingUID,
@Parameter(description = "configuration parameters") @Nullable Map<String, Object> configurationParameters)
@Parameter(description = "configuration parameters") @Nullable Map<String, @Nullable Object> configurationParameters)
throws IOException {
final Locale locale = localeService.getLocale(language);
@ -788,8 +788,9 @@ public class ThingResource implements RESTResource {
return linkedItemsMap;
}
private @Nullable Map<String, Object> normalizeConfiguration(@Nullable Map<String, Object> properties,
ThingTypeUID thingTypeUID, @Nullable ThingUID thingUID) {
private @Nullable Map<String, @Nullable Object> normalizeConfiguration(
@Nullable Map<String, @Nullable Object> properties, ThingTypeUID thingTypeUID,
@Nullable ThingUID thingUID) {
if (properties == null || properties.isEmpty()) {
return properties;
}
@ -824,7 +825,7 @@ public class ThingResource implements RESTResource {
return ConfigUtil.normalizeTypes(properties, configDescriptions);
}
private @Nullable Map<String, Object> normalizeConfiguration(Map<String, Object> properties,
private @Nullable Map<String, @Nullable Object> normalizeConfiguration(Map<String, @Nullable Object> properties,
ChannelTypeUID channelTypeUID, ChannelUID channelUID) {
if (properties == null || properties.isEmpty()) {
return properties;

View File

@ -28,6 +28,7 @@ import java.util.Optional;
import javax.ws.rs.core.Response;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openhab.core.io.rest.core.service.ConfigurableServiceDTO;
@ -135,7 +136,7 @@ public class ConfigurableServiceResourceOSGiTest extends JavaOSGiTest {
Response response = configurableServiceResource.getConfiguration("id");
assertThat(response.getStatus(), is(200));
Map<String, Object> newConfiguration = new HashMap<>();
Map<String, @Nullable Object> newConfiguration = new HashMap<>();
newConfiguration.put("a", "b");
response = configurableServiceResource.updateConfiguration("en", "id", newConfiguration);
assertThat(response.getStatus(), is(204));