100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps
Java asset-managementbuildingdashboarddataplatformdevice-managementedge-computingenergyenergy-managementenergy-monitorfleet-managementfleet-telematicsiotiot-applicationiot-platformmodbusmqttmqtt-brokerota-updaterules-enginesmartcity
Fix agent links always showing asset invalid error (#2259)
Koen39540604a2c3
diff --git a/manager/src/main/java/org/openremote/manager/asset/AssetModelService.java b/manager/src/main/java/org/openremote/manager/asset/AssetModelService.javaindex a51550e20..6d967cd8d 100644--- a/manager/src/main/java/org/openremote/manager/asset/AssetModelService.java+++ b/manager/src/main/java/org/openremote/manager/asset/AssetModelService.java@@ -27,7 +27,6 @@ import com.fasterxml.jackson.databind.ObjectMapper;import com.fasterxml.jackson.databind.annotation.JsonDeserialize;import com.fasterxml.jackson.databind.annotation.JsonSerialize;import com.fasterxml.jackson.databind.deser.std.StdDeserializer;-import com.fasterxml.jackson.databind.node.ObjectNode;import com.fasterxml.jackson.databind.type.TypeFactory;import com.fasterxml.jackson.databind.util.TokenBuffer;import org.apache.camel.builder.RouteBuilder;@@ -52,14 +51,12 @@ import org.openremote.model.value.MetaItemDescriptor;import org.openremote.model.value.ValueDescriptor;import java.io.IOException;-import java.lang.reflect.Array;import com.fasterxml.jackson.databind.JsonNode;import java.nio.file.Files;import java.nio.file.Path;import java.util.*;-import java.util.concurrent.ConcurrentHashMap;import java.util.concurrent.atomic.AtomicReference;import java.util.function.Function;import java.util.logging.Level;@@ -275,7 +272,7 @@ public class AssetModelService extends RouteBuilder implements ContainerService,@Overridepublic void start(Container container) throws Exception {-+ ValueUtil.cacheCommonValueDescriptorSchemas();}@Overridediff --git a/model/src/main/java/org/openremote/model/asset/agent/AgentLink.java b/model/src/main/java/org/openremote/model/asset/agent/AgentLink.javaindex 9a3047299..c10ead6e1 100644--- a/model/src/main/java/org/openremote/model/asset/agent/AgentLink.java+++ b/model/src/main/java/org/openremote/model/asset/agent/AgentLink.java@@ -37,7 +37,7 @@ import java.util.Optional;* own concrete implementation of this class or use {@link DefaultAgentLink} with fields describing each configuration* item and standard JSR-380 annotations should be used to provide validation logic.*/-@JsonTypeInfo(property = "type", use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, defaultImpl = DefaultAgentLink.class)+@JsonTypeInfo(property = "type", use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXTERNAL_PROPERTY, defaultImpl = DefaultAgentLink.class)@JsonSchemaDefault("{\"id\":\"\",\"type\":\"DefaultAgentLink\"}")@TsIgnoreTypeParamspublic abstract class AgentLink<T extends AgentLink<?>> implements Serializable {diff --git a/model/src/main/java/org/openremote/model/util/JSONSchemaUtil.java b/model/src/main/java/org/openremote/model/util/JSONSchemaUtil.javaindex 65a8a2449..dbdc1692b 100644--- a/model/src/main/java/org/openremote/model/util/JSONSchemaUtil.java+++ b/model/src/main/java/org/openremote/model/util/JSONSchemaUtil.java@@ -47,6 +47,8 @@ import java.util.*;import java.util.concurrent.ConcurrentHashMap;import java.util.stream.Collectors;+import com.fasterxml.jackson.annotation.JsonTypeInfo;+public class JSONSchemaUtil {public static class SchemaNodeMapper {@@ -314,12 +316,34 @@ public class JSONSchemaUtil {}ObjectNode definition = context.getGeneratorConfig().createObjectNode();+ definition.put(context.getKeyword(SchemaKeyword.TAG_TYPE), context.getKeyword(SchemaKeyword.TAG_TYPE_OBJECT));ArrayNode oneOfArray = definition.withArray(context.getKeyword(SchemaKeyword.TAG_ONEOF));for (ResolvedType subType : subTypes) {oneOfArray.add(context.createDefinitionReference(subType));}+ Class<?> rawType = resolvedType.getErasedType();++ // Custom subtype handling mimicking createSubtypeDefinition referencing subtypes as enum type value+ // for polymorphic types+ if (rawType.isAnnotationPresent(JsonTypeInfo.class) && rawType.getAnnotation(JsonTypeInfo.class).include() == JsonTypeInfo.As.EXTERNAL_PROPERTY) {+ ArrayNode enumTypeArray = definition+ .withObject(context.getKeyword(SchemaKeyword.TAG_PROPERTIES))+ .withObject(context.getKeyword(SchemaKeyword.TAG_TYPE))+ .withArray(context.getKeyword(SchemaKeyword.TAG_ENUM));++ // Removing and adding the `oneOf` property on the schema ensures the definitions are referenced+ definition.remove(context.getKeyword(SchemaKeyword.TAG_ONEOF));+ for (ResolvedType subType : subTypes) {+ // Add subtype to the enum type array of the abstract class+ enumTypeArray.add(subType.getErasedType().getSimpleName());+ // Add back subtype definitions to `oneOf` property+ definition.withArray(context.getKeyword(SchemaKeyword.TAG_ONEOF)).add(context.createDefinitionReference(subType));+ }+ return new CustomDefinition(definition); // Setting ALWAYS_REF here doesn't affect referencing+ }+// Always inline the super class schema to avoid allOf wrapping, which cannot be cleaned up as// JSON Schema draft-7 does not permit other properties alongside a $ref, see// https://json-schema.org/draft-07/draft-handrews-json-schema-01#rfc.section.8.3@@ -333,13 +357,27 @@ public class JSONSchemaUtil {if (erasedType.getSuperclass() == Object.class) {return;}- String key = null;+Class<?> current = erasedType;- while(current != null && current.getSuperclass() != Object.class && key == null) {+ JsonTypeInfo annotation = null;+ String key = null;+ do {current = current.getSuperclass();- key = Optional.ofNullable(current)- .map(c -> c.getAnnotation(JsonTypeInfo.class))- .map(JsonTypeInfo::property).orElse(null);+ if (current != null) {+ annotation = current.getAnnotation(JsonTypeInfo.class);+ if (annotation != null) key = annotation.property();+ }+ } while (current != null && current.getSuperclass() != Object.class && key == null);++ // If EXTERNAL_PROPERTY specified, manually add 'const', 'default' and 'required' properties+ if (annotation != null && annotation.include() == JsonTypeInfo.As.EXTERNAL_PROPERTY) {+ attrs.withArray(context.getKeyword(SchemaKeyword.TAG_REQUIRED))+ .add(context.getKeyword(SchemaKeyword.TAG_TYPE));+ attrs.withObject(context.getKeyword(SchemaKeyword.TAG_PROPERTIES))+ .withObject(context.getKeyword(SchemaKeyword.TAG_TYPE))+ .put(context.getKeyword(SchemaKeyword.TAG_CONST), erasedType.getSimpleName())+ .put(context.getKeyword(SchemaKeyword.TAG_DEFAULT), erasedType.getSimpleName());+ return;}addDefaultToDiscriminator(attrs, context, key);});@@ -505,7 +543,8 @@ public class JSONSchemaUtil {(!attrs.has(context.getKeyword(SchemaKeyword.TAG_TITLE))&& !scope.getType().isInstanceOf(Map.class)&& attrs.has(context.getKeyword(SchemaKeyword.TAG_TYPE))- && attrs.get(context.getKeyword(SchemaKeyword.TAG_TYPE)).textValue().equals("object"))+ && attrs.get(context.getKeyword(SchemaKeyword.TAG_TYPE)).isTextual()+ && attrs.get(context.getKeyword(SchemaKeyword.TAG_TYPE)).textValue().equals(context.getKeyword(SchemaKeyword.TAG_TYPE_OBJECT)))) {// Code found here: http://stackoverflow.com/questions/2559759/how-do-i-convert-camelcase-into-human-readable-names-in-javaString title = scope.getType().getErasedType().getSimpleName().replaceAll(diff --git a/model/src/main/java/org/openremote/model/util/ValueUtil.java b/model/src/main/java/org/openremote/model/util/ValueUtil.javaindex c6eb43911..6337f6a55 100644--- a/model/src/main/java/org/openremote/model/util/ValueUtil.java+++ b/model/src/main/java/org/openremote/model/util/ValueUtil.java@@ -939,8 +939,16 @@ public class ValueUtil {JSON.registerSubtypes(agentLinkSubTypes);doSchemaInit();+ }++ protected static void doSchemaInit() {+ generator = new SchemaGenerator(JSONSchemaUtil.getJsonSchemaConfig(JSON));+ }- // Add JSON schemas for complex meta items and generate cache keys+ /**+ * Computes JSON schemas for complex meta items and generates cache keys+ */+ public static void cacheCommonValueDescriptorSchemas() {Map<String, MetaItemDescriptor<?>> metaItems = getMetaItemDescriptors();valueDescriptorSchemas.putAll(metaItems.values().stream().map(AbstractNameValueDescriptorHolder::getType)@@ -958,10 +966,6 @@ public class ValueUtil {)));}- protected static void doSchemaInit() {- generator = new SchemaGenerator(JSONSchemaUtil.getJsonSchemaConfig(JSON));- }-public static JsonNode getValueDescriptorSchema(String name) {if (ValueUtil.getValueDescriptor(name).isEmpty()) {return null;diff --git a/model/src/test/java/org/openremote/model/util/JSONSchemaUtilTest.java b/model/src/test/java/org/openremote/model/util/JSONSchemaUtilTest.javaindex 1f40aab71..33de51eab 100644--- a/model/src/test/java/org/openremote/model/util/JSONSchemaUtilTest.java+++ b/model/src/test/java/org/openremote/model/util/JSONSchemaUtilTest.java@@ -1,12 +1,8 @@package org.openremote.model.util;-import com.fasterxml.jackson.annotation.JsonProperty;-import com.fasterxml.jackson.annotation.JsonPropertyDescription;-import com.fasterxml.jackson.annotation.JsonSubTypes;-import com.fasterxml.jackson.annotation.JsonTypeInfo;+import com.fasterxml.jackson.annotation.*;import com.fasterxml.jackson.core.JsonProcessingException;import com.fasterxml.jackson.databind.JsonNode;-import com.fasterxml.jackson.databind.annotation.JsonSerialize;import org.json.JSONException;import org.junit.jupiter.api.BeforeEach;import org.junit.jupiter.api.Test;@@ -348,21 +344,14 @@ public class JSONSchemaUtilTest {@JsonTypeInfo(property = "type", use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY)@JsonSubTypes({- @JsonSubTypes.Type(TestAgentLinkAnnotationResolving.class),- @JsonSubTypes.Type(TestAgentLinkSuperclassAnnotationResolving.class),+ @JsonSubTypes.Type(SubType.class),+ @JsonSubTypes.Type(SubTypeSuperclass.class),})- abstract static class TestAgentLink<T extends TestAgentLink<?>> implements Serializable {-- @JsonSchemaFormat("or-agent-id")- protected String id;-- @JsonSerialize- protected String getType() {- return getClass().getSimpleName();- }- }- static class TestAgentLinkAnnotationResolving extends TestAgentLink<TestAgentLinkAnnotationResolving> { }- static class TestAgentLinkSuperclassAnnotationResolving extends TestAgentLinkAnnotationResolving { }+ abstract static class PolymorphicType<T extends PolymorphicType<?>> implements Serializable {}+ @JsonTypeName("SubType")+ static class SubType extends PolymorphicType<SubType> {}+ @JsonTypeName("SubTypeSuperclass")+ static class SubTypeSuperclass extends SubType { }@Testpublic void shouldHaveSubtypesWithDefaultTypeProperty() throws JsonProcessingException, JSONException {@@ -370,28 +359,28 @@ public class JSONSchemaUtilTest {{"$schema": "http://json-schema.org/draft-07/schema#","definitions": {- "TestAgentLinkAnnotationResolving": {- "title": "Test Agent Link Annotation Resolving",+ "SubType": {+ "title": "Sub Type","type": "object","additionalProperties": true,"properties": {"type": {- "const": "JSONSchemaUtilTest$TestAgentLinkAnnotationResolving",- "default": "JSONSchemaUtilTest$TestAgentLinkAnnotationResolving"+ "const": "SubType",+ "default": "SubType"}},"required": ["type"]},- "TestAgentLinkSuperclassAnnotationResolving": {- "title": "Test Agent Link Superclass Annotation Resolving",+ "SubTypeSuperclass": {+ "title": "Sub Type Superclass","type": "object","additionalProperties": true,"properties": {"type": {- "const": "JSONSchemaUtilTest$TestAgentLinkSuperclassAnnotationResolving",- "default": "JSONSchemaUtilTest$TestAgentLinkSuperclassAnnotationResolving"+ "const": "SubTypeSuperclass",+ "default": "SubTypeSuperclass"}},"required": [@@ -400,44 +389,105 @@ public class JSONSchemaUtilTest {}},"oneOf": [- { "$ref": "#/definitions/TestAgentLinkAnnotationResolving" },- { "$ref": "#/definitions/TestAgentLinkSuperclassAnnotationResolving" }+ { "$ref": "#/definitions/SubType" },+ { "$ref": "#/definitions/SubTypeSuperclass" }],- "title": "Test Agent Link"+ "type": "object",+ "additionalProperties": true,+ "title": "Polymorphic Type"}""");- JsonNode actual = ValueUtil.getSchema(TestAgentLink.class);+ JsonNode actual = ValueUtil.getSchema(PolymorphicType.class);assertEquals(expected.toString(), actual.toString(), true);}- @JsonTypeInfo(property = "type", use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY)- abstract static class TestAgentLinkReflections<T extends TestAgentLinkReflections<?>> implements Serializable {+ @JsonTypeInfo(property = "type", use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXTERNAL_PROPERTY)+ @JsonSubTypes({+ @JsonSubTypes.Type(ExternalSubType.class),+ @JsonSubTypes.Type(ExternalSubTypeSuperclass.class),+ })+ abstract static class ExternalPolymorphicType<T extends ExternalPolymorphicType<?>> implements Serializable {}+ static class ExternalSubType extends ExternalPolymorphicType<ExternalSubType> { }+ static class ExternalSubTypeSuperclass extends ExternalSubType { }- @JsonSchemaFormat("or-agent-id")- protected String id;+ @Test+ public void shouldSetEnumTypeForExternalProperty() throws JsonProcessingException, JSONException {+ JsonNode expected = ValueUtil.JSON.readTree("""+ {+ "$schema": "http://json-schema.org/draft-07/schema#",+ "definitions": {+ "ExternalSubType": {+ "title": "External Sub Type",+ "type": "object",+ "additionalProperties": true,+ "properties": {+ "type": {+ "const": "ExternalSubType",+ "default": "ExternalSubType"+ }+ },+ "required": [+ "type"+ ]+ },+ "ExternalSubTypeSuperclass": {+ "title": "External Sub Type Superclass",+ "type": "object",+ "additionalProperties": true,+ "properties": {+ "type": {+ "const": "ExternalSubTypeSuperclass",+ "default": "ExternalSubTypeSuperclass"+ }+ },+ "required": [+ "type"+ ]+ }+ },+ "oneOf": [+ { "$ref": "#/definitions/ExternalSubType" },+ { "$ref": "#/definitions/ExternalSubTypeSuperclass" }+ ],+ "properties": {+ "type": {+ "enum": [+ "ExternalSubType",+ "ExternalSubTypeSuperclass"+ ]+ }+ },+ "type": "object",+ "additionalProperties": true,+ "title": "External Polymorphic Type"+ }"""+ );- @JsonSerialize- protected String getType() {- return getClass().getSimpleName();- }+ JsonNode actual = ValueUtil.getSchema(ExternalPolymorphicType.class);+ assertEquals(expected.toString(), actual.toString(), true);}- static class TestAgentLinkReflectionResolving extends TestAgentLinkReflections<TestAgentLinkReflectionResolving> { }++ @JsonTypeName("ReflectedPolymorphicType")+ @JsonTypeInfo(property = "type", use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY)+ abstract static class ReflectedPolymorphicType<T extends ReflectedPolymorphicType<?>> implements Serializable {}+ @JsonTypeName("ResolvedSubType")+ static class ResolvedSubType extends ReflectedPolymorphicType<ResolvedSubType> { }@Test- public void shouldResolveSubtypesReflections() throws JsonProcessingException, JSONException {+ public void shouldResolveSubtypesThroughReflections() throws JsonProcessingException, JSONException {JsonNode expected = ValueUtil.JSON.readTree("""{"$schema": "http://json-schema.org/draft-07/schema#","definitions": {- "TestAgentLinkReflectionResolving": {- "title": "Test Agent Link Reflection Resolving",+ "ResolvedSubType": {+ "title": "Resolved Sub Type","type": "object","additionalProperties": true,"properties": {"type": {- "const": "JSONSchemaUtilTest$TestAgentLinkReflectionResolving",- "default": "JSONSchemaUtilTest$TestAgentLinkReflectionResolving"+ "const": "ResolvedSubType",+ "default": "ResolvedSubType"}},"required": [@@ -445,14 +495,15 @@ public class JSONSchemaUtilTest {]}},- "title": "Test Agent Link Reflections",+ "title": "Reflected Polymorphic Type","oneOf": [- { "$ref": "#/definitions/TestAgentLinkReflectionResolving" }+ { "$ref": "#/definitions/ResolvedSubType" }],"type": "object",+ "additionalProperties": true,"properties": {"type": {- "const": "JSONSchemaUtilTest$TestAgentLinkReflections"+ "const": "ReflectedPolymorphicType"}},"required": [@@ -461,9 +512,7 @@ public class JSONSchemaUtilTest {}""");--- JsonNode actual = ValueUtil.getSchema(TestAgentLinkReflections.class);+ JsonNode actual = ValueUtil.getSchema(ReflectedPolymorphicType.class);assertEquals(expected.toString(), actual.toString(), true);}