diff --git a/src/main/java/software/amazon/smithy/lsp/ext/FileCachingCollector.java b/src/main/java/software/amazon/smithy/lsp/ext/FileCachingCollector.java index 4298828e..ec9c8ef5 100644 --- a/src/main/java/software/amazon/smithy/lsp/ext/FileCachingCollector.java +++ b/src/main/java/software/amazon/smithy/lsp/ext/FileCachingCollector.java @@ -239,9 +239,12 @@ private Optional getOperationForInlinedInputOrOutput(Shape shape ) { String suffix = getOperationInputOrOutputSuffix(shape, preamble); String shapeName = shape.getId().getName(); + String matchingOperationName = shapeName.substring(0, shapeName.length() - suffix.length()); + ShapeId matchingOperationId = ShapeId.fromParts(shape.getId().getNamespace(), matchingOperationName); + return model.shapes(OperationShape.class) - .filter(operationShape -> operationShape.getId().getName().equals(matchingOperationName)) + .filter(operationShape -> operationShape.getId().equals(matchingOperationId)) .findFirst() .filter(operation -> shapeWasDefinedInline(operation, shape, modelFile)); } diff --git a/src/test/java/software/amazon/smithy/lsp/ext/SmithyProjectTest.java b/src/test/java/software/amazon/smithy/lsp/ext/SmithyProjectTest.java index 195d400a..12c70c3b 100644 --- a/src/test/java/software/amazon/smithy/lsp/ext/SmithyProjectTest.java +++ b/src/test/java/software/amazon/smithy/lsp/ext/SmithyProjectTest.java @@ -149,6 +149,23 @@ public void allowsEmptyStructsWithMixins() throws Exception { } } + // https://github.com/awslabs/smithy-language-server/issues/110 + // Note: This test is flaky, it may succeed even if the code being tested is incorrect. + @Test + public void handlesSameOperationNameBetweenNamespaces() throws Exception { + Path baseDir = Paths.get(SmithyProjectTest.class.getResource("models/operation-name-conflict").toURI()); + Path modelA = baseDir.resolve("a.smithy"); + Path modelB = baseDir.resolve("b.smithy"); + List modelFiles = ListUtils.of(modelA, modelB); + + try (Harness hs = Harness.create(SmithyBuildExtensions.builder().build(), modelFiles)) { + Map locationMap = hs.getProject().getLocations(); + + correctLocation(locationMap, "a#HelloWorld", 4, 0, 13, 1); + correctLocation(locationMap, "b#HelloWorld", 6, 0, 15, 1); + } + } + @Test public void definitionLocationsV1() throws Exception { Path baseDir = Paths.get(SmithyProjectTest.class.getResource("models/v1").toURI()); diff --git a/src/test/resources/software/amazon/smithy/lsp/ext/models/operation-name-conflict/a.smithy b/src/test/resources/software/amazon/smithy/lsp/ext/models/operation-name-conflict/a.smithy new file mode 100644 index 00000000..40b1aad0 --- /dev/null +++ b/src/test/resources/software/amazon/smithy/lsp/ext/models/operation-name-conflict/a.smithy @@ -0,0 +1,14 @@ +$version: "2" + +namespace a + +operation HelloWorld { + input := { + @required + name: String + } + output := { + @required + name: String + } +} diff --git a/src/test/resources/software/amazon/smithy/lsp/ext/models/operation-name-conflict/b.smithy b/src/test/resources/software/amazon/smithy/lsp/ext/models/operation-name-conflict/b.smithy new file mode 100644 index 00000000..b37f9092 --- /dev/null +++ b/src/test/resources/software/amazon/smithy/lsp/ext/models/operation-name-conflict/b.smithy @@ -0,0 +1,16 @@ +$version: "2" + +namespace b + +string Ignored + +operation HelloWorld { + input := { + @required + name: String + } + output := { + @required + name: String + } +}