Skip to content

Commit

Permalink
fix: parsing multi-valued param in explicit mode
Browse files Browse the repository at this point in the history
  • Loading branch information
martinmaillard committed Feb 8, 2022
1 parent 4a7302d commit f71fcd4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
19 changes: 12 additions & 7 deletions lib/middleware/iriTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,22 @@ function createTermFromVariable ({ template, value }) {
return value
}

const matches = value.match(literalValueRegex)
if (matches) {
let datatypeOrLanguage = matches.groups.language
if (matches.groups.datatype) {
datatypeOrLanguage = rdf.namedNode(matches.groups.datatype)
const parseValue = value => {
const matches = value.match(literalValueRegex)
if (matches) {
let datatypeOrLanguage = matches.groups.language
if (matches.groups.datatype) {
datatypeOrLanguage = rdf.namedNode(matches.groups.datatype)
}

return rdf.literal(matches.groups.value, datatypeOrLanguage)
}

return rdf.literal(matches.groups.value, datatypeOrLanguage)
return rdf.namedNode(value)
}

return rdf.namedNode(value)
const values = Array.isArray(value) ? value : [value]
return values.map(parseValue)
}

function middleware ({ dataset, term, graph }) {
Expand Down
26 changes: 26 additions & 0 deletions test/iriTemplate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,5 +366,31 @@ describe('middleware/iriTemplate', () => {
const boundTerm = clownface({ dataset }).out().term
deepStrictEqual(boundTerm, rdf.literal('A simple string', 'en'))
})

it('should return individual object for comma-separated query values', async () => {
let dataset = null
const app = express()
const tagProp = rdf.namedNode('http://example.org/tag')

app.use(middleware(iriTemplateMappingBuilder({
template: '/{?tag}',
variables: {
tag: 'http://example.org/tag'
},
explicitRepresentation: true
})))

app.use(async (req, res, next) => {
dataset = await req.dataset()

next()
})

await request(app).get('/?tag=http%3A%2F%2Fexample.org%2Fdimension%2Fcolors,http%3A%2F%2Fexample.org%2Fdimension%2Fcountries')

strictEqual(dataset.size, 2)
strictEqual(dataset.match(null, tagProp, rdf.namedNode('http://example.org/dimension/colors')).size, 1)
strictEqual(dataset.match(null, tagProp, rdf.namedNode('http://example.org/dimension/countries')).size, 1)
})
})
})

0 comments on commit f71fcd4

Please sign in to comment.