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
pull/3346/head
Boris Krivonog 2023-01-29 21:20:02 +01:00 committed by GitHub
parent 24b1cce213
commit a50a0886e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 0 deletions

View File

@ -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

View File

@ -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<PropertyName> 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();
}
}