Skip to content

Commit e48e772

Browse files
committed
fix: patch add initial member to group
1 parent bd369eb commit e48e772

File tree

3 files changed

+76
-2
lines changed

3 files changed

+76
-2
lines changed

core/src/main/scala/scim/model/PatchOp.scala

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,17 @@ object PatchOp {
2828
case None =>
2929
addIt(root)(on, context.value)
3030
case Some(ap@AttributePath(name, _, None)) =>
31-
addIt(ap.basePath(context.defaultSchema))(on, context.value)
32-
case Some(ap@AttributePath(name, _, Some(_))) =>
31+
val jsonPath = ap.basePath(context.defaultSchema)
32+
if (jsonPath.json.isEmpty(on)) {
33+
val v = if (context.value.isArray) context.value else Json.arr(context.value)
34+
on.asObject.map(_.add(name, v))
35+
.map(Json.fromJsonObject)
36+
.map(Right.apply)
37+
.getOrElse(Left("entity must be on object"))
38+
} else {
39+
addIt(ap.basePath(context.defaultSchema))(on, context.value)
40+
}
41+
case Some(ap@AttributePath(_, _, Some(_))) =>
3342
Left("adding a sub attribute is not supported")
3443
case Some(FilteredAttributePath(_, _, _, _)) =>
3544
Left("add with filter is not supported")

core/src/test/scala/scim/model/Jsons.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,4 +214,19 @@ object Jsons {
214214
| ]
215215
| }
216216
|""".stripMargin)
217+
val groupEmpty = parse(
218+
"""
219+
| {
220+
| "id": "6c5bb468-14b2-4183-baf2-06d523e03bd3",
221+
| "schemas": ["urn:ietf:params:scim:schemas:core:2.0:Group"],
222+
| "displayName": "Group C",
223+
| "meta": {
224+
| "resourceType": "Group",
225+
| "created": "2011-08-01T18:29:50.873Z",
226+
| "lastModified": "2011-08-01T18:29:50.873Z",
227+
| "location": "https://example.com/v2/Groups/6c5bb468-14b2-4183-baf2-06d523e03bd3",
228+
| "version": "W\/\"wGB85s2QJMjiNnuI\""
229+
| }
230+
| }
231+
|""".stripMargin)
217232
}

core/src/test/scala/scim/model/PatchOpSpec.scala

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,56 @@ class PatchOpSpec extends AnyFunSpec with Checkers with Matchers with OptionValu
8080
patch.applyTo(Schema.Group)(before) should be(Right(after))
8181
}
8282

83+
it("should add a first member to a group (element to Null->array)") {
84+
val patch = patchOp(
85+
"""
86+
| { "schemas":
87+
| ["urn:ietf:params:scim:api:messages:2.0:PatchOp"],
88+
| "Operations":[
89+
| {
90+
| "op":"add",
91+
| "path":"members",
92+
| "value":[
93+
| {
94+
| "display": "Babs Jensen",
95+
| "$ref":
96+
| "https://example.com/v2/Users/2819c223...413861904646",
97+
| "value": "2819c223-7f76-453a-919d-413861904646"
98+
| }
99+
| ]
100+
| }
101+
| ]
102+
| }
103+
|""".stripMargin)
104+
105+
val before = Jsons.groupEmpty
106+
107+
val after = parseJson(
108+
"""
109+
| {
110+
| "id": "6c5bb468-14b2-4183-baf2-06d523e03bd3",
111+
| "schemas": ["urn:ietf:params:scim:schemas:core:2.0:Group"],
112+
| "displayName": "Group C",
113+
| "meta": {
114+
| "resourceType": "Group",
115+
| "created": "2011-08-01T18:29:50.873Z",
116+
| "lastModified": "2011-08-01T18:29:50.873Z",
117+
| "location": "https://example.com/v2/Groups/6c5bb468-14b2-4183-baf2-06d523e03bd3",
118+
| "version": "W\/\"wGB85s2QJMjiNnuI\""
119+
| },
120+
| "members": [
121+
| {
122+
| "display": "Babs Jensen",
123+
| "$ref": "https://example.com/v2/Users/2819c223...413861904646",
124+
| "value": "2819c223-7f76-453a-919d-413861904646"
125+
| }
126+
| ]
127+
| }
128+
|""".stripMargin)
129+
130+
patch.applyTo(Schema.Group)(before) should be(Right(after))
131+
}
132+
83133
it("should add new attribute") {
84134
val patch = patchOp(
85135
"""

0 commit comments

Comments
 (0)