44 */
55package software .amazon .smithy .openapi .fromsmithy .mappers ;
66
7+ import java .util .ArrayList ;
8+ import java .util .LinkedHashMap ;
79import java .util .List ;
10+ import java .util .Map ;
811import java .util .Set ;
912import java .util .logging .Logger ;
1013import java .util .stream .Collectors ;
14+ import software .amazon .smithy .aws .traits .HttpChecksumTrait ;
15+ import software .amazon .smithy .model .shapes .Shape ;
1116import software .amazon .smithy .model .shapes .ShapeId ;
17+ import software .amazon .smithy .model .traits .EndpointTrait ;
18+ import software .amazon .smithy .model .traits .HostLabelTrait ;
1219import software .amazon .smithy .model .traits .Trait ;
1320import software .amazon .smithy .openapi .OpenApiException ;
1421import software .amazon .smithy .openapi .fromsmithy .Context ;
1522import software .amazon .smithy .openapi .fromsmithy .OpenApiMapper ;
1623import software .amazon .smithy .openapi .model .OpenApi ;
17- import software .amazon .smithy .utils .Pair ;
1824import software .amazon .smithy .utils .SetUtils ;
1925import software .amazon .smithy .utils .SmithyInternalApi ;
2026
2531@ SmithyInternalApi
2632public final class UnsupportedTraits implements OpenApiMapper {
2733 private static final Logger LOGGER = Logger .getLogger (UnsupportedTraits .class .getName ());
28- private static final Set <String > TRAITS = SetUtils .of ("endpoint" , "hostLabel" , "aws.protocols#httpChecksum" );
34+ private static final Set <ShapeId > TRAITS = SetUtils .of (
35+ EndpointTrait .ID ,
36+ HostLabelTrait .ID ,
37+ HttpChecksumTrait .ID );
2938
3039 @ Override
3140 public byte getOrder () {
@@ -34,25 +43,34 @@ public byte getOrder() {
3443
3544 @ Override
3645 public void before (Context <? extends Trait > context , OpenApi .Builder builder ) {
37- List <Pair <ShapeId , List <String >>> violations = context .getModel ()
38- .shapes ()
39- .map (shape -> Pair .of (shape .getId (),
40- TRAITS .stream ()
41- .filter (trait -> shape .findTrait (trait ).isPresent ())
42- .collect (Collectors .toList ())))
43- .filter (pair -> pair .getRight ().size () > 0 )
44- .collect (Collectors .toList ());
46+ Map <ShapeId , List <ShapeId >> violations = new LinkedHashMap <>();
47+ for (Shape shape : context .getModel ().toSet ()) {
48+ List <ShapeId > unsupportedTraits = new ArrayList <>(TRAITS .size ());
49+ for (ShapeId trait : TRAITS ) {
50+ if (shape .hasTrait (trait )) {
51+ unsupportedTraits .add (trait );
52+ }
53+ }
54+ if (!unsupportedTraits .isEmpty ()) {
55+ violations .put (shape .getId (), unsupportedTraits );
56+ }
57+ }
4558
4659 if (violations .isEmpty ()) {
4760 return ;
4861 }
4962
5063 StringBuilder message = new StringBuilder (
5164 "Encountered unsupported Smithy traits when converting to OpenAPI:" );
52- violations .forEach (pair -> message .append (String .format (
53- " (`%s`: [%s])" ,
54- pair .getLeft (),
55- String .join ("," , pair .getRight ()))));
65+ for (Map .Entry <ShapeId , List <ShapeId >> entry : violations .entrySet ()) {
66+ message .append (String .format (
67+ " (`%s`: [`%s`])" ,
68+ entry .getKey (),
69+ entry .getValue ()
70+ .stream ()
71+ .map (ShapeId ::toString )
72+ .collect (Collectors .joining ("`, `" ))));
73+ }
5674 message .append (". While these traits may still be meaningful to clients and servers using the Smithy "
5775 + "model directly, they have no direct corollary in OpenAPI and can not be included in "
5876 + "the generated model." );
0 commit comments