From 53a83244ef6666a5927475ea0d7681ff7cfddc47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Koz=C5=82owski?= Date: Tue, 30 May 2023 01:06:43 +0200 Subject: [PATCH 1/3] Reproduce (non-deterministically) #110 --- .../amazon/smithy/lsp/ext/SmithyProjectTest.java | 15 +++++++++++++++ .../ext/models/operation-name-conflict/a.smithy | 14 ++++++++++++++ .../ext/models/operation-name-conflict/b.smithy | 16 ++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 src/test/resources/software/amazon/smithy/lsp/ext/models/operation-name-conflict/a.smithy create mode 100644 src/test/resources/software/amazon/smithy/lsp/ext/models/operation-name-conflict/b.smithy 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 195d400..a865e23 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,21 @@ public void allowsEmptyStructsWithMixins() throws Exception { } } + // https://github.com/awslabs/smithy-language-server/issues/110 + @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(); + + assertNotNull(locationMap.get(ShapeId.from("b#HelloWorld"))); + } + } + @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 0000000..40b1aad --- /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 0000000..b37f909 --- /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 + } +} From 4a32c78bd30ad19a7edd18ec1198417138c7a1ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Koz=C5=82owski?= Date: Tue, 30 May 2023 01:39:47 +0200 Subject: [PATCH 2/3] Fix #110 --- .../software/amazon/smithy/lsp/ext/FileCachingCollector.java | 5 ++++- .../software/amazon/smithy/lsp/ext/SmithyProjectTest.java | 4 +++- 2 files changed, 7 insertions(+), 2 deletions(-) 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 4298828..ec9c8ef 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 a865e23..19a1832 100644 --- a/src/test/java/software/amazon/smithy/lsp/ext/SmithyProjectTest.java +++ b/src/test/java/software/amazon/smithy/lsp/ext/SmithyProjectTest.java @@ -150,6 +150,7 @@ 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()); @@ -160,7 +161,8 @@ public void handlesSameOperationNameBetweenNamespaces() throws Exception { try (Harness hs = Harness.create(SmithyBuildExtensions.builder().build(), modelFiles)) { Map locationMap = hs.getProject().getLocations(); - assertNotNull(locationMap.get(ShapeId.from("b#HelloWorld"))); + correctLocation(locationMap, "a#HelloWorld", 4, 0, 13, 1); + correctLocation(locationMap, "b#HelloWorld", 6, 0, 15, 1); } } From 9453cccee9316d6e6aab1898f4d43516b61e8b4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Koz=C5=82owski?= Date: Tue, 30 May 2023 01:44:28 +0200 Subject: [PATCH 3/3] Remove extra spaces --- .../software/amazon/smithy/lsp/ext/SmithyProjectTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 19a1832..12c70c3 100644 --- a/src/test/java/software/amazon/smithy/lsp/ext/SmithyProjectTest.java +++ b/src/test/java/software/amazon/smithy/lsp/ext/SmithyProjectTest.java @@ -161,8 +161,8 @@ public void handlesSameOperationNameBetweenNamespaces() throws Exception { 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); + correctLocation(locationMap, "a#HelloWorld", 4, 0, 13, 1); + correctLocation(locationMap, "b#HelloWorld", 6, 0, 15, 1); } }