This is an automated email from the ASF dual-hosted git repository.
Cole-Greer pushed a commit to branch 3.7-dev
in repository https://gitbox.apache.org/repos/asf/tinkerpop.git
The following commit(s) were added to refs/heads/3.7-dev by this push:
new 816cfc886d TINKERPOP-3271 `subgraph()` throws a descriptive error for
non-Edge input (#3556)
816cfc886d is described below
commit 816cfc886d6aa896ba19db443820a1240d307194
Author: Guian Gumpac <[email protected]>
AuthorDate: Thu Jul 23 17:04:49 2026 -0700
TINKERPOP-3271 `subgraph()` throws a descriptive error for non-Edge input
(#3556)
Assisted-by: Kiro: Claude Opus 4.8
---
CHANGELOG.asciidoc | 1 +
.../traversal/step/sideEffect/SubgraphStep.java | 10 +++-
.../sideEffect/TinkerGraphSubgraphStepTest.java | 60 ++++++++++++++++++++++
3 files changed, 70 insertions(+), 1 deletion(-)
diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc
index f316877f25..b7151a6a45 100644
--- a/CHANGELOG.asciidoc
+++ b/CHANGELOG.asciidoc
@@ -25,6 +25,7 @@
image::https://raw.githubusercontent.com/apache/tinkerpop/master/docs/static/ima
[[release-3-7-7]]
=== TinkerPop 3.7.7 (Release Date: NOT OFFICIALLY RELEASED YET)
+* Fixed `subgraph()` to throw a descriptive error identifying the required
`Edge` input instead of an internal `ClassCastException` when the traversal
produces a non-edge value.
* Fixed `PeerPressure.property_name` in `gremlin-python` incorrectly mapping
to the `pageRank` property name token.
* Added `NextN(n)` to `Traversal` in `gremlin-go` for batched result
iteration, providing API parity with `next(n)` in the Java, Python, and .NET
GLVs.
* Added `next(n)` to `Traversal` in `gremlin-javascript` for batched result
iteration, providing API parity with `next(n)` in the Java, Python, and .NET
GLVs.
diff --git
a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/sideEffect/SubgraphStep.java
b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/sideEffect/SubgraphStep.java
index 35c7c51df8..b405ae945a 100644
---
a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/sideEffect/SubgraphStep.java
+++
b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/sideEffect/SubgraphStep.java
@@ -78,7 +78,15 @@ public final class SubgraphStep extends SideEffectStep<Edge>
implements SideEffe
subgraphSupportsMetaProperties =
subgraph.features().vertex().supportsMetaProperties();
- addEdgeToSubgraph(traverser.get());
+ // subgraph() produces an edge-induced subgraph and therefore requires
Edge input. if the traverser value is
+ // not an Edge we throw a descriptive error.
+ final Object value = traverser.get();
+ if (!(value instanceof Edge))
+ throw new IllegalStateException(String.format(
+ "subgraph() requires Edge input but encountered %s; use an
edge step such as outE(), inE(), or bothE()",
+ null == value ? "null" :
value.getClass().getSimpleName()));
+
+ addEdgeToSubgraph((Edge) value);
}
@Override
diff --git
a/tinkergraph-gremlin/src/test/java/org/apache/tinkerpop/gremlin/tinkergraph/process/traversal/step/sideEffect/TinkerGraphSubgraphStepTest.java
b/tinkergraph-gremlin/src/test/java/org/apache/tinkerpop/gremlin/tinkergraph/process/traversal/step/sideEffect/TinkerGraphSubgraphStepTest.java
new file mode 100644
index 0000000000..c554bcbb67
--- /dev/null
+++
b/tinkergraph-gremlin/src/test/java/org/apache/tinkerpop/gremlin/tinkergraph/process/traversal/step/sideEffect/TinkerGraphSubgraphStepTest.java
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package
org.apache.tinkerpop.gremlin.tinkergraph.process.traversal.step.sideEffect;
+
+import
org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
+import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
+import org.junit.Test;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.containsString;
+import static org.junit.Assert.fail;
+
+/**
+ * Verifies {@code subgraph()} throws a descriptive {@link
IllegalStateException} for non-{@code Edge} input.
+ */
+public class TinkerGraphSubgraphStepTest {
+
+ @Test
+ public void
shouldThrowDescriptiveExceptionWhenSubgraphReceivesNonEdgePrimitiveInput()
throws Exception {
+ try (final TinkerGraph graph = TinkerGraph.open()) {
+ final GraphTraversalSource g = graph.traversal();
+ try {
+ g.inject(1).subgraph("sg").iterate();
+ fail("subgraph() should throw an IllegalStateException when
the input is not an Edge");
+ } catch (IllegalStateException ise) {
+ assertThat(ise.getMessage(), containsString("requires Edge
input"));
+ assertThat(ise.getMessage(), containsString("Integer"));
+ }
+ }
+ }
+
+ @Test
+ public void
shouldThrowDescriptiveExceptionWhenSubgraphReceivesVertexInput() throws
Exception {
+ try (final TinkerGraph graph = TinkerGraph.open()) {
+ final GraphTraversalSource g = graph.traversal();
+ try {
+ g.addV().subgraph("sg").iterate();
+ fail("subgraph() should throw an IllegalStateException when
the input is not an Edge");
+ } catch (IllegalStateException ise) {
+ assertThat(ise.getMessage(), containsString("requires Edge
input"));
+ }
+ }
+ }
+}