kopflos icon indicating copy to clipboard operation
kopflos copied to clipboard

Hydra box does not use actual decoded query string params

Open tpluscode opened this issue 3 years ago • 0 comments

I just stumbled upon a small problem with spaced encoded in query string parameters of IRI Template routes

Spaces in query can be encoded as %20 or +. In the latter case, it appears that they are not decoded by hydra-box but the plus sign is preserved in the value.

Here is the diff that solved my problem. At first I thought that the problem was in using req.params and not req.query but I assume that it could not, since a template variable can also be a segment? (is that actually supported, I don't remember). Thus, for now I added a replacer so that + does become a space but only when the given key is in fact a query string param.

Let me know if you think that the improvement should actually be in using req.query...

diff --git a/node_modules/hydra-box/lib/middleware/iriTemplate.js b/node_modules/hydra-box/lib/middleware/iriTemplate.js
index b725da2..100702b 100644
--- a/node_modules/hydra-box/lib/middleware/iriTemplate.js
+++ b/node_modules/hydra-box/lib/middleware/iriTemplate.js
@@ -65,13 +65,17 @@ function middleware ({ dataset, term, graph }) {
     const templateParams = clownface({ dataset: rdf.dataset() }).blankNode()
 
     Object.entries(req.params).forEach(([key, value]) => {
+      const isQueryParam = key in req.query
       const property = variablePropertyMap.get(key)
 
       if (!property) {
         return
       }
 
-      templateParams.addOut(property, createTermFromVariable({ template: iriTemplateNode, value }))
+      templateParams.addOut(property, createTermFromVariable({
+        template: iriTemplateNode,
+        value: isQueryParam ? value.replace(/\+/g, ' ') : value
+      }))
     })
 
     req.dataset = () => {

This issue body was partially generated by patch-package.

tpluscode avatar Nov 16 '22 11:11 tpluscode