55
66package software .amazon .smithy .java .dynamicclient ;
77
8+ import java .util .ArrayList ;
9+ import java .util .Collections ;
10+ import java .util .HashSet ;
811import java .util .List ;
912import java .util .Set ;
13+ import java .util .function .BiConsumer ;
1014import software .amazon .smithy .java .core .schema .ApiOperation ;
1115import software .amazon .smithy .java .core .schema .ApiService ;
1216import software .amazon .smithy .java .core .schema .Schema ;
1519import software .amazon .smithy .java .core .serde .TypeRegistry ;
1620import software .amazon .smithy .java .dynamicschemas .SchemaConverter ;
1721import software .amazon .smithy .java .dynamicschemas .WrappedDocument ;
22+ import software .amazon .smithy .model .Model ;
23+ import software .amazon .smithy .model .knowledge .ServiceIndex ;
24+ import software .amazon .smithy .model .shapes .OperationShape ;
25+ import software .amazon .smithy .model .shapes .ServiceShape ;
1826import software .amazon .smithy .model .shapes .ShapeId ;
1927
20- final class DynamicOperation implements ApiOperation <WrappedDocument , WrappedDocument > {
28+ public final class DynamicOperation implements ApiOperation <WrappedDocument , WrappedDocument > {
2129
2230 private final ApiService service ;
2331 private final Schema operationSchema ;
@@ -27,7 +35,7 @@ final class DynamicOperation implements ApiOperation<WrappedDocument, WrappedDoc
2735 private final TypeRegistry typeRegistry ;
2836 private final List <ShapeId > effectiveAuthSchemes ;
2937
30- public DynamicOperation (
38+ DynamicOperation (
3139 ApiService service ,
3240 Schema operationSchema ,
3341 Schema inputSchema ,
@@ -94,4 +102,54 @@ public TypeRegistry errorRegistry() {
94102 public List <ShapeId > effectiveAuthSchemes () {
95103 return effectiveAuthSchemes ;
96104 }
105+
106+ public static DynamicOperation create (
107+ OperationShape shape ,
108+ SchemaConverter schemaConverter ,
109+ Model model ,
110+ ServiceShape service ,
111+ TypeRegistry serviceErrorRegistry ,
112+ BiConsumer <ShapeId , TypeRegistry .Builder > registerErrorCallback
113+ ) {
114+ var operationSchema = schemaConverter .getSchema (shape );
115+
116+ List <ShapeId > authSchemes = new ArrayList <>();
117+ for (var trait : ServiceIndex .of (model ).getEffectiveAuthSchemes (service ).values ()) {
118+ authSchemes .add (trait .toShapeId ());
119+ }
120+
121+ var inputSchema = schemaConverter .getSchema (model .expectShape (shape .getInputShape ()));
122+ var outputSchema = schemaConverter .getSchema (model .expectShape (shape .getOutputShape ()));
123+
124+ // Default to using the service registry.
125+ var registry = serviceErrorRegistry ;
126+
127+ var errorSchemas = new HashSet <Schema >();
128+ // Create a type registry that is able to deserialize errors using schemas.
129+ if (!shape .getErrors ().isEmpty ()) {
130+ var registryBuilder = TypeRegistry .builder ();
131+ for (var e : shape .getErrors ()) {
132+ registerErrorCallback .accept (e , registryBuilder );
133+ errorSchemas .add (schemaConverter .getSchema (model .expectShape (e )));
134+ }
135+ // Compose the operation errors with the service errors.
136+ registry = TypeRegistry .compose (registryBuilder .build (), serviceErrorRegistry );
137+ }
138+
139+ var serviceSchema = schemaConverter .getSchema (service );
140+ var apiService = new ApiService () {
141+ @ Override
142+ public Schema schema () {
143+ return serviceSchema ;
144+ }
145+ };
146+ return new DynamicOperation (
147+ apiService ,
148+ operationSchema ,
149+ inputSchema ,
150+ outputSchema ,
151+ Collections .unmodifiableSet (errorSchemas ),
152+ registry ,
153+ authSchemes );
154+ }
97155}
0 commit comments