From 7342b2c5168dc868c49241e22632a18ca63438bb Mon Sep 17 00:00:00 2001
From: Miles Ziemer <45497130+milesziemer@users.noreply.github.com>
Date: Mon, 22 May 2023 15:47:32 -0400
Subject: [PATCH] Fix dangling doc comments in structures (#1776)

Due to differences in how structures are parsed vs lists and maps,
dangling doc comments were not being handled properly on structure
shapes. Our tests were only verifying the behavior for maps, so it
wasn't caught initially. With these changes, proper warnings are
emitted when there are dangling doc comments in a structure shape.
---
 .../amazon/smithy/model/loader/IdlModelLoader.java       | 4 ++++
 .../loader/detects-invalid-documentation-comments.errors | 3 ++-
 .../loader/detects-invalid-documentation-comments.smithy | 9 ++++++++-
 3 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/smithy-model/src/main/java/software/amazon/smithy/model/loader/IdlModelLoader.java b/smithy-model/src/main/java/software/amazon/smithy/model/loader/IdlModelLoader.java
index 141db61b01c..db3ed62e71b 100644
--- a/smithy-model/src/main/java/software/amazon/smithy/model/loader/IdlModelLoader.java
+++ b/smithy-model/src/main/java/software/amazon/smithy/model/loader/IdlModelLoader.java
@@ -858,6 +858,10 @@ private void parseMember(LoadOperation.DefineShape operation, Set<String> define
         if (tokenizer.getCurrentToken() == IdlToken.RBRACE) {
             if (memberTraits.size() == 1
                     && memberTraits.get(0).getTraitType() == IdlTraitParser.TraitType.DOC_COMMENT) {
+                IdlTraitParser.Result danglingDocComment = memberTraits.get(0);
+                SourceLocation sourceLocation = danglingDocComment.getValue().getSourceLocation();
+                String value = danglingDocComment.getValue().toString();
+                emit(LoaderUtils.emitBadDocComment(sourceLocation, value));
                 return;
             }
         }
diff --git a/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/loader/detects-invalid-documentation-comments.errors b/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/loader/detects-invalid-documentation-comments.errors
index 7c4750454de..56aced838bb 100644
--- a/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/loader/detects-invalid-documentation-comments.errors
+++ b/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/loader/detects-invalid-documentation-comments.errors
@@ -3,4 +3,5 @@
 [WARNING] -: Found documentation comments ('///') attached to nothing. Documentation comments must appear on their own lines, directly before shapes and members, and before any traits. The invalid comments were: Invalid 3 (before use) | Model.BadDocumentationComment
 [WARNING] -: Found documentation comments ('///') attached to nothing. Documentation comments must appear on their own lines, directly before shapes and members, and before any traits. The invalid comments were: Invalid 4 (inside trait)\nInvalid 5 (not before traits)\nValid docs for Foo | Model.BadDocumentationComment
 [WARNING] -: Found documentation comments ('///') attached to nothing. Documentation comments must appear on their own lines, directly before shapes and members, and before any traits. The invalid comments were: Invalid 6 (not before traits)\nInvalid 7 (inside shape line)\nInvalid 8 (dangling) | Model.BadDocumentationComment
-[WARNING] -: Found documentation comments ('///') attached to nothing. Documentation comments must appear on their own lines, directly before shapes and members, and before any traits. The invalid comments were: Invalid 9 (dangling) | Model.BadDocumentationComment
+[WARNING] -: Found documentation comments ('///') attached to nothing. Documentation comments must appear on their own lines, directly before shapes and members, and before any traits. The invalid comments were: Invalid 9 (not before traits)\nInvalid 10 (dangling) | Model.BadDocumentationComment
+[WARNING] -: Found documentation comments ('///') attached to nothing. Documentation comments must appear on their own lines, directly before shapes and members, and before any traits. The invalid comments were: Invalid 11 (dangling) | Model.BadDocumentationComment
diff --git a/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/loader/detects-invalid-documentation-comments.smithy b/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/loader/detects-invalid-documentation-comments.smithy
index 1ea993785ad..8d98ccefdf7 100644
--- a/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/loader/detects-invalid-documentation-comments.smithy
+++ b/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/loader/detects-invalid-documentation-comments.smithy
@@ -24,4 +24,11 @@ map Foo
     /// Invalid 8 (dangling)
 }
 
-/// Invalid 9 (dangling)
+structure Bar {
+    @required
+    /// Invalid 9 (not before traits)
+    baz: Boolean
+    /// Invalid 10 (dangling)
+}
+
+/// Invalid 11 (dangling)