-
Notifications
You must be signed in to change notification settings - Fork 12
Add builder for OpenAPIContract. #103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
b01e336
to
765af73
Compare
@@ -46,7 +43,7 @@ public interface OpenAPIContract { | |||
* @return A succeeded {@link Future} holding an {@link OpenAPIContract} instance, otherwise a failed {@link Future}. | |||
*/ | |||
static Future<OpenAPIContract> from(Vertx vertx, String unresolvedContractPath) { | |||
return readYamlOrJson(vertx, unresolvedContractPath).compose(json -> from(vertx, json)); | |||
return builder(vertx).setContract(unresolvedContractPath).build(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we explicitly mention, that we also support YAML files?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the naming of the methods? Or in the javadoc?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the javadoc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added it to the javadoc of the builder in 8fe103e
* @return The builder, for a fluent interface | ||
*/ | ||
public OpenAPIContractBuilder setContract(String contractPath) { | ||
this.contractFile = contractPath; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is it now, Path or File?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will use path at every place
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I replaced all occurences of file and replaced it with path
@@ -57,7 +54,11 @@ static Future<OpenAPIContract> from(Vertx vertx, String unresolvedContractPath) | |||
* @return A succeeded {@link Future} holding an {@link OpenAPIContract} instance, otherwise a failed {@link Future}. | |||
*/ | |||
static Future<OpenAPIContract> from(Vertx vertx, JsonObject unresolvedContract) { | |||
return from(vertx, unresolvedContract, emptyMap()); | |||
if (unresolvedContract == null) | |||
return Future.failedFuture(OpenAPIContractException.createInvalidContract("Spec must not be null")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I saw this check is now made in the Builder and also throws a BuilderException which makes more sense. We should not throw it here.
Of course this is kind of breaking, so we should mention it in the release notes. But it is allowed, we are still in Technical Preview and the impact is very low.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I left it here to not break existing tests. If it is no problem to remove it, I will remove it and the corresponding test(s).
var resolvedContracts = Future | ||
.succeededFuture(additionalContracts) | ||
.compose(x -> readContractFiles() | ||
.map(r -> { | ||
var all = new HashMap<>(x); | ||
all.putAll(r); | ||
return all; | ||
})); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
var resolvedContracts = Future | |
.succeededFuture(additionalContracts) | |
.compose(x -> readContractFiles() | |
.map(r -> { | |
var all = new HashMap<>(x); | |
all.putAll(r); | |
return all; | |
})); | |
var allAdditionalContracts = readContractFiles() | |
.map(resolvedFiles -> { | |
var all = new HashMap<>(additionalContracts); | |
all.putAll(resolvedFiles); | |
return all; | |
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point to reduce unnecessary code. Is corrected in 32fe992
}); | ||
} | ||
|
||
private Future<OpenAPIContract> buildOpenAPIContract(JsonObject unresolvedContract, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
private Future<OpenAPIContract> buildOpenAPIContract(JsonObject unresolvedContract, | |
private Future<OpenAPIContract> buildOpenAPIContract(JsonObject resolvedContract, |
Now it is resolved, isn't it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is corrected in 2dd5ae0
return failedFuture(createInvalidContract("Spec must not be null")); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this branch is not possible, it is also not tested. Please add a test case for this branch, or remove it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is removed in 494d5fe
} | ||
|
||
private Future<OpenAPIContract> buildOpenAPIContract(JsonObject unresolvedContract, | ||
Map<String, JsonObject> additionalContractFiles) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Map<String, JsonObject> additionalContractFiles) { | |
Map<String, JsonObject> additionalContracts) { |
This are no longer contract files, they are resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is corrected in 2dd5ae0
List<Future<?>> validationFutures = new ArrayList<>(additionalContractFiles.size()); | ||
for (String ref : additionalContractFiles.keySet()) { | ||
// Todo: As soon a more modern Java version is used the validate part could be extracted in a private static | ||
// method and reused below. | ||
JsonObject file = additionalContractFiles.get(ref); | ||
Future<?> validationFuture = version.validateAdditionalContractFile(vertx, repository, file) | ||
.compose(v -> vertx.executeBlocking(() -> repository.dereference(ref, JsonSchema.of(ref, file)))); | ||
|
||
validationFutures.add(validationFuture); | ||
} | ||
return Future.all(validationFutures).map(repository); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
List<Future<?>> validationFutures = new ArrayList<>(additionalContractFiles.size()); | |
for (String ref : additionalContractFiles.keySet()) { | |
// Todo: As soon a more modern Java version is used the validate part could be extracted in a private static | |
// method and reused below. | |
JsonObject file = additionalContractFiles.get(ref); | |
Future<?> validationFuture = version.validateAdditionalContractFile(vertx, repository, file) | |
.compose(v -> vertx.executeBlocking(() -> repository.dereference(ref, JsonSchema.of(ref, file)))); | |
validationFutures.add(validationFuture); | |
} | |
return Future.all(validationFutures).map(repository); | |
List<Future<?>> validationFutures = new ArrayList<>(additionalContractFiles.size()); | |
additionalContractFiles.forEach((ref, additionalContract) -> { | |
Future<?> validationFuture = version.validateAdditionalContractFile(vertx, repository, additionalContract) | |
.compose(v -> vertx.executeBlocking(() -> repository.dereference(ref, JsonSchema.of(ref, additionalContract)))); | |
validationFutures.add(validationFuture); | |
}); | |
return Future.all(validationFutures).map(repository); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Of course variables need to be renamed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
*/ | ||
@ExtendWith(VertxExtension.class) | ||
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) | ||
public class OpenAPIContractBuilderTest { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please have a look into class ResourceHelper
which is also used in OpenAPIContractTest
. There are some useful helper methods.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Refactored strings to constants in 710040a
Will allow to add new fields to the contract setup without requiring to change or to add new factory methods.
…tracts, additionalContractPaths
… now covered by the OpenApiContractBuilder
Variable is named file, but actually is a contract part
a8bd99a
to
0f55864
Compare
* @param additionalContractFiles The additional contract files | ||
* @param vertx The related Vert.x instance. | ||
* @param contract The unresolved contract. | ||
* @param additionalContractPartPaths The additional contract part paths |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* @param additionalContractPartPaths The additional contract part paths | |
* @param additionalContractParts The additional contract parts |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 01ba794
/** | ||
* Builder for OpenAPIContracts.<br> | ||
* | ||
* In the simplest case (you only have one contract) you must either provide a path to your openapi-contract in json |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought we mentioned it in the JavaDoc of the methods which allows to specify a contract path or additional contract part paths
} | ||
|
||
/** | ||
* Puts a contract that is referenced by the main contract. This method can be |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* Puts a contract that is referenced by the main contract. This method can be | |
* Puts an additional contract part that is referenced by the main contract. This method can be |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 01ba794
|
||
/** | ||
* Puts a contract that is referenced by the main contract. This method can be | ||
* called multiple times to add multiple referenced contracts. Overrides a previously |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* called multiple times to add multiple referenced contracts. Overrides a previously | |
* called multiple times to add multiple referenced additional contract parts. Overrides a previously |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 01ba794
/** | ||
* Puts a contract that is referenced by the main contract. This method can be | ||
* called multiple times to add multiple referenced contracts. Overrides a previously | ||
* added contract when the same key is used. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* added contract when the same key is used. | |
* added additional contract part when the same key is used. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 01ba794
* called multiple times to add multiple referenced contracts. | ||
* | ||
* @param key The unique key for the contract. | ||
* @param contractPart The contract object. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* @param contractPart The contract object. | |
* @param contractPart The additional contract part. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 01ba794
} | ||
|
||
/** | ||
* Uses the contracts from the provided map to resolve referenced contracts. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* Uses the contracts from the provided map to resolve referenced contracts. | |
* Uses the additional contract parts from the provided map to resolve referenced additional contract parts. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 01ba794
|
||
/** | ||
* Uses the contracts from the provided map to resolve referenced contracts. | ||
* Replaces all previously put contracts by {@link #putAdditionalContractPart(String, JsonObject)}. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* Replaces all previously put contracts by {@link #putAdditionalContractPart(String, JsonObject)}. | |
* Replaces all previously put additional contract parts by {@link #putAdditionalContractPart(String, JsonObject)}. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 01ba794
/** | ||
* Uses the contracts from the provided map to resolve referenced contracts. | ||
* Replaces all previously put contracts by {@link #putAdditionalContractPart(String, JsonObject)}. | ||
* If the same key is used also replaces the contracts set by {@link #putAdditionalContractPartPath(String, String)} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* If the same key is used also replaces the contracts set by {@link #putAdditionalContractPartPath(String, String)} | |
* If the same key is used also replaces the additional contract part paths set by {@link #putAdditionalContractPartPath(String, String)} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 01ba794
import io.vertx.json.schema.JsonSchemaValidationException; | ||
import io.vertx.json.schema.OutputUnit; | ||
import io.vertx.json.schema.SchemaRepository; | ||
import io.vertx.json.schema.*; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in c8858f8
e7495aa
to
e22e7af
Compare
e22e7af
to
710040a
Compare
Will allow to add new fields to the contract setup without requiring to change or to add new factory methods.
Motivation:
Split the pull request #101 into two simpler request: one for the builder part, one for the additional media types. This pull request covers the first part.
Conformance:
You should have signed the Eclipse Contributor Agreement: Done
Please also make sure you adhere to the code style guidelines: https://github.com/vert-x3/wiki/wiki/Vert.x-code-style-guidelines: May require some reformatting.