Skip to content

Commit 36895d6

Browse files
committed
Make sure that serialized context uses strings, and not RDF::URI.
1 parent 1c5a711 commit 36895d6

File tree

4 files changed

+66
-13
lines changed

4 files changed

+66
-13
lines changed

lib/json/ld/api.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ def self.frame(input, frame, options = {})
330330
debug(".frame") {"node_map: #{@node_map.to_json(JSON_STATE)}"}
331331

332332
result = []
333-
frame(framing_state, @node_map, (expanded_frame.first || {}), result, nil)
333+
frame(framing_state, @node_map, (expanded_frame.first || {}), parent: result)
334334
debug(".frame") {"after frame: #{result.to_json(JSON_STATE)}"}
335335

336336
# Initalize context from frame

lib/json/ld/context.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Context
88

99
# Term Definitions specify how properties and values have to be interpreted as well as the current vocabulary mapping and the default language
1010
class TermDefinition
11-
# @return [String] IRI map
11+
# @return [RDF::URI] IRI map
1212
attr_accessor :id
1313

1414
# @return [String] term name
@@ -63,10 +63,10 @@ def to_context_definition(context)
6363
type_mapping.nil? &&
6464
reverse_property.nil?
6565

66-
cid unless cid == term && context.vocab
66+
cid.to_s unless cid == term && context.vocab
6767
else
6868
defn = {}
69-
defn[reverse_property ? '@reverse' : '@id'] = cid unless cid == term && !reverse_property
69+
defn[reverse_property ? '@reverse' : '@id'] = cid.to_s unless cid == term && !reverse_property
7070
if type_mapping
7171
defn['@type'] = if KEYWORDS.include?(type_mapping)
7272
type_mapping
@@ -126,7 +126,7 @@ def inspect
126126
#
127127
# Sets the default vocabulary used for expanding terms which
128128
# aren't otherwise absolute IRIs
129-
# @return [String]
129+
# @return [RDF::URI]
130130
attr_reader :vocab
131131

132132
# @return [Hash{Symbol => Object}] Global options used in generating IRIs

lib/json/ld/frame.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ module Frame
1010
# @param [Hash{String => Hash}] nodes
1111
# Map of flattened nodes
1212
# @param [Hash{String => Object}] frame
13-
# @param [Hash{String => Object}] parent
13+
# @param [Hash{Symbol => Object}] options ({})
14+
# @option options [Hash{String => Object}] :parent
1415
# Parent node or top-level array
15-
# @param [String] property
16+
# @option options [String] :property
1617
# Property referencing this frame, or null for array.
1718
# @raise [JSON::LD::InvalidFrame]
18-
def frame(state, nodes, frame, parent, property)
19+
def frame(state, nodes, frame, options = {})
1920
depth do
21+
parent, property = options[:parent], options[:property]
2022
debug("frame") {"state: #{state.inspect}"}
2123
debug("frame") {"nodes: #{nodes.keys.inspect}"}
2224
debug("frame") {"frame: #{frame.to_json(JSON_STATE)}"}
@@ -48,9 +50,7 @@ def frame(state, nodes, frame, parent, property)
4850

4951
# If embedOn is true, and id is in map of embeds from state
5052
if embed && (existing = state[:embeds].fetch(id, nil))
51-
# only overwrite an existing embed if it has already been added to its
52-
# parent -- otherwise its parent is somewhere up the tree from this
53-
# embed and the embed would occur twice once the tree is added
53+
# only overwrite an existing embed if it has already been added to its parent -- otherwise its parent is somewhere up the tree from this embed and the embed would occur twice once the tree is added
5454
embed = false
5555

5656
embed = if existing[:parent].is_a?(Array)
@@ -114,7 +114,7 @@ def frame(state, nodes, frame, parent, property)
114114
debug("frame") {"list item of #{prop} recurse for #{itemid.inspect}"}
115115

116116
# If listitem is a node reference process listitem recursively using this algorithm passing a new map of nodes that contains the @id of listitem as the key and the node reference as the value. Pass the first value from frame for property as frame, list as parent, and @list as active property.
117-
frame(state, {itemid => @node_map[itemid]}, frame[prop].first, list, '@list')
117+
frame(state, {itemid => @node_map[itemid]}, frame[prop].first, parent: list, property: '@list')
118118
else
119119
# Otherwise, append a copy of listitem to @list in list.
120120
debug("frame") {"list item of #{prop} non-node ref #{listitem.inspect}"}
@@ -128,7 +128,7 @@ def frame(state, nodes, frame, parent, property)
128128
debug("frame") {"value property #{prop} recurse for #{itemid.inspect}"}
129129

130130
# passing a new map as nodes that contains the @id of item as the key and the node reference as the value. Pass the first value from frame for property as frame, output as parent, and property as active property
131-
frame(state, {itemid => @node_map[itemid]}, frame[prop].first, output, prop)
131+
frame(state, {itemid => @node_map[itemid]}, frame[prop].first, parent: output, property: prop)
132132
else
133133
# Otherwise, append a copy of item to active property in output.
134134
debug("frame") {"value property #{prop} non-node ref #{item.inspect}"}

spec/frame_spec.rb

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,5 +512,58 @@
512512
end
513513
end
514514
end
515+
516+
describe "@reverse", skip:true do
517+
{
518+
"embed matched frames with @reverse" => {
519+
:frame => {
520+
"@context" => {"ex" => "http://example.org/"},
521+
"@type" => "ex:Type1",
522+
"@reverse" => {
523+
"ex:includes" => {}
524+
}
525+
},
526+
:input => [
527+
{
528+
"@context" => {"ex" => "http://example.org/"},
529+
"@id" => "ex:Sub1",
530+
"@type" => "ex:Type1"
531+
},
532+
{
533+
"@context" => {"ex" => "http://example.org/"},
534+
"@id" => "ex:Sub2",
535+
"@type" => "ex:Type2",
536+
"ex:includes" => {"@id" => "ex:Sub1"}
537+
},
538+
],
539+
:output =>{
540+
"@context" => {"ex" => "http://example.org/"},
541+
"@graph" => [{
542+
"@id" => "ex:Sub1",
543+
"@type" => "ex:Type1",
544+
"@reverse" => {
545+
"ex:includes" => {
546+
"@id" => "ex:Sub2",
547+
"@type" => "ex:Type2",
548+
"ex:includes" => {"@id" => "ex:Sub1"}
549+
}
550+
}
551+
}]
552+
}
553+
},
554+
}.each do |title, params|
555+
it title do
556+
@debug = []
557+
begin
558+
jld = JSON::LD::API.frame(params[:input], params[:frame], :debug => @debug)
559+
expect(jld).to produce(params[:output], @debug)
560+
rescue JSON::LD::JsonLdError, JSON::LD::JsonLdError, JSON::LD::InvalidFrame => e
561+
fail("#{e.class}: #{e.message}\n" +
562+
"#{@debug.join("\n")}\n" +
563+
"Backtrace:\n#{e.backtrace.join("\n")}")
564+
end
565+
end
566+
end
567+
end
515568
end
516569
end

0 commit comments

Comments
 (0)