Skip to content

Commit

Permalink
Make request lists optional fields
Browse files Browse the repository at this point in the history
  • Loading branch information
courtneyeh committed Nov 16, 2023
1 parent c37db08 commit 048e09a
Showing 1 changed file with 13 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ public class PostStateValidators extends RestApiEndpoint {
DeserializableTypeDefinition.object(RequestBody.class)
.name("PostStateValidatorsRequestBody")
.initializer(RequestBody::new)
.withField(
.withOptionalField(
"ids",
DeserializableTypeDefinition.listOf(STRING_TYPE),
RequestBody::getIds,
RequestBody::setIds)
.withField(
.withOptionalField(
"statuses",
DeserializableTypeDefinition.listOf(STRING_TYPE),
RequestBody::getStringStatuses,
Expand Down Expand Up @@ -81,7 +81,7 @@ public PostStateValidators(final DataProvider dataProvider) {
public void handleRequest(RestApiRequest request) throws JsonProcessingException {
final RequestBody requestBody = request.getRequestBody();

final List<String> validators = requestBody.getIds();
final List<String> validators = requestBody.getIds().orElse(List.of());
final List<StatusParameter> statusParameters = requestBody.getStatuses();

final Set<ValidatorStatus> statusFilter = getApplicableValidatorStatuses(statusParameters);
Expand Down Expand Up @@ -109,24 +109,26 @@ public RequestBody(final List<String> ids, final List<StatusParameter> statuses)
this.statuses = statuses;
}

public List<String> getIds() {
return ids;
public Optional<List<String>> getIds() {
return ids.isEmpty() ? Optional.empty() : Optional.of(ids);
}

public void setIds(final List<String> ids) {
this.ids = ids;
public void setIds(final Optional<List<String>> ids) {
ids.ifPresent(i -> this.ids = i);
}

public List<StatusParameter> getStatuses() {
return statuses;
}

public List<String> getStringStatuses() {
return statuses.stream().map(Enum::name).collect(Collectors.toList());
public Optional<List<String>> getStringStatuses() {
return statuses.isEmpty()
? Optional.empty()
: Optional.of(statuses.stream().map(Enum::name).collect(Collectors.toList()));
}

public void setStatuses(final List<String> statuses) {
this.statuses = statuses.stream().map(StatusParameter::valueOf).toList();
public void setStatuses(final Optional<List<String>> statuses) {
statuses.ifPresent(s -> this.statuses = s.stream().map(StatusParameter::valueOf).toList());
}
}
}

0 comments on commit 048e09a

Please sign in to comment.