Skip to content

Commit

Permalink
Merge pull request #1782 from eclipse-ditto/bugfix/fix-metadata-field…
Browse files Browse the repository at this point in the history
…selector-restrictiveness

fix that ThingFieldSelector did not allow specific paths of "_metadata"
  • Loading branch information
alstanchev authored Nov 1, 2023
2 parents 804b35a + 2f52170 commit e6f1636
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public final class ThingFieldSelector implements JsonFieldSelector {
.withoutUrlDecoding()
.build();
static final List<String> SELECTABLE_FIELDS = Arrays.asList("thingId", "policyId", "definition",
"_namespace", "_revision", "_created", "_modified", "_metadata", "_policy", "_context", "_context(/[^,]+)?",
"features(/[^,]+)?", "attributes(/[^,]+)?");
"_namespace", "_revision", "_created", "_modified", "_metadata(/[^,]+)?", "_policy", "_context",
"_context(/[^,]+)?", "features(/[^,]+)?", "attributes(/[^,]+)?");
private static final String KNOWN_FIELDS_REGEX = "/?(" + String.join("|", SELECTABLE_FIELDS) + ")";
private static final String FIELD_SELECTION_REGEX = "^" + KNOWN_FIELDS_REGEX + "(," + KNOWN_FIELDS_REGEX + ")*$";
private static final Pattern FIELD_SELECTION_PATTERN = Pattern.compile(FIELD_SELECTION_REGEX);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,12 @@ public static JsonFieldSelector expandFeatureIdWildcards(final Collection<JsonKe
*/
public static Stream<JsonPointer> expandFeatureIdWildcard(final Collection<JsonKey> featureIds,
final JsonPointer jsonPointer) {
if (hasFeatureIdWildcard(jsonPointer)) {
if (hasMetadataFeatureIdWildcard(jsonPointer)) {
return featureIds.stream().map(fid -> Thing.JsonFields.METADATA.getPointer()
.append(Thing.JsonFields.FEATURES.getPointer())
.append(JsonPointer.of(fid))
.append(jsonPointer.getSubPointer(3).orElse(JsonPointer.empty())));
} else if (hasFeatureIdWildcard(jsonPointer)) {
return featureIds.stream().map(fid -> Thing.JsonFields.FEATURES.getPointer()
.append(JsonPointer.of(fid))
.append(jsonPointer.getSubPointer(2).orElse(JsonPointer.empty())));
Expand All @@ -806,11 +811,33 @@ public static Stream<JsonPointer> expandFeatureIdWildcard(final Collection<JsonK
}

private static boolean hasFeatureIdWildcard(final JsonPointer pointer) {
return pointer.getLevelCount() > 1
&& pointer.getRoot()
return pointer.getLevelCount() > 1 &&
isFeaturePath(pointer) && pointer.get(1).filter(FEATURE_ID_WILDCARD::equals).isPresent();
}

private static boolean isFeaturePath(final JsonPointer pointer) {
return pointer.getRoot()
.filter(root -> Thing.JsonFields.FEATURES.getPointer().equals(JsonPointer.of(root)))
.isPresent();
}

private static boolean hasMetadataFeatureIdWildcard(final JsonPointer pointer) {
return pointer.getLevelCount() > 2 &&
isMetadataFeaturePath(pointer) &&
pointer.get(2).filter(FEATURE_ID_WILDCARD::equals).isPresent();
}

private static boolean isMetadataFeaturePath(final JsonPointer pointer) {
return pointer.getRoot()
.filter(root -> Thing.JsonFields.METADATA.getPointer().equals(JsonPointer.of(root)))
.isPresent()
&& pointer.get(1).filter(FEATURE_ID_WILDCARD::equals).isPresent();
&&
pointer.getSubPointer(1)
.map(metadataRoot -> metadataRoot.getRoot()
.filter(subRoot -> Thing.JsonFields.FEATURES.getPointer()
.equals(JsonPointer.of(subRoot)))
.isPresent()
).orElse(false);
}

}

0 comments on commit e6f1636

Please sign in to comment.