1111 */
1212package io .vertx .openapi .contract ;
1313
14- import static io .vertx .core .Future .failedFuture ;
15- import static io .vertx .openapi .contract .OpenAPIContractException .createInvalidContract ;
16-
1714import io .vertx .codegen .annotations .GenIgnore ;
1815import io .vertx .core .Future ;
1916import io .vertx .core .Promise ;
2421import io .vertx .json .schema .JsonSchemaValidationException ;
2522import io .vertx .openapi .contract .impl .OpenAPIContractImpl ;
2623import io .vertx .openapi .impl .Utils ;
24+
2725import java .util .HashMap ;
2826import java .util .Map ;
2927import java .util .stream .Collectors ;
3028
29+ import static io .vertx .core .Future .failedFuture ;
30+ import static io .vertx .openapi .contract .OpenAPIContractException .createInvalidContract ;
31+
3132/**
3233 * Builder for OpenAPIContracts.<br>
33- *
34+ * <p>
3435 * In the simplest case (you only have one contract) you must either provide a path to your openapi-contract in json
3536 * or yaml format or an already parsed openapi-spec as a {@link JsonObject}.
3637 * See {@link OpenAPIContractBuilder#setContractPath(String)} and {@link OpenAPIContractBuilder#setContract(JsonObject)}.
@@ -89,8 +90,8 @@ public OpenAPIContractBuilder setContract(JsonObject contract) {
8990
9091 /**
9192 * Puts a contract that is referenced by the main contract. This method can be
92- * called multiple times to add multiple referenced contracts . Overrides a previously
93- * added contract when the same key is used.
93+ * called multiple times to add multiple referenced additional contract parts . Overrides a previously
94+ * added additional contract part when the same key is used.
9495 *
9596 * @param key The unique key for the contract.
9697 * @param path The path to the contract.
@@ -121,11 +122,11 @@ public OpenAPIContractBuilder setAdditionalContractPartPaths(Map<String, String>
121122 }
122123
123124 /**
124- * Puts a contract that is referenced by the main contract. This method can be
125+ * Puts aa additional contract part that is referenced by the main contract. This method can be
125126 * called multiple times to add multiple referenced contracts.
126127 *
127128 * @param key The unique key for the contract.
128- * @param contractPart The contract object .
129+ * @param contractPart The additional contract part .
129130 * @return The builder, for a fluent interface
130131 */
131132 public OpenAPIContractBuilder putAdditionalContractPart (String key , JsonObject contractPart ) {
@@ -135,9 +136,9 @@ public OpenAPIContractBuilder putAdditionalContractPart(String key, JsonObject c
135136 }
136137
137138 /**
138- * Uses the contracts from the provided map to resolve referenced contracts .
139- * Replaces all previously put contracts by {@link #putAdditionalContractPart(String, JsonObject)}.
140- * If the same key is used also replaces the contracts set by {@link #putAdditionalContractPartPath(String, String)}
139+ * Uses the addtitional contract parts from the provided map to resolve referenced additional contract parts .
140+ * Replaces all previously put additional contract parts by {@link #putAdditionalContractPart(String, JsonObject)}.
141+ * If the same key is used also replaces the addtional contract part paths set by {@link #putAdditionalContractPartPath(String, String)}
141142 * and {@link #setAdditionalContractPartPaths(Map)}.
142143 *
143144 * @param contractParts A map that contains additional contract parts.
@@ -161,62 +162,62 @@ public Future<OpenAPIContract> build() {
161162
162163 if (contractPath == null && contract == null ) {
163164 return Future .failedFuture (new OpenAPIContractBuilderException (
164- "Neither a contract path or a contract is set. One of them must be set." ));
165+ "Neither a contract path or a contract is set. One of them must be set." ));
165166 }
166167
167168 Future <JsonObject > readContract = contractPath == null
168- ? Future .succeededFuture (contract )
169- : Utils .readYamlOrJson (vertx , contractPath );
169+ ? Future .succeededFuture (contract )
170+ : Utils .readYamlOrJson (vertx , contractPath );
170171
171172 var resolvedContractParts = readContractPaths ()
172- .map (r -> {
173- var all = new HashMap <>(additionalContractParts );
174- all .putAll (r );
175- return all ;
176- });
173+ .map (r -> {
174+ var all = new HashMap <>(additionalContractParts );
175+ all .putAll (r );
176+ return all ;
177+ });
177178
178179 return Future .all (readContract , resolvedContractParts )
179- .compose (composite -> {
180- JsonObject contract = composite .resultAt (0 );
181- Map <String , JsonObject > contractParts = composite .resultAt (1 );
182- return buildOpenAPIContract (contract , contractParts );
183- });
180+ .compose (composite -> {
181+ JsonObject contract = composite .resultAt (0 );
182+ Map <String , JsonObject > contractParts = composite .resultAt (1 );
183+ return buildOpenAPIContract (contract , contractParts );
184+ });
184185 }
185186
186187 private Future <OpenAPIContract > buildOpenAPIContract (JsonObject resolvedContract ,
187- Map <String , JsonObject > additionalContractParts ) {
188+ Map <String , JsonObject > additionalContractParts ) {
188189 OpenAPIVersion version = OpenAPIVersion .fromContract (resolvedContract );
189190 String baseUri = "app://" ;
190191
191192 ContextInternal ctx = (ContextInternal ) vertx .getOrCreateContext ();
192193 Promise <OpenAPIContract > promise = ctx .promise ();
193194
194195 version .getRepository (vertx , baseUri )
195- .compose (repository -> {
196- var validationFutures = additionalContractParts .entrySet ()
197- .stream ()
198- .map (entry -> version .validateAdditionalContractPart (vertx , repository , entry .getValue ())
199- .compose (v -> vertx .executeBlocking (
200- () -> repository .dereference (entry .getKey (), JsonSchema .of (entry .getKey (), entry .getValue ())))))
201- .collect (Collectors .toList ());
202- return Future .all (validationFutures ).map (repository );
203- }).compose (repository -> version .validateContract (vertx , repository , resolvedContract ).compose (res -> {
196+ .compose (repository -> {
197+ var validationFutures = additionalContractParts .entrySet ()
198+ .stream ()
199+ .map (entry -> version .validateAdditionalContractPart (vertx , repository , entry .getValue ())
200+ .compose (v -> vertx .executeBlocking (
201+ () -> repository .dereference (entry .getKey (), JsonSchema .of (entry .getKey (), entry .getValue ())))))
202+ .collect (Collectors .toList ());
203+ return Future .all (validationFutures ).map (repository );
204+ }).compose (repository -> version .validateContract (vertx , repository , resolvedContract ).compose (res -> {
204205 try {
205206 res .checkValidity ();
206207 return version .resolve (vertx , repository , resolvedContract );
207208 } catch (JsonSchemaValidationException | UnsupportedOperationException e ) {
208209 return failedFuture (createInvalidContract (null , e ));
209210 }
210211 })
211- .map (resolvedSpec -> new OpenAPIContractImpl (resolvedSpec , version , repository )))
212- .recover (e -> {
213- // Convert any non-openapi exceptions into an OpenAPIContractException
214- if (e instanceof OpenAPIContractException ) {
215- return failedFuture (e );
216- }
217- return failedFuture (
218- createInvalidContract ("Found issue in specification for reference: " + e .getMessage (), e ));
219- }).onComplete (promise );
212+ .map (resolvedSpec -> new OpenAPIContractImpl (resolvedSpec , version , repository )))
213+ .recover (e -> {
214+ // Convert any non-openapi exceptions into an OpenAPIContractException
215+ if (e instanceof OpenAPIContractException ) {
216+ return failedFuture (e );
217+ }
218+ return failedFuture (
219+ createInvalidContract ("Found issue in specification for reference: " + e .getMessage (), e ));
220+ }).onComplete (promise );
220221
221222 return promise .future ();
222223 }
@@ -228,9 +229,9 @@ private Future<Map<String, JsonObject>> readContractPaths() {
228229 var read = new HashMap <String , JsonObject >();
229230 return Future .all (additionalContractPartPaths .entrySet ().stream ()
230231 .map (e -> Utils .readYamlOrJson (vertx , e .getValue ())
231- .map (c -> read .put (e .getKey (), c )))
232+ .map (c -> read .put (e .getKey (), c )))
232233 .collect (Collectors .toList ()))
233- .map (ign -> read );
234+ .map (ign -> read );
234235 }
235236
236237}
0 commit comments