Repository: smithy-lang/smithy
Pull request #2397 (2024-09-17 05:23:18)

Use most common suffix for IDL inline IO
This commit updates IDL serialization to use the most common suffix for serializing IDL models when inlining input/output shapes. Before this update, the first suffix encountered would be used, even if it was only used that one time.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.



Patch:
diff --git a/smithy-model/src/main/java/software/amazon/smithy/model/shapes/SmithyIdlModelSerializer.java b/smithy-model/src/main/java/software/amazon/smithy/model/shapes/SmithyIdlModelSerializer.java
index 53000a44c..19a713aff 100644
--- a/smithy-model/src/main/java/software/amazon/smithy/model/shapes/SmithyIdlModelSerializer.java
+++ b/smithy-model/src/main/java/software/amazon/smithy/model/shapes/SmithyIdlModelSerializer.java
@@ -243,8 +243,8 @@ public final class SmithyIdlModelSerializer {
             return Pair.of(inlineInputSuffix, inlineOutputSuffix);
         }
 
-        Set<String> inputSuffixes = new HashSet<>();
-        Set<String> outputSuffixes = new HashSet<>();
+        Map<String, Integer> inputSuffixes = new LinkedHashMap<>();
+        Map<String, Integer> outputSuffixes = new LinkedHashMap<>();
         for (Shape shape : shapes) {
             if (!shape.isOperationShape()) {
                 continue;
@@ -255,15 +255,28 @@ public final class SmithyIdlModelSerializer {
             StructureShape output = fullModel.expectShape(operation.getOutputShape(), StructureShape.class);
 
             if (shapes.contains(input) && input.getId().getName().startsWith(operation.getId().getName())) {
-                inputSuffixes.add(input.getId().getName().substring(operation.getId().getName().length()));
+                String inputSuffix = input.getId().getName().substring(operation.getId().getName().length());
+                int inputCount = inputSuffixes.getOrDefault(inputSuffix, 0);
+                inputSuffixes.put(inputSuffix, ++inputCount);
             }
 
             if (shapes.contains(output) && output.getId().getName().startsWith(operation.getId().getName())) {
-                outputSuffixes.add(output.getId().getName().substring(operation.getId().getName().length()));
+                String outputSuffix = output.getId().getName().substring(operation.getId().getName().length());
+                int outputCount = outputSuffixes.getOrDefault(outputSuffix, 0);
+                outputSuffixes.put(outputSuffix, ++outputCount);
             }
         }
-        String inputSuffix = inputSuffixes.size() == 1 ? inputSuffixes.iterator().next() : inlineInputSuffix;
-        String outputSuffix = outputSuffixes.size() == 1 ? outputSuffixes.iterator().next() : inlineOutputSuffix;
+
+        String inputSuffix = inputSuffixes.entrySet()
+                .stream()
+                .max(Map.Entry.comparingByValue())
+                .map(Map.Entry::getKey)
+                .orElse(inlineInputSuffix);
+        String outputSuffix = outputSuffixes.entrySet()
+                .stream()
+                .max(Map.Entry.comparingByValue())
+                .map(Map.Entry::getKey)
+                .orElse(inlineOutputSuffix);
         return Pair.of(inputSuffix, outputSuffix);
     }
 


Test patch:
diff --git a/smithy-model/src/test/resources/software/amazon/smithy/model/shapes/idl-serialization/inferred-io/shared.smithy b/smithy-model/src/test/resources/software/amazon/smithy/model/shapes/idl-serialization/inferred-io/shared.smithy
index 8dc75dc77..b8d45df4d 100644
--- a/smithy-model/src/test/resources/software/amazon/smithy/model/shapes/idl-serialization/inferred-io/shared.smithy
+++ b/smithy-model/src/test/resources/software/amazon/smithy/model/shapes/idl-serialization/inferred-io/shared.smithy
@@ -4,6 +4,11 @@ $operationOutputSuffix: "Response"
 
 namespace com.example
 
+operation NotShared {
+    input: NotSharedInput
+    output: NotSharedOutput
+}
+
 operation SharedCustomA {
     input := {}
     output := {}
@@ -13,3 +18,9 @@ operation SharedCustomB {
     input := {}
     output := {}
 }
+
+@input
+structure NotSharedInput {}
+
+@output
+structure NotSharedOutput {}