1919import static software .amazon .smithy .typescript .codegen .integration .RuntimeClientPlugin .Convention .HAS_CONFIG ;
2020import static software .amazon .smithy .typescript .codegen .integration .RuntimeClientPlugin .Convention .HAS_MIDDLEWARE ;
2121
22- import java .util .ArrayList ;
2322import java .util .Collections ;
2423import java .util .HashSet ;
2524import java .util .List ;
4443import software .amazon .smithy .model .shapes .TimestampShape ;
4544import software .amazon .smithy .model .traits .DeprecatedTrait ;
4645import software .amazon .smithy .model .traits .DocumentationTrait ;
46+ import software .amazon .smithy .model .traits .EndpointTrait ;
4747import software .amazon .smithy .model .traits .HttpHeaderTrait ;
4848import software .amazon .smithy .model .traits .HttpPayloadTrait ;
4949import software .amazon .smithy .model .traits .StreamingTrait ;
50+ import software .amazon .smithy .model .transform .ModelTransformer ;
51+ import software .amazon .smithy .rulesengine .traits .EndpointRuleSetTrait ;
5052import software .amazon .smithy .typescript .codegen .LanguageTarget ;
5153import software .amazon .smithy .typescript .codegen .TypeScriptDependency ;
5254import software .amazon .smithy .typescript .codegen .TypeScriptSettings ;
@@ -81,22 +83,16 @@ public final class AddS3Config implements TypeScriptIntegration {
8183 + "For more information, please go to https://github.com/aws/aws-sdk-js-v3#known-issues</p>" ;
8284
8385 /**
84- * This removes the host prefix from the endpoint traits of the model.
85- *
86- */
87- public static Model removeHostPrefixTrait (Model model , TypeScriptSettings settings ) {
88- boolean checkEndPointRuleSet = model .hasTrait (EndpointRuleSetTrait .class )
89- Set <Shape > shapesWithTraits = model .getShapesWithTrait (EndpointTrait .class );
90- Model .Builder mb = model .toBuilder ();
91- for (Shape shape : shapesWithTraits ) {
92- OperationShape .Builder ob = OperationShape .shapeToBuilder (shape .asOperationShape ().get ());
93- ob .removeTrait (EndpointTrait .ID );
94- OperationShape operation = ob .build ();
95- mb .removeShape (operation .getId ());
96- mb .addShape (operation );
97- }
98- return mb .build ();
99-
86+ * Remove the hostPrefix functionality by removing the endpoint traits.
87+ * Only applied if endpointRuleSet trait present on S3/S3Control services.
88+ */
89+ public static Shape removeHostPrefixTrait (Shape shape ) {
90+ return shape .asOperationShape ()
91+ .map (OperationShape ::shapeToBuilder )
92+ .map (builder -> ((OperationShape .Builder ) builder ).removeTrait (EndpointTrait .ID ))
93+ .map (OperationShape .Builder ::build )
94+ .map (s -> (Shape ) s )
95+ .orElse (shape );
10096 }
10197
10298 @ Override
@@ -114,9 +110,9 @@ public Model preprocessModel(Model model, TypeScriptSettings settings) {
114110 if (!isS3 (serviceShape )) {
115111 return model ;
116112 }
117- removeHostPrefixTrait (model , settings );
118113
119114 Model .Builder modelBuilder = model .toBuilder ();
115+ boolean hasRuleset = !model .getServiceShapesWithTrait (EndpointRuleSetTrait .class ).isEmpty ();
120116
121117 TopDownIndex topDownIndex = TopDownIndex .of (model );
122118 Set <StructureShape > inputShapes = new HashSet <>();
@@ -227,7 +223,13 @@ public Model preprocessModel(Model model, TypeScriptSettings settings) {
227223 }
228224 }
229225
230- return modelBuilder .addShapes (inputShapes ).build ();
226+ Model builtModel = modelBuilder .addShapes (inputShapes ).build ();
227+ if (hasRuleset ) {
228+ ModelTransformer .create ().mapShapes (
229+ builtModel , AddS3Config ::removeHostPrefixTrait
230+ );
231+ }
232+ return builtModel ;
231233 }
232234
233235 @ Override
0 commit comments