From a50a0886e89d47c1fcfd80dad6f80496a31dc1b4 Mon Sep 17 00:00:00 2001 From: Boris Krivonog Date: Sun, 29 Jan 2023 21:20:02 +0100 Subject: [PATCH] Fix parsing of YAML default value for community marketplace UI Widgets (#3334) * fixes https://github.com/openhab/openhab-core/issues/3332 Signed-off-by: Boris Krivonog boris.krivonog@inova.si --- .../CommunityUIWidgetAddonHandler.java | 3 + .../SerializedNameAnnotationIntrospector.java | 57 +++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 bundles/org.openhab.core.addon.marketplace/src/main/java/org/openhab/core/addon/marketplace/internal/community/SerializedNameAnnotationIntrospector.java diff --git a/bundles/org.openhab.core.addon.marketplace/src/main/java/org/openhab/core/addon/marketplace/internal/community/CommunityUIWidgetAddonHandler.java b/bundles/org.openhab.core.addon.marketplace/src/main/java/org/openhab/core/addon/marketplace/internal/community/CommunityUIWidgetAddonHandler.java index 76dd8e130..d69f39f2c 100644 --- a/bundles/org.openhab.core.addon.marketplace/src/main/java/org/openhab/core/addon/marketplace/internal/community/CommunityUIWidgetAddonHandler.java +++ b/bundles/org.openhab.core.addon.marketplace/src/main/java/org/openhab/core/addon/marketplace/internal/community/CommunityUIWidgetAddonHandler.java @@ -33,6 +33,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.introspect.AnnotationIntrospectorPair; import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; /** @@ -60,6 +61,8 @@ public class CommunityUIWidgetAddonHandler implements MarketplaceAddonHandler { this.yamlMapper = new ObjectMapper(new YAMLFactory()); yamlMapper.findAndRegisterModules(); this.yamlMapper.setDateFormat(new SimpleDateFormat("MMM d, yyyy, hh:mm:ss aa", Locale.ENGLISH)); + yamlMapper.setAnnotationIntrospector(new AnnotationIntrospectorPair(new SerializedNameAnnotationIntrospector(), + yamlMapper.getSerializationConfig().getAnnotationIntrospector())); } @Override diff --git a/bundles/org.openhab.core.addon.marketplace/src/main/java/org/openhab/core/addon/marketplace/internal/community/SerializedNameAnnotationIntrospector.java b/bundles/org.openhab.core.addon.marketplace/src/main/java/org/openhab/core/addon/marketplace/internal/community/SerializedNameAnnotationIntrospector.java new file mode 100644 index 000000000..5e32f55a2 --- /dev/null +++ b/bundles/org.openhab.core.addon.marketplace/src/main/java/org/openhab/core/addon/marketplace/internal/community/SerializedNameAnnotationIntrospector.java @@ -0,0 +1,57 @@ +/** + * Copyright (c) 2010-2023 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.core.addon.marketplace.internal.community; + +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.eclipse.jdt.annotation.NonNullByDefault; + +import com.fasterxml.jackson.core.Version; +import com.fasterxml.jackson.databind.AnnotationIntrospector; +import com.fasterxml.jackson.databind.PropertyName; +import com.fasterxml.jackson.databind.introspect.Annotated; +import com.google.gson.annotations.SerializedName; + +/** + * This is a {@link SerializedNameAnnotationIntrospector}, which processes SerializedName annotations. + * + * @author Boris Krivonog - Initial contribution + * + */ +@NonNullByDefault +final class SerializedNameAnnotationIntrospector extends AnnotationIntrospector { + private static final long serialVersionUID = 1L; + + @Override + @NonNullByDefault({}) + public PropertyName findNameForDeserialization(Annotated annotated) { + return Optional.ofNullable(annotated.getAnnotation(SerializedName.class)).map(s -> new PropertyName(s.value())) + .orElseGet(() -> super.findNameForDeserialization(annotated)); + } + + @Override + @NonNullByDefault({}) + public List findPropertyAliases(Annotated annotated) { + return Optional.ofNullable(annotated.getAnnotation(SerializedName.class)) + .map(s -> Stream.of(s.alternate()).map(PropertyName::new).collect(Collectors.toList())) + .orElseGet(() -> super.findPropertyAliases(annotated)); + } + + @Override + public Version version() { + return Version.unknownVersion(); + } +}