quarkusio/quarkus · error · ClassCastException

Cannot cast <item> of kind <kind> to a io.quarkus.redis.data

Error message

Cannot cast <item> of kind <kind> to a io.quarkus.redis.datasource.graph.GraphQueryResponseItem$ScalarItem

What it means

GraphQueryResponseItem is a polymorphic result type for RedisGraph query results; each item is a SCALAR, NODE, or RELATION. The asScalarItem() convenience accessor throws a ClassCastException when the underlying item's kind() is not SCALAR, protecting you from an unsafe downcast. Call kind() first to know which concrete accessor is valid.

Source

Thrown at extensions/redis-client/runtime/src/main/java/io/quarkus/redis/datasource/graph/GraphQueryResponseItem.java:21

import java.util.List;

public interface GraphQueryResponseItem {

    enum Kind {
        SCALAR,
        NODE,
        RELATION
    }

    Kind kind();

    String name();

    default ScalarItem asScalarItem() {
        if (this instanceof ScalarItem) {
            return (ScalarItem) this;
        }
        throw new ClassCastException("Cannot cast " + this + " of kind " + kind() + " to a " + ScalarItem.class.getName());
    }

    default NodeItem asNodeItem() {
        if (this instanceof NodeItem) {
            return (NodeItem) this;
        }
        throw new ClassCastException("Cannot cast " + this + " of kind " + kind() + " to a " + NodeItem.class.getName());
    }

    default RelationItem asRelationItem() {
        if (this instanceof RelationItem) {
            return (RelationItem) this;
        }
        throw new ClassCastException("Cannot cast " + this + " of kind " + kind() + " to a " + RelationItem.class.getName());
    }

    interface ScalarItem extends GraphQueryResponseItem {

View on GitHub (pinned to e1c734241f)

Solutions

  1. Check item.kind() == Kind.SCALAR before calling asScalarItem()
  2. Switch over kind() and call the matching accessor: asScalarItem(), asNodeItem(), or asRelationItem()
  3. Fix the Cypher query so the expected column really returns a scalar value
  4. Catch ClassCastException around the accessor if kind-checking is impractical

Example fix

// before
String s = result.get(0).asScalarItem().asString();
// after
GraphQueryResponseItem item = result.get(0);
if (item.kind() == GraphQueryResponseItem.Kind.SCALAR) {
    String s = item.asScalarItem().asString();
} else {
    throw new IllegalStateException("Expected scalar, got " + item.kind());
}
Defensive patterns

Strategy: type-guard

Validate before calling

if (item.kind() != GraphQueryResponseItem.Kind.SCALAR) {
    throw new IllegalStateException("Expected SCALAR but got " + item.kind());
}

Type guard

boolean isScalar(GraphQueryResponseItem item) {
    return item.kind() == GraphQueryResponseItem.Kind.SCALAR;
}

Try / catch

try {
    ScalarItem s = item.asScalarItem();
} catch (ClassCastException e) {
    // fall back: inspect kind(), handle NODE/RELATION
}

Prevention

When it happens

Trigger: Calling item.asScalarItem() on an item whose kind() is NODE or RELATION — typically when a Cypher query returns a heterogeneous list of results (e.g. 'MATCH (n)-[r]->(m) RETURN n, r, m') and code blindly calls asScalarItem() on every element.

Common situations: Iterating graph query result rows without checking kind(); column-order changes in the Cypher RETURN clause after a query edit; expecting a scalar like a count or string but the query actually returns nodes or edges.

Related errors


AI-assisted analysis of quarkusio/quarkus@e1c734241f (2026-09-05). Data as JSON: /api/errors/c9edb8b49eeef3f7. Report an issue: GitHub.