Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encode URI templates according to spec #41

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/MetadataBuilder.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const fs = require('fs')
const encodeURIComponent = require('strict-uri-encode')
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not exactly related to the issue but I thought it useful to actually produce correct templates. While I did not observe any problems, according to the URI Templates spec, template variables should also be encoded


class MetadataBuilder {
static readFirstLine (filename) {
Expand Down
26 changes: 13 additions & 13 deletions lib/metadata/TableSchema.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const difference = require('lodash/difference')
const namespace = require('../namespace')
const parseDateTime = require('../parseDateTime')
const uriTemplate = require('uri-templates')
const uriTemplate = require('url-template')
const URL = require('url')
const RdfUtils = require('./RdfUtils')

Expand Down Expand Up @@ -37,10 +37,10 @@ class TableSchema {
return
}

const aboutUrlTemplate = uriTemplate(aboutUrl)
const aboutUrlTemplate = uriTemplate.parse(aboutUrl)

return (row) => {
return this.factory.namedNode(URL.resolve(this.baseIRI, aboutUrlTemplate.fill(row))) // eslint-disable-line node/no-deprecated-api
return this.factory.namedNode(URL.resolve(this.baseIRI, aboutUrlTemplate.expand(row))) // eslint-disable-line node/no-deprecated-api
}
}

Expand All @@ -51,7 +51,7 @@ class TableSchema {
return
}

return uriTemplate(url)
return uriTemplate.parse(url)
}

parseColumns () {
Expand All @@ -70,17 +70,17 @@ class TableSchema {
const valueUrl = RdfUtils.findValue(this.dataset, node, this.ns.valueUrl)

return {
aboutUrl: aboutUrl && uriTemplate(aboutUrl),
aboutUrl: aboutUrl && uriTemplate.parse(aboutUrl),
datatype: this.parseDatatype(node),
language: language && uriTemplate(language),
language: language && uriTemplate.parse(language),
name,
nullValue,
defaultValue,
propertyUrl: (propertyUrl && uriTemplate(propertyUrl)) || this.propertyUrl || this.defaultPropertyUrl(name),
propertyUrl: (propertyUrl && uriTemplate.parse(propertyUrl)) || this.propertyUrl || this.defaultPropertyUrl(name),
suppressOutput: suppressOutput === 'true',
titles,
virtual,
valueUrl: valueUrl && uriTemplate(valueUrl)
valueUrl: valueUrl && uriTemplate.parse(valueUrl)
}
})
}
Expand Down Expand Up @@ -136,7 +136,7 @@ class TableSchema {
return null
}

return this.factory.namedNode(URL.resolve(this.baseIRI, column.aboutUrl.fill(row))) // eslint-disable-line node/no-deprecated-api
return this.factory.namedNode(URL.resolve(this.baseIRI, column.aboutUrl.expand(row))) // eslint-disable-line node/no-deprecated-api
}

value (column, row) {
Expand All @@ -145,7 +145,7 @@ class TableSchema {
}

if (column.valueUrl) {
return this.factory.namedNode(column.valueUrl.fill(row))
return this.factory.namedNode(column.valueUrl.expand(row))
}

let value = column.titles.reduce((value, title) => {
Expand All @@ -169,12 +169,12 @@ class TableSchema {
}

if (column.datatype.base) {
return this.factory.literal(value, (column.language && column.language.fill(row).toLowerCase()) || column.datatype.base)
return this.factory.literal(value, (column.language && column.language.expand(row).toLowerCase()) || column.datatype.base)
}
}

property (column, row) {
return this.factory.namedNode(column.propertyUrl.fill(row))
return this.factory.namedNode(column.propertyUrl.expand(row))
}

createAllColumns (row) {
Expand All @@ -198,7 +198,7 @@ class TableSchema {

defaultPropertyUrl (name) {
return {
fill: () => {
expand: () => {
return this.baseIRI + '#' + encodeURI(name)
}
}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"lodash": "^4.17.15",
"luxon": "^1.17.3",
"readable-stream": "^3.4.0",
"uri-templates": "^0.2.0"
"strict-uri-encode": "^2.0.0",
"url-template": "^2"
},
"devDependencies": {
"@rdfjs/parser-jsonld": "^1.1.2",
Expand Down
2 changes: 2 additions & 0 deletions test/support/test031-strict-url-encode.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(foo)
(bar)
14 changes: 14 additions & 0 deletions test/support/test031-strict-url-encode.csv-metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"@context": "http://www.w3.org/ns/csvw",
"url": "test031-strict-url-encode.csv",
"tableSchema": {
"aboutUrl": "http://example.org/data.csv#{_row}",
"columns": [
{
"titles": "(foo)",
"propertyUrl": "http://example.org/vocab#{_name}",
"valueUrl": "http://example.org/object/{%28foo%29}"
}
]
}
}
10 changes: 10 additions & 0 deletions test/support/test031-strict-url-encode.nt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<http://example.org/data.csv#1> <http://example.org/vocab#%28foo%29> <http://example.org/object/%28bar%29> .
_:c14n0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/ns/csvw#Row> .
_:c14n0 <http://www.w3.org/ns/csvw#describes> <http://example.org/data.csv#1> .
_:c14n0 <http://www.w3.org/ns/csvw#rownum> "1"^^<http://www.w3.org/2001/XMLSchema#integer> .
_:c14n0 <http://www.w3.org/ns/csvw#url> <test031-strict-url-encode.csv#row=2> .
_:c14n1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/ns/csvw#Table> .
_:c14n1 <http://www.w3.org/ns/csvw#row> _:c14n0 .
_:c14n1 <http://www.w3.org/ns/csvw#url> <test031-strict-url-encode.csv> .
_:c14n2 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/ns/csvw#TableGroup> .
_:c14n2 <http://www.w3.org/ns/csvw#table> _:c14n1 .