Skip to content

Commit 9930e0a

Browse files
committed
Implement compactToRelative by controlling if compacting context uses the document base as _base IRI_.
1 parent edac299 commit 9930e0a

File tree

3 files changed

+43
-26
lines changed

3 files changed

+43
-26
lines changed

example-files/issue-507.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
require 'pp'
2+
require 'linkeddata'
3+
4+
context = JSON.parse %({
5+
"@context": {
6+
"id": "@id",
7+
"rdfs": {"@id": "http://.../"},
8+
"seeAlso": {"@id": "rdfs:seeAlso", "@container": "@set"}
9+
}
10+
})
11+
12+
input = JSON.parse %({
13+
"@context": {
14+
"id": "@id",
15+
"rdfs": {"@id": "http://.../"},
16+
"seeAlso": {"@id": "rdfs:seeAlso", "@container": "@set"}
17+
},
18+
"seeAlso": [
19+
{
20+
"id": "http://example.org/reference1"
21+
},
22+
"http://example.org/reference2",
23+
{"id": "http://example.org/reference3", "format": "text/html"}
24+
]
25+
})
26+
27+
pp JSON::LD::API.compact(input, context['@context'])

lib/json/ld/api.rb

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,12 @@ def initialize(input, context, options = {}, &block)
9090
@options = {
9191
compactArrays: true,
9292
rename_bnodes: true,
93-
documentLoader: self.class.method(:documentLoader),
94-
compactToRelative: true
93+
documentLoader: self.class.method(:documentLoader)
9594
}.merge(options)
9695
@namer = options[:unique_bnodes] ? BlankNodeUniqer.new : (@options[:rename_bnodes] ? BlankNodeNamer.new("b") : BlankNodeMapper.new)
9796

9897
# For context via Link header
99-
context_ref = nil
98+
remote_base, context_ref = nil, nil
10099

101100
@value = case input
102101
when Array, Hash then input.dup
@@ -116,8 +115,9 @@ def initialize(input, context, options = {}, &block)
116115
when String
117116
remote_doc = @options[:documentLoader].call(input, @options)
118117

119-
@options = {base: remote_doc.documentUrl}.merge(@options)
118+
remote_base = remote_doc.documentUrl
120119
context_ref = remote_doc.contextUrl
120+
@options = {base: remote_doc.documentUrl}.merge(@options) unless @options[:no_default_base]
121121

122122
case remote_doc.document
123123
when String
@@ -136,13 +136,6 @@ def initialize(input, context, options = {}, &block)
136136
@options[:processingMode] ||= @context.processingMode || "json-ld-1.0"
137137
@options[:validate] ||= %w(json-ld-1.0 json-ld-1.1).include?(@options[:processingMode])
138138

139-
# If, after processing, the context does not have a _base IRI_, and the _compactToRelative_ option is set to true or processingMode is json-ld-1.0, set _base IRI_ in the active context to either the _base_ option from the API, if set, or the IRI of the currently being processed document.
140-
if !@context.base && @options[:base]
141-
doc_base = RDF::URI(@options[:base]).dup
142-
doc_base.canonicalize! if options[:canonicalize]
143-
@context.base = doc_base
144-
end
145-
146139
if block_given?
147140
case block.arity
148141
when 0, -1 then instance_eval(&block)
@@ -222,16 +215,17 @@ def self.expand(input, options = {}, &block)
222215
# @see http://json-ld.org/spec/latest/json-ld-api/#compaction-algorithm
223216
def self.compact(input, context, options = {})
224217
result = nil
218+
options = {compactToRelative: true}.merge(options)
225219

226220
# 1) Perform the Expansion Algorithm on the JSON-LD input.
227221
# This removes any existing context to allow the given context to be cleanly applied.
228222
expanded_input = options[:expanded] ? input : API.expand(input, options) do |result, base_iri|
229-
options[:base] ||= base_iri
223+
options[:base] ||= base_iri if options[:compactToRelative]
230224
result
231225
end
232226

233227
#require 'byebug'; byebug
234-
API.new(expanded_input, context, options) do
228+
API.new(expanded_input, context, options.merge(no_default_base: true)) do
235229
log_debug(".compact") {"expanded input: #{expanded_input.to_json(JSON_STATE) rescue 'malformed json'}"}
236230
result = compact(value)
237231

@@ -267,15 +261,16 @@ def self.compact(input, context, options = {})
267261
# @see http://json-ld.org/spec/latest/json-ld-api/#framing-algorithm
268262
def self.flatten(input, context, options = {})
269263
flattened = []
264+
options = {compactToRelative: true}.merge(options)
270265

271266
# Expand input to simplify processing
272267
expanded_input = options[:expanded] ? input : API.expand(input, options) do |result, base_iri|
273-
options[:base] ||= base_iri
268+
options[:base] ||= base_iri if options[:compactToRelative]
274269
result
275270
end
276271

277272
# Initialize input using
278-
API.new(expanded_input, context, options) do
273+
API.new(expanded_input, context, options.merge(no_default_base: true)) do
279274
log_debug(".flatten") {"expanded input: #{value.to_json(JSON_STATE) rescue 'malformed json'}"}
280275

281276
# Initialize node map to a JSON object consisting of a single member whose key is @default and whose value is an empty JSON object.
@@ -340,6 +335,7 @@ def self.frame(input, frame, options = {})
340335
options = {
341336
base: (input if input.is_a?(String)),
342337
compactArrays: true,
338+
compactToRelative: true,
343339
embed: '@last',
344340
explicit: false,
345341
requireAll: true,
@@ -369,15 +365,15 @@ def self.frame(input, frame, options = {})
369365

370366
# Expand input to simplify processing
371367
expanded_input = options[:expanded] ? input : API.expand(input, options) do |result, base_iri|
372-
options[:base] ||= base_iri
368+
options[:base] ||= base_iri if options[:compactToRelative]
373369
result
374370
end
375371

376372
# Expand frame to simplify processing
377373
expanded_frame = API.expand(frame, options.merge(processingMode: "json-ld-1.1-expand-frame"))
378374

379375
# Initialize input using frame as context
380-
API.new(expanded_input, nil, options) do
376+
API.new(expanded_input, nil, options.merge(no_default_base: true)) do
381377
log_debug(".frame") {"expanded frame: #{expanded_frame.to_json(JSON_STATE) rescue 'malformed json'}"}
382378

383379
# Get framing nodes from expanded input, replacing Blank Node identifiers as necessary

lib/json/ld/context.rb

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,10 +1187,7 @@ def compact_iri(iri, value: nil, vocab: nil, reverse: false, quiet: false, **opt
11871187
candidates << ciri unless value && term_definitions.has_key?(ciri)
11881188
end if candidates.empty?
11891189

1190-
if !candidates.empty?
1191-
#log_debug("") {"=> compact iri: #{candidates.term_sort.first.inspect}"} unless quiet
1192-
return candidates.term_sort.first
1193-
end
1190+
return candidates.term_sort.first if !candidates.empty?
11941191

11951192
# If we still don't have any terms and we're using standard_prefixes,
11961193
# try those, and add to mapping
@@ -1203,13 +1200,10 @@ def compact_iri(iri, value: nil, vocab: nil, reverse: false, quiet: false, **opt
12031200
iri.sub(v.to_uri.to_s, "#{prefix}:").sub(/:$/, '')
12041201
end
12051202

1206-
if !candidates.empty?
1207-
#log_debug("") {"=> standard prefies: #{candidates.term_sort.first.inspect}"} unless quiet
1208-
return candidates.term_sort.first
1209-
end
1203+
return candidates.term_sort.first if !candidates.empty?
12101204
end
12111205

1212-
if !vocab && @options[:compactToRelative]
1206+
if !vocab
12131207
# transform iri to a relative IRI using the document's base IRI
12141208
iri = remove_base(iri)
12151209
#log_debug("") {"=> relative iri: #{iri.inspect}"} unless quiet

0 commit comments

Comments
 (0)