Skip to content

Commit

Permalink
Add config option to disable closed world assumption (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefandesu committed Feb 21, 2022
1 parent 8b75fda commit 123dc9d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
1 change: 1 addition & 0 deletions config/config.default.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"baseUrl": null,
"title": "JSKOS Server",
"version": null,
"closedWorldAssumption": true,
"port": 3000,
"proxies": [],
"mongo": {
Expand Down
4 changes: 4 additions & 0 deletions config/config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
}
]
},
"closedWorldAssumption": {
"description": "If false, empty JSKOS closed world statements (see specs) will be removed from returned entities",
"type": "boolean"
},
"namespace": {
"description": "A namespace string that is used for generating unique URIs.",
"type": "string"
Expand Down
24 changes: 17 additions & 7 deletions utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,28 @@ const wrappers = {

}

// Recursively remove all fields starting with _ from response
// Gets called in `returnJSON` and `handleDownload`. Shouldn't be used anywhere else.
const cleanJSON = (json) => {
/**
* Recursively remove certain fields from response
*
* Gets called in `returnJSON` and `handleDownload`. Shouldn't be used anywhere else.
*
* @param {(Object|Object[])} json JSON object or array of objects
* @param {number} [depth=0] Should not be set when called from outside
*/
const cleanJSON = (json, depth = 0) => {
if (_.isArray(json)) {
json.forEach(cleanJSON)
json.forEach(value => cleanJSON(value, depth))
} else if (_.isObject(json)) {
_.forOwn(json, (value, key) => {
if (key.startsWith("_")) {
// remove from object
if (
// Remove top level empty arrays/objects if closedWorldAssumption is set to false
(depth === 0 && !config.closedWorldAssumption && (_.isEqual(value, {}) || _.isEqual(value, [])) )

This comment has been minimized.

Copy link
@nichtich

nichtich Feb 22, 2022

Member

The removal should not be limited to the top level but applied recursively.

This comment has been minimized.

Copy link
@stefandesu

stefandesu Feb 22, 2022

Author Member

Are you sure this will not have further consequences? For example, mappings that have an empty array to.memberSet will suddenly not be valid JSKOS anymore because memberSet is removed. There are probably other consequences as well.

This comment has been minimized.

Copy link
@nichtich

nichtich Feb 22, 2022

Member

memberSet is optional but we probably depend on it, so better only clear the first level, as implemented, until more is needed.

// Remove all fields started with _
|| key.startsWith("_")
) {
_.unset(json, key)
} else {
cleanJSON(value)
cleanJSON(value, depth + 1)
}
})
}
Expand Down

0 comments on commit 123dc9d

Please sign in to comment.