1212
1313package io .vertx .openapi .contract ;
1414
15+ import static io .vertx .core .Future .failedFuture ;
16+ import static io .vertx .openapi .contract .OpenAPIContractException .createInvalidContract ;
17+ import static io .vertx .openapi .impl .Utils .readYamlOrJson ;
18+ import static java .util .Collections .emptyMap ;
19+
1520import io .vertx .codegen .annotations .Nullable ;
1621import io .vertx .codegen .annotations .VertxGen ;
1722import io .vertx .core .Future ;
2429import io .vertx .json .schema .JsonSchemaValidationException ;
2530import io .vertx .json .schema .SchemaRepository ;
2631import io .vertx .openapi .contract .impl .OpenAPIContractImpl ;
27-
2832import java .util .ArrayList ;
2933import java .util .HashMap ;
3034import java .util .List ;
3135import java .util .Map ;
3236
33- import static io .vertx .core .Future .failedFuture ;
34- import static io .vertx .openapi .contract .OpenAPIContractException .createInvalidContract ;
35- import static io .vertx .openapi .impl .Utils .readYamlOrJson ;
36- import static java .util .Collections .emptyMap ;
37-
3837@ VertxGen
3938public interface OpenAPIContract {
4039
@@ -72,7 +71,7 @@ static Future<OpenAPIContract> from(Vertx vertx, JsonObject unresolvedContract)
7271 * @return A succeeded {@link Future} holding an {@link OpenAPIContract} instance, otherwise a failed {@link Future}.
7372 */
7473 static Future <OpenAPIContract > from (Vertx vertx , String unresolvedContractPath ,
75- Map <String , String > additionalContractFiles ) {
74+ Map <String , String > additionalContractFiles ) {
7675
7776 Map <String , Future <JsonObject >> jsonFilesFuture = new HashMap <>();
7877 jsonFilesFuture .put (unresolvedContractPath , readYamlOrJson (vertx , unresolvedContractPath ));
@@ -97,7 +96,7 @@ static Future<OpenAPIContract> from(Vertx vertx, String unresolvedContractPath,
9796 * @return A succeeded {@link Future} holding an {@link OpenAPIContract} instance, otherwise a failed {@link Future}.
9897 */
9998 static Future <OpenAPIContract > from (Vertx vertx , JsonObject unresolvedContract ,
100- Map <String , JsonObject > additionalContractFiles ) {
99+ Map <String , JsonObject > additionalContractFiles ) {
101100 if (unresolvedContract == null ) {
102101 return failedFuture (createInvalidContract ("Spec must not be null" ));
103102 }
@@ -109,36 +108,36 @@ static Future<OpenAPIContract> from(Vertx vertx, JsonObject unresolvedContract,
109108 Promise <OpenAPIContract > promise = ctx .promise ();
110109
111110 version .getRepository (vertx , baseUri )
112- .compose (repository -> {
113- List <Future <?>> validationFutures = new ArrayList <>(additionalContractFiles .size ());
114- for (String ref : additionalContractFiles .keySet ()) {
115- // Todo: As soon a more modern Java version is used the validate part could be extracted in a private static
116- // method and reused below.
117- JsonObject file = additionalContractFiles .get (ref );
118- Future <?> validationFuture = version .validateAdditionalContractFile (vertx , repository , file )
119- .compose (v -> vertx .executeBlocking (() -> repository .dereference (ref , JsonSchema .of (ref , file ))));
120-
121- validationFutures .add (validationFuture );
122- }
123- return Future .all (validationFutures ).map (repository );
124- }).compose (repository ->
125- version . validateContract ( vertx , repository , unresolvedContract ). compose ( res -> {
126- try {
127- res . checkValidity ( );
128- return version . resolve ( vertx , repository , unresolvedContract );
129- } catch ( JsonSchemaValidationException | UnsupportedOperationException e ) {
130- return failedFuture ( createInvalidContract ( null , e ));
131- }
132- } )
133- . map ( resolvedSpec -> ( OpenAPIContract ) new OpenAPIContractImpl ( resolvedSpec , version , repository ))
134- ). recover ( e -> {
135- //Convert any non-openapi exceptions into an OpenAPIContractException
136- if ( e instanceof OpenAPIContractException ) {
137- return failedFuture ( e );
138- }
139-
140- return failedFuture ( createInvalidContract ("Found issue in specification for reference: " + e .getMessage (), e ));
141- }).onComplete (promise );
111+ .compose (repository -> {
112+ List <Future <?>> validationFutures = new ArrayList <>(additionalContractFiles .size ());
113+ for (String ref : additionalContractFiles .keySet ()) {
114+ // Todo: As soon a more modern Java version is used the validate part could be extracted in a private static
115+ // method and reused below.
116+ JsonObject file = additionalContractFiles .get (ref );
117+ Future <?> validationFuture = version .validateAdditionalContractFile (vertx , repository , file )
118+ .compose (v -> vertx .executeBlocking (() -> repository .dereference (ref , JsonSchema .of (ref , file ))));
119+
120+ validationFutures .add (validationFuture );
121+ }
122+ return Future .all (validationFutures ).map (repository );
123+ }).compose (repository -> version . validateContract ( vertx , repository , unresolvedContract ). compose ( res -> {
124+ try {
125+ res . checkValidity ();
126+ return version . resolve ( vertx , repository , unresolvedContract );
127+ } catch ( JsonSchemaValidationException | UnsupportedOperationException e ) {
128+ return failedFuture ( createInvalidContract ( null , e ));
129+ }
130+ })
131+ . map ( resolvedSpec -> ( OpenAPIContract ) new OpenAPIContractImpl ( resolvedSpec , version , repository )) )
132+ . recover ( e -> {
133+ // Convert any non-openapi exceptions into an OpenAPIContractException
134+ if ( e instanceof OpenAPIContractException ) {
135+ return failedFuture ( e );
136+ }
137+
138+ return failedFuture (
139+ createInvalidContract ("Found issue in specification for reference: " + e .getMessage (), e ));
140+ }).onComplete (promise );
142141
143142 return promise .future ();
144143 }
0 commit comments