Skip to content

Re-evaluate usage of of JsonTypeInfo defaultImpl on AgentLink #3575

Open

Description

@richturner

Some discussion around JSR-380 validation on AgentLink in another PR:

https://github.com/openremote/core/pull/3380/changes/4f785084e5f2af9cbb96acc932d99d9bf5ab111c..ed33802ad785cfa9bc205f75ca2e9b9d6a3077c0#r4564270569

We should reconsider if an invalid agentlink type should always deserialise to DefaultAgentLink or not.

My thoughts are that it shouldn't but maybe there was a good reason I did this in the first place.

Activity

  1. Ekhorn commented on Nov 16, 2026

    @Ekhorn
    Contributor

    My presumption would've been maybe it was a UI consideration? But the following (AI) investigation opposes that, although we nonetheless probably have to make some UI changes around this:

    Investigation summary

    Short answer: I found nothing suggesting the DefaultAgentLink fallback was added for the UI. It existed before the JSON Forms editor, and what it does today matters only to the backend.

    History

    Jan 2021: the model refactor added the fallback. A custom type id resolver sent any unknown id to AgentLink.Default (getAgentLinkClass(id).orElse(AgentLink.Default.class)):

    @JsonTypeInfo(property = "type", use = JsonTypeInfo.Id.CUSTOM, defaultImpl = AgentLink.Default.class)
    @JsonTypeIdResolver(AgentLink.AgentLinkTypeIdResolver.class)
    public abstract class AgentLink<T extends AgentLink<?>> implements Serializable {
    /**
    * Resolves agent link type as agent type strings using {@link org.openremote.model.util.AssetModelUtil}
    */
    @TsIgnore
    public static class AgentLinkTypeIdResolver extends TypeIdResolverBase {
    @Override
    public String idFromValue(Object value) {
    if (value == null) {
    return null;
    }
    if (!(value instanceof AgentLink)) {
    throw new IllegalArgumentException("Type must be an agent link type");
    }
    return value.getClass().getSimpleName().replaceAll("\\$", ".");
    }
    @Override
    public String idFromValueAndType(Object value, Class<?> suggestedType) {
    return idFromValue(value);
    }
    @Override
    public JsonTypeInfo.Id getMechanism() {
    return JsonTypeInfo.Id.CUSTOM;
    }
    @Override
    public JavaType typeFromId(DatabindContext context, String id) throws IOException {
    Class<? extends AgentLink<?>> agentLinkClass = AssetModelUtil.getAgentLinkClass(id)
    .orElse(AgentLink.Default.class);

    Aug 2021: 0647f57, titled "WIP or-json-forms usage", replaced that resolver with Id.NAME + defaultImpl and moved the inner class out to DefaultAgentLink. The commit title mentions the UI, but the fallback was only carried over from the earlier commit. The class javadoc gives a Java generics reason for it: it hides the type parameter in class references.

    /**
    * Does nothing other than hide the generic type parameter which causes problems with inference from
    * class references
    */
    @TsIgnore
    public class DefaultAgentLink extends AgentLink<DefaultAgentLink> {

    UI

    The only UI-related piece is the @JsonSchemaDefault with "type":"DefaultAgentLink". It came in Oct 2025 with cfbda0c to give JSON Forms a starting value.

    @JsonTypeInfo(
    property = "type",
    use = JsonTypeInfo.Id.NAME,
    include = JsonTypeInfo.As.EXTERNAL_PROPERTY,
    defaultImpl = DefaultAgentLink.class)
    @JsonSchemaDefault("{\"id\":\"\",\"type\":\"DefaultAgentLink\"}")

    When you pick an agent, the UI always replaces type with that agent's agentLinkType. So the UI never depends on the fallback.

    if (newAgentDescriptor) {
    props.handleChange("", {
    id: agent.id,
    type: newAgentDescriptor.agentLinkType,
    });

    Backend effects

    Reading stored assets keeps working. Link subtypes are only registered for agent descriptors that are loaded. A stored link can have a type that is no longer registered (protocol removed, extension not deployed, class renamed) or no type key at all; with the fallback it still reads.

    // Add agent link sub types to object mapper (need to avoid circular dependency)
    NamedType[] agentLinkSubTypes = Arrays.stream(getAgentLinkClasses())
    .map(agentLinkClass -> new NamedType(
    agentLinkClass,
    agentLinkClass.getSimpleName()
    )).toArray(NamedType[]::new);
    JSON.registerSubtypes(agentLinkSubTypes);

    Without defaultImpl, Jackson throws in those cases by default. Meta is deserialised with no try/catch, unlike values, so I'd expect the whole asset to fail to load. I didn't run this.

    case "meta" -> {
    // findNonContextualValueDeserializer is slow so caching the result to speed up
    // deserialisation
    if (metaDeserialiser == null) {
    metaDeserialiser = ctxt.findNonContextualValueDeserializer(META_MAP_TYPE);
    }
    attribute.meta = (MetaMap) metaDeserialiser.deserialize(jp, ctxt);
    }

    } else {
    try {
    ((Attribute) attribute).value = deserialiseValue(valueDescriptor, jp, ctxt);
    } catch (Exception ignored) {
    }

    The fallback doesn't make the link work. Agent.getAgentLink casts without checking the type. An HTTP protocol, for example, would be handed a DefaultAgentLink and fail with a ClassCastException while linking.

    public <T extends MetaHolder> V getAgentLink(T attribute) {
    AgentLink<?> agentLink =
    attribute
    .getMetaValue(AGENT_LINK)
    .orElseThrow(
    () ->
    new IllegalStateException(
    "Failed to getAgentLink<?>despite attribute being linked to an agent"));
    return (V) agentLink;

    try {
    doLinkAttribute(assetId, attribute, agent.getAgentLink(attribute));
    } catch (Exception e) {
    linkedAttributes.remove(attributeRef);
    throw new RuntimeException(e);
    }

    The class itself has to stay. DefaultAgentLink is the real link type for the TCP, UDP, serial, Art-Net and Trådfri agents. Only defaultImpl is in question. This is also why, after binding, a fallback can't be told apart from a real link, the point raised in #3380 (comment)
    https://github.com/openremote/core/blob/53afd942e2c/agent/src/main/java/org/openremote/agent/protocol/tcp/TCPAgent.java#L29-L35

    The one backend reason I can find is that assets with unknown or missing link types still load. If defaultImpl is removed, that case needs its own handling, such as keeping the ritem, so the asset doesn't fail to load.

  2. richturner commented on Nov 16, 2026

    @richturner
    MemberAuthor

    I would guess it was related to the Schema generation and the JSON forms but if it can be removed and the UI can still function then we should look at doing so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions