Skip to content

Commit e334862

Browse files
committed
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.
1 parent 0b9ffc4 commit e334862

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

smithy-model/src/main/java/software/amazon/smithy/model/shapes/SmithyIdlModelSerializer.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,8 @@ private Pair<String, String> determineInlineSuffixes(Model fullModel, Collection
243243
return Pair.of(inlineInputSuffix, inlineOutputSuffix);
244244
}
245245

246-
Set<String> inputSuffixes = new HashSet<>();
247-
Set<String> outputSuffixes = new HashSet<>();
246+
Map<String, Integer> inputSuffixes = new LinkedHashMap<>();
247+
Map<String, Integer> outputSuffixes = new LinkedHashMap<>();
248248
for (Shape shape : shapes) {
249249
if (!shape.isOperationShape()) {
250250
continue;
@@ -255,15 +255,28 @@ private Pair<String, String> determineInlineSuffixes(Model fullModel, Collection
255255
StructureShape output = fullModel.expectShape(operation.getOutputShape(), StructureShape.class);
256256

257257
if (shapes.contains(input) && input.getId().getName().startsWith(operation.getId().getName())) {
258-
inputSuffixes.add(input.getId().getName().substring(operation.getId().getName().length()));
258+
String inputSuffix = input.getId().getName().substring(operation.getId().getName().length());
259+
int inputCount = inputSuffixes.getOrDefault(inputSuffix, 0);
260+
inputSuffixes.put(inputSuffix, ++inputCount);
259261
}
260262

261263
if (shapes.contains(output) && output.getId().getName().startsWith(operation.getId().getName())) {
262-
outputSuffixes.add(output.getId().getName().substring(operation.getId().getName().length()));
264+
String outputSuffix = output.getId().getName().substring(operation.getId().getName().length());
265+
int outputCount = outputSuffixes.getOrDefault(outputSuffix, 0);
266+
outputSuffixes.put(outputSuffix, ++outputCount);
263267
}
264268
}
265-
String inputSuffix = inputSuffixes.size() == 1 ? inputSuffixes.iterator().next() : inlineInputSuffix;
266-
String outputSuffix = outputSuffixes.size() == 1 ? outputSuffixes.iterator().next() : inlineOutputSuffix;
269+
270+
String inputSuffix = inputSuffixes.entrySet()
271+
.stream()
272+
.max(Map.Entry.comparingByValue())
273+
.map(Map.Entry::getKey)
274+
.orElse(inlineInputSuffix);
275+
String outputSuffix = outputSuffixes.entrySet()
276+
.stream()
277+
.max(Map.Entry.comparingByValue())
278+
.map(Map.Entry::getKey)
279+
.orElse(inlineOutputSuffix);
267280
return Pair.of(inputSuffix, outputSuffix);
268281
}
269282

smithy-model/src/test/resources/software/amazon/smithy/model/shapes/idl-serialization/inferred-io/shared.smithy

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ $operationOutputSuffix: "Response"
44

55
namespace com.example
66

7+
operation NotShared {
8+
input: NotSharedInput
9+
output: NotSharedOutput
10+
}
11+
712
operation SharedCustomA {
813
input := {}
914
output := {}
@@ -13,3 +18,9 @@ operation SharedCustomB {
1318
input := {}
1419
output := {}
1520
}
21+
22+
@input
23+
structure NotSharedInput {}
24+
25+
@output
26+
structure NotSharedOutput {}

0 commit comments

Comments
 (0)