BASIL variables
A naming convention that turns SPARQL variables into Web API parameters. Parameters, and their types, are declared in the query itself.
Version: BASIL 1.0.0. Source of truth: VariableParser.
Overview
When an API is invoked, BASIL looks at the variables of the stored query. Any variable whose name starts with an underscore is a parameter. The value received in the HTTP request is converted into an RDF term and substituted for the variable, then the query is executed.
Query
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT ?city WHERE {
?city a dbo:City ;
dbo:country ?_country_iri .
} LIMIT ?_limit_number
Request
GET /basil/{id}/api?country=http://dbpedia.org/resource/Italy&limit=10
Query executed
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT ?city WHERE {
?city a dbo:City ;
dbo:country <http://dbpedia.org/resource/Italy> .
} LIMIT 10
Syntax
The variable name has up to three parts, separated by underscores:
?_name[_type[_localName]] # mandatory parameter
?__name[_type[_localName]] # optional parameter
?and$are equivalent, as in SPARQL.- One leading underscore: the parameter is mandatory. Two: it is optional.
- name is the name of the API parameter. It cannot contain underscores.
- type (optional) is one of the keywords
iri,literal,number, a two-letter language tag, an XSD datatype local name, or a prefix declared in the query. - localName (only after a prefix) is the local name of the datatype. It may contain underscores.
Summary table
| Variable | Parameter | Value v is substituted as |
|---|---|---|
?_name | name, mandatory | "v" |
?__name | name, optional | "v", or left unbound |
?_name_literal | name | "v" |
?_name_iri | name | <v> |
?_name_number | name | v as xsd:integer, or xsd:double if it contains a dot |
?_name_en | name | "v"@en (any two-letter tag) |
?_name_date | name | "v"^^xsd:date (any XSD datatype) |
?_name_ex_myType | name | "v"^^ex:myType (prefix ex declared in the query) |
SERVICE ?_name_iri | name | <v>, only if enabled (see below) |
Parameter types
Plain literal — ?_name, ?_name_literal
The default. The value becomes a plain string literal, with quotes and special characters escaped.
SELECT ?s WHERE { ?s rdfs:label ?_label }
# ?label=Rome → ?s rdfs:label "Rome"
Optional — ?__name
Two leading underscores make the parameter optional. If the request does not provide it, the variable remains a normal, unbound SPARQL variable. Write the query so that it is meaningful in both cases, for example with OPTIONAL or FILTER(!BOUND(...)). The type suffixes below apply in the same way: ?__type_iri, ?__min_number. Avoid optional parameters in positions where SPARQL does not allow a variable, such as LIMIT.
SELECT ?book WHERE {
?book a schema:Book ; schema:author ?__author_iri .
}
# no ?author → any author
# ?author=http://… → books by that author
IRI — ?_name_iri
The value is used as an IRI. Characters not allowed in a SPARQL IRI (spaces, < > " { } | ^ ` \) cause the request to fail with HTTP 400.
SELECT ?p ?o WHERE { ?_resource_iri ?p ?o }
# ?resource=http://dbpedia.org/resource/Rome → <http://dbpedia.org/resource/Rome> ?p ?o
Number — ?_name_number
The value is a number: xsd:integer, or xsd:double if it contains a dot. Useful for LIMIT, OFFSET and numeric filters. A value that is not a number gives HTTP 400.
SELECT ?s WHERE { ?s ex:population ?p FILTER(?p > ?_min_number) } LIMIT ?_limit_number
Language tag — ?_name_xx
Any two-letter suffix is read as a language tag. The value becomes a literal in that language.
SELECT ?s WHERE { ?s rdfs:label ?_label_it }
# ?label=Roma → "Roma"@it
XSD datatype — ?_name_type
A suffix that is the local name of an XSD datatype known to Apache Jena (for example integer, decimal, float, boolean, date, dateTime, duration) gives a literal with that datatype.
SELECT ?e WHERE { ?e ex:date ?d FILTER(?d >= ?_from_date) }
# ?from=2024-01-01 → "2024-01-01"^^xsd:date
Prefixed datatype — ?_name_prefix_localName
When the type is followed by a further part, the type is read as a prefix and the rest as the local name of the datatype. The prefix must be declared in the query; xsd is always available, also when not declared. The local name may contain underscores.
PREFIX ex: <http://example.org/ns#>
SELECT ?invoice WHERE { ?invoice ex:due ?_amount_ex_due_date }
# ?amount=2024-05-31 → "2024-05-31"^^<http://example.org/ns#due_date>
SERVICE endpoint — SERVICE ?_name_iri { … }
A parameter can be used as the IRI of a SERVICE clause, so that the caller chooses which endpoint is queried. This is disabled by default and must be enabled when starting the server:
java -Dbasil.sparql.serviceParameters=true -jar basil-server-1.0.0.jar …
When enabled, the value is always substituted as an IRI.
Do not enable this on a public server unless you intend to let any caller send queries, through your server, to any endpoint.
Rules and edge cases
- The underscore is reserved. It separates the parts of the name, so a parameter name cannot contain it.
?_first_nameis the parameterfirstwith an unrecognised suffixname(substituted as plain literal);?_first_name_enis read as the parameterfirsttypedname:en, which is rejected unless a prefixnameis declared. Use?_firstNameinstead. - Keywords are case-insensitive:
_IRI,_Numberare accepted. - Two-letter suffixes are language tags, also when they could be a prefix.
?_x_exis the literal"v"@ex; a prefixed datatype always has three parts. - Unknown suffixes fall back to plain literal.
- The same variable can appear many times in the query; all occurrences are substituted.
- One variable per parameter. Do not use two variables with the same parameter name and different types (e.g.
?_qand?_q_en) in one query: only one of them is bound. limitandoffsetare reserved request parameters. For SELECT, CONSTRUCT, DESCRIBE and ASK queries,?limit=nand?offset=nin the request set theLIMITandOFFSETof the query, also when the query does not declare them. If you need your own paging parameters, use other names (e.g.?_size_number).- Update queries (SPARQL 1.1 Update) follow the same convention.
- Parameters can be passed as query string (
GET) or as form parameters (POST). If a parameter is repeated, the first value is used.
Errors
| Situation | Result |
|---|---|
Creating or updating an API with ?_name_prefix_localName whose prefix is not declared | HTTP 400; the API is not saved |
| Invoking an API without a mandatory parameter | HTTP 400, listing the missing parameters |
Invalid IRI for an _iri parameter, or invalid number for _number | HTTP 400 |
Use GET /basil/{id}/explain to see the query after the substitution of the parameters.
Security
Substitution is safe against injection. Each value is serialised as an RDF term (escaped literal, validated IRI, or number) before the query is parsed, so a value cannot change the structure of the query. Parameters are never pasted into the query text as they are.
Changes
- 1.0.0 — prefixes in
?_name_prefix_localNameare resolved against the prefixes declared in the query; an undeclared prefix is an error (HTTP 400). APIs stored with earlier versions are still loaded, and such parameters are substituted as plain literals. Any XSD datatype known to Jena is accepted. Datatype local names may contain underscores.SERVICEparameters are disabled by default. Invalid parameter values return HTTP 400 instead of 500. - 0.x — original convention, described in the wiki.