Skip to content

Commit 3293841

Browse files
committed
Sort @type when looking for scoped contexts.
1 parent 0bd491e commit 3293841

File tree

4 files changed

+44
-3
lines changed

4 files changed

+44
-3
lines changed

lib/json/ld/compact.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,10 @@ def compact(element, property: nil)
5656

5757
# Apply any context defined on an alias of @type
5858
# If key is @type and any compacted value is a term having a local context, overlay that context.
59-
Array(element['@type']).each do |expanded_type|
60-
term = context.compact_iri(expanded_type, vocab: true)
59+
Array(element['@type']).
60+
map {|expanded_type| context.compact_iri(expanded_type, vocab: true)}.
61+
sort.
62+
each do |term|
6163
term_context = self.context.term_definitions[term].context if context.term_definitions[term]
6264
self.context = context.parse(term_context) if term_context
6365
end

lib/json/ld/expand.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def expand(input, active_property, context, ordered: true, framing: false)
5454
# See if keys mapping to @type have terms with a local context
5555
input.each_pair do |key, val|
5656
next unless context.expand_iri(key, vocab: true) == '@type'
57-
Array(val).each do |term|
57+
Array(val).sort.each do |term|
5858
term_context = context.term_definitions[term].context if context.term_definitions[term]
5959
context = term_context ? context.parse(term_context) : context
6060
end

spec/compact_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1876,6 +1876,28 @@
18761876
"c": "C in example"
18771877
}),
18781878
},
1879+
"orders lexicographically" => {
1880+
input: %([{
1881+
"@type": ["http://example/t2", "http://example/t1"],
1882+
"http://example.org/foo": [
1883+
{"@id": "urn:bar"}
1884+
]
1885+
}]),
1886+
context: %({
1887+
"@vocab": "http://example/",
1888+
"t1": {"@context": {"foo": {"@id": "http://example.com/foo"}}},
1889+
"t2": {"@context": {"foo": {"@id": "http://example.org/foo", "@type": "@id"}}}
1890+
}),
1891+
output: %({
1892+
"@context": {
1893+
"@vocab": "http://example/",
1894+
"t1": {"@context": {"foo": {"@id": "http://example.com/foo"}}},
1895+
"t2": {"@context": {"foo": {"@id": "http://example.org/foo", "@type": "@id"}}}
1896+
},
1897+
"@type": ["t2", "t1"],
1898+
"foo": "urn:bar"
1899+
}),
1900+
},
18791901
"with @container: @type" => {
18801902
input: %([{
18811903
"http://example/typemap": [

spec/expand_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2083,6 +2083,23 @@
20832083
]
20842084
}])
20852085
},
2086+
"orders lexicographically" => {
2087+
input: %({
2088+
"@context": {
2089+
"@vocab": "http://example/",
2090+
"t1": {"@context": {"foo": {"@id": "http://example.com/foo"}}},
2091+
"t2": {"@context": {"foo": {"@id": "http://example.org/foo", "@type": "@id"}}}
2092+
},
2093+
"@type": ["t2", "t1"],
2094+
"foo": "urn:bar"
2095+
}),
2096+
output: %([{
2097+
"@type": ["http://example/t2", "http://example/t1"],
2098+
"http://example.org/foo": [
2099+
{"@id": "urn:bar"}
2100+
]
2101+
}])
2102+
},
20862103
"Raises InvalidTermDefinition if processingMode is not specified" => {
20872104
input: %({
20882105
"@context": {

0 commit comments

Comments
 (0)