Skip to content

Commit

Permalink
fix time:now placeholder truncation
Browse files Browse the repository at this point in the history
* an exception was thrown regarding special regex char "[" used in "split"
* also fixed that fallback DittoJsonException for unknown exceptions could not be deserialized
  • Loading branch information
thjaeckle committed Feb 14, 2024
1 parent 0d04168 commit 792836f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;

import org.eclipse.ditto.base.model.exceptions.DittoJsonException;
import org.eclipse.ditto.base.model.exceptions.DittoRuntimeException;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.json.JsonParsableException;
import org.eclipse.ditto.json.JsonFieldSelectorInvalidException;
import org.eclipse.ditto.json.JsonKeyInvalidException;
import org.eclipse.ditto.json.JsonMissingFieldException;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonParseException;
import org.eclipse.ditto.json.JsonPointerInvalidException;
import org.eclipse.ditto.base.model.exceptions.DittoJsonException;
import org.eclipse.ditto.base.model.exceptions.DittoRuntimeException;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.json.JsonParsableException;
import org.eclipse.ditto.json.JsonRuntimeException;

/**
* Contains all strategies to deserialize subclasses of {@link org.eclipse.ditto.base.model.exceptions.DittoRuntimeException} from a combination of
Expand Down Expand Up @@ -103,6 +104,13 @@ private static final class DittoJsonExceptionRegistry {
private final Map<String, JsonParsable<DittoRuntimeException>> dittoJsonParseRegistries = new HashMap<>();

private DittoJsonExceptionRegistry() {
dittoJsonParseRegistries.put(DittoJsonException.FALLBACK_ERROR_CODE,
(jsonObject, dittoHeaders) -> new DittoJsonException(
JsonRuntimeException.newBuilder(DittoJsonException.FALLBACK_ERROR_CODE)
.message(getMessage(jsonObject))
.description(getDescription(jsonObject))
.build(), dittoHeaders));

dittoJsonParseRegistries.put(JsonParseException.ERROR_CODE,
(jsonObject, dittoHeaders) -> new DittoJsonException(
JsonParseException.newBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,9 @@ private boolean isValidTimeRange(final String timeRangeSuffix) {
final String durationWithTruncate = timeRangeSuffix.substring(1);
final String durationString;
if (durationWithTruncate.contains(TRUNCATE_START) && durationWithTruncate.contains(TRUNCATE_END)) {
final String[] durationStringAndTruncateString = durationWithTruncate.split(TRUNCATE_START, 2);
durationString = durationStringAndTruncateString[0];
final String truncateString = durationStringAndTruncateString[1];
if (!isValidTruncateStatement(truncateString.substring(0, truncateString.lastIndexOf(TRUNCATE_END)))) {
durationString = durationWithTruncate
.substring(0, durationWithTruncate.indexOf(TRUNCATE_START));
if (!isValidTruncation(durationWithTruncate)) {
return false;
}
} else {
Expand All @@ -133,11 +132,19 @@ private boolean isValidTimeRange(final String timeRangeSuffix) {
} catch (final Exception e) {
return false;
}
} else if (timeRangeSuffix.contains(TRUNCATE_START) && timeRangeSuffix.contains(TRUNCATE_END)) {
return isValidTruncation(timeRangeSuffix);
} else {
return false;
}
}

private boolean isValidTruncation(final String durationWithTruncate) {
final String truncateString = durationWithTruncate
.substring(durationWithTruncate.indexOf(TRUNCATE_START) + 1);
return isValidTruncateStatement(truncateString.substring(0, truncateString.lastIndexOf(TRUNCATE_END)));
}

private boolean isValidTruncateStatement(final String truncateString) {
return DittoDuration.DittoTimeUnit.forSuffix(truncateString).isPresent();
}
Expand Down

0 comments on commit 792836f

Please sign in to comment.