Skip to content
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

Fixed issue with PATCH incorrectly replacing entire complex attr #726

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.function.Predicate;
import org.apache.directory.scim.spec.schema.Schema.Attribute.Type;

import static java.util.stream.Collectors.toList;

Expand Down Expand Up @@ -337,8 +338,16 @@ public void applySingleValue(Map<String, Object> sourceAsMap, Attribute attribut
checkMutability(attribute.getAttribute(subAttributeName), parentValue.get(subAttributeName));
parentValue.put(subAttributeName, value);
} else {
checkMutability(attribute, sourceAsMap.get(attribute.getName()));
sourceAsMap.put(attribute.getName(), value);
Object currentValue = sourceAsMap.get(attribute.getName());
checkMutability(attribute, currentValue);
// PATCH on complex attributes should only replace the values given, leaving any not provided in the request untouched
if (attribute.getType() == Type.COMPLEX) {
Map<String, Object> newValue = ((Map<String, Object>) currentValue);
newValue.putAll((Map<String, Object>) value);
sourceAsMap.put(attribute.getName(), newValue);
} else {
sourceAsMap.put(attribute.getName(), value);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,46 @@ public PatchHandlerTest() {
this.patchHandler = new DefaultPatchHandler(schemaRegistry);
}

@Test
public void applyReplaceComplexAttribute() {
// setup existing user
Name existingName = new Name();
existingName.setFamilyName("Family Name");
existingName.setGivenName("Given Name");
ScimUser user = user();
user.setName(existingName);

// setup patch ops
Map<String, String> newName = Map.of("familyName", "New Family Name");
PatchOperation op = patchOperation(REPLACE, "name", newName);

//execute
ScimUser updatedUser = patchHandler.apply(user, List.of(op));
assertThat(updatedUser.getName().getFamilyName()).isEqualTo("New Family Name");
// assert that PATCH did not update fields not provided in PatchOperation
assertThat(updatedUser.getName().getGivenName()).isNotNull();
jasonfagerberg-toast marked this conversation as resolved.
Show resolved Hide resolved
}

@Test
public void applyReplaceNestedComplexAttribute() {
// setup existing user
Name existingName = new Name();
existingName.setFamilyName("Family Name");
existingName.setGivenName("Given Name");
ScimUser user = user();
user.setName(existingName);

// setup patch ops
Map<String, Map<String, String>> newName = Map.of("name", Map.of("familyName", "New Family Name"));
PatchOperation op = patchOperation(REPLACE, null, newName);

//execute
ScimUser updatedUser = patchHandler.apply(user, List.of(op));
assertThat(updatedUser.getName().getFamilyName()).isEqualTo("New Family Name");
// assert that PATCH did not update fields not provided in PatchOperation
assertThat(updatedUser.getName().getGivenName()).isNotNull();
jasonfagerberg-toast marked this conversation as resolved.
Show resolved Hide resolved
}

@Test
public void applyReplaceUserName() {
String newUserName = "[email protected]";
Expand Down
Loading