Skip to content

Commit f34430c

Browse files
committed
HLRC: Use Optional in validation logic (#33104)
The Validatable class comes from an old class in server code, that assumed null was returned in the event of validation having no errors. This commit changes that to use Optional, which is cleaner than passing around null objects.
1 parent 15824ad commit f34430c

File tree

2 files changed

+15
-18
lines changed

2 files changed

+15
-18
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/RestHighLevelClient.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@
174174
import java.util.List;
175175
import java.util.Map;
176176
import java.util.Objects;
177+
import java.util.Optional;
177178
import java.util.ServiceLoader;
178179
import java.util.Set;
179180
import java.util.function.Function;
@@ -1334,9 +1335,9 @@ protected final <Req extends Validatable, Resp> Resp performRequest(Req request,
13341335
RequestOptions options,
13351336
CheckedFunction<Response, Resp, IOException> responseConverter,
13361337
Set<Integer> ignores) throws IOException {
1337-
ValidationException validationException = request.validate();
1338-
if (validationException != null && validationException.validationErrors().isEmpty() == false) {
1339-
throw validationException;
1338+
Optional<ValidationException> validationException = request.validate();
1339+
if (validationException != null && validationException.isPresent()) {
1340+
throw validationException.get();
13401341
}
13411342
return internalPerformRequest(request, requestConverter, options, responseConverter, ignores);
13421343
}
@@ -1445,9 +1446,9 @@ protected final <Req extends Validatable, Resp> void performRequestAsync(Req req
14451446
RequestOptions options,
14461447
CheckedFunction<Response, Resp, IOException> responseConverter,
14471448
ActionListener<Resp> listener, Set<Integer> ignores) {
1448-
ValidationException validationException = request.validate();
1449-
if (validationException != null && validationException.validationErrors().isEmpty() == false) {
1450-
listener.onFailure(validationException);
1449+
Optional<ValidationException> validationException = request.validate();
1450+
if (validationException != null && validationException.isPresent()) {
1451+
listener.onFailure(validationException.get());
14511452
return;
14521453
}
14531454
internalPerformRequestAsync(request, requestConverter, options, responseConverter, listener, ignores);

client/rest-high-level/src/main/java/org/elasticsearch/client/Validatable.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,20 @@
1818
*/
1919
package org.elasticsearch.client;
2020

21+
import java.util.Optional;
22+
2123
/**
2224
* Defines a validation layer for Requests.
2325
*/
2426
public interface Validatable {
25-
ValidationException EMPTY_VALIDATION = new ValidationException() {
26-
@Override
27-
public void addValidationError(String error) {
28-
throw new UnsupportedOperationException("Validation messages should not be added to the empty validation");
29-
}
30-
};
31-
3227
/**
33-
* Perform validation. This method does not have to be overridden in the event that no validation needs to be done.
28+
* Perform validation. This method does not have to be overridden in the event that no validation needs to be done,
29+
* or the validation was done during object construction time. A {@link ValidationException} that is not null is
30+
* assumed to contain validation errors and will be thrown.
3431
*
35-
* @return potentially null, in the event of older actions, an empty {@link ValidationException} in newer actions, or finally a
36-
* {@link ValidationException} that contains a list of all failed validation.
32+
* @return An {@link Optional} {@link ValidationException} that contains a list of validation errors.
3733
*/
38-
default ValidationException validate() {
39-
return EMPTY_VALIDATION;
34+
default Optional<ValidationException> validate() {
35+
return Optional.empty();
4036
}
4137
}

0 commit comments

Comments
 (0)