Skip to content

Commit

Permalink
Fix JsonBlob unique name validation (#437)
Browse files Browse the repository at this point in the history
  • Loading branch information
ghsolomon authored Jan 16, 2025
1 parent 211df12 commit 3411036
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

### 💥 Breaking Changes (upgrade difficulty: 🟢 LOW - requires Java 17 for Hazelcast)

### 🐞 Bug Fixes

* Fixed `JsonBlob` unique name validation which also affected `Views`.

### ⚙️ Technical
* Minor Grails upgrades
* Hazelcast upgrade 5.3.7 -> 5.5.0
Expand Down
27 changes: 26 additions & 1 deletion grails-app/domain/io/xh/hoist/jsonblob/JsonBlob.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ class JsonBlob implements JSONFormat {
type maxSize: 50, blank: false
owner maxSize: 50, nullable: true, blank: false
acl nullable: true
name maxSize: 255, blank: false, unique: ['owner', 'type', 'archivedDate']
name maxSize: 255, blank: false, validator: { val, obj ->
isNameUnique(val, obj) ?: 'default.not.unique.message'
}
value validator: {Utils.isJSON(it) ?: 'default.invalid.json.message'}
meta nullable: true, validator: {Utils.isJSON(it) ?: 'default.invalid.json.message'}
description nullable: true
Expand Down Expand Up @@ -90,4 +92,27 @@ class JsonBlob implements JSONFormat {

return ret
}

//-------------------
// Implementation
//-------------------
private static boolean isNameUnique(String blobName, JsonBlob blob) {
// Unique constraint validation across nullable fields can be unreliable.
// See https://github.com/grails/grails-data-mapping/issues/1585
!(blob.owner != null ?
where {
name == blobName &&
type == blob.type &&
archivedDate == blob.archivedDate &&
owner == blob.owner &&
token != blob.token
} :
where {
name == blobName &&
type == blob.type &&
archivedDate == blob.archivedDate &&
owner == null &&
token != blob.token
})
}
}

0 comments on commit 3411036

Please sign in to comment.