Skip to content

Commit

Permalink
fix json serializer nil exception (#79)
Browse files Browse the repository at this point in the history
if hash was missing @context key the json serializer throwed a nil exception
  • Loading branch information
RaimiSol authored and syphax-bouazzouni committed Oct 19, 2023
1 parent 41e674f commit f44f7ba
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 52 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ group :development do
end

# NCBO gems (can be from a local dev path or from rubygems/git)
gem 'goo', github: 'ontoportal-lirmm/goo', branch: 'development'
gem 'goo', github: 'ontoportal-lirmm/goo', branch: 'master'
gem 'sparql-client', github: 'ontoportal-lirmm/sparql-client', branch: 'master'
88 changes: 47 additions & 41 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
GIT
remote: https://github.com/ontoportal-lirmm/goo.git
revision: 3d5bbe1db4a6aca2ff621ccfcdb85a32dbe9704e
branch: development
revision: bd7154217438c3b9160e0e9b495c7c718b55fbf8
branch: master
specs:
goo (0.0.2)
addressable (~> 2.8)
Expand Down Expand Up @@ -32,27 +32,28 @@ GEM
multi_json (~> 1.3)
thread_safe (~> 0.1)
tzinfo (~> 0.3.37)
addressable (2.8.1)
addressable (2.8.5)
public_suffix (>= 2.0.2, < 6.0)
ansi (1.5.0)
ast (2.4.2)
bcrypt (3.1.18)
base64 (0.1.1)
bcrypt (3.1.19)
builder (3.2.4)
coderay (1.1.3)
concurrent-ruby (1.1.10)
connection_pool (2.3.0)
concurrent-ruby (1.2.2)
connection_pool (2.4.1)
cube-ruby (0.0.3)
daemons (1.4.1)
date (3.3.2)
date (3.3.3)
docile (1.4.0)
domain_name (0.5.20190701)
unf (>= 0.0.5, < 1.0.0)
email_spec (2.2.1)
email_spec (2.2.2)
htmlentities (~> 4.3.3)
launchy (~> 2.1)
mail (~> 2.7)
eventmachine (1.2.7)
faraday (1.10.2)
faraday (1.10.3)
faraday-em_http (~> 1.0)
faraday-em_synchrony (~> 1.0)
faraday-excon (~> 1.1)
Expand All @@ -75,7 +76,7 @@ GEM
faraday-patron (1.0.0)
faraday-rack (1.0.0)
faraday-retry (1.0.3)
ffi (1.15.5)
ffi (1.16.3)
hashie (5.0.0)
htmlentities (4.3.4)
http-accept (1.7.0)
Expand All @@ -85,91 +86,96 @@ GEM
concurrent-ruby (~> 1.0)
json (2.6.3)
json_pure (2.6.3)
launchy (2.5.0)
addressable (~> 2.7)
language_server-protocol (3.17.0.3)
launchy (2.5.2)
addressable (~> 2.8)
libxml-ruby (2.9.0)
logger (1.5.3)
macaddr (1.7.2)
systemu (~> 2.6.5)
mail (2.8.0)
mail (2.8.1)
mini_mime (>= 0.1.1)
net-imap
net-pop
net-smtp
method_source (1.0.0)
mime-types (3.4.1)
mime-types (3.5.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2022.0105)
mini_mime (1.1.2)
mime-types-data (3.2023.1003)
mini_mime (1.1.5)
minitest (4.7.5)
minitest-reporters (0.14.24)
ansi
builder
minitest (>= 2.12, < 5.0)
powerbar
multi_json (1.15.0)
multipart-post (2.2.3)
multipart-post (2.3.0)
net-http-persistent (2.9.4)
net-imap (0.3.2)
net-imap (0.4.1)
date
net-protocol
net-pop (0.1.2)
net-protocol
net-protocol (0.2.1)
timeout
net-smtp (0.3.3)
net-smtp (0.4.0)
net-protocol
netrc (0.11.0)
oj (2.18.5)
omni_logger (0.1.4)
logger
parallel (1.22.1)
parser (3.1.3.0)
parallel (1.23.0)
parser (3.2.2.4)
ast (~> 2.4.1)
racc
pony (1.13.1)
mail (>= 2.0)
powerbar (2.0.1)
hashie (>= 1.1.0)
pry (0.14.1)
pry (0.14.2)
coderay (~> 1.1)
method_source (~> 1.0)
public_suffix (5.0.1)
public_suffix (5.0.3)
racc (1.7.1)
rack (1.6.13)
rack-test (0.8.3)
rack (>= 1.0, < 3)
rainbow (3.1.1)
rake (10.5.0)
rdf (1.0.8)
addressable (>= 2.2)
redis (5.0.5)
redis (5.0.7)
redis-client (>= 0.9.0)
redis-client (0.11.2)
redis-client (0.17.0)
connection_pool
regexp_parser (2.6.1)
regexp_parser (2.8.2)
rest-client (2.1.0)
http-accept (>= 1.7.0, < 2.0)
http-cookie (>= 1.0.2, < 2.0)
mime-types (>= 1.16, < 4.0)
netrc (~> 0.8)
rexml (3.2.5)
rexml (3.2.6)
rsolr (1.1.2)
builder (>= 2.1.2)
rubocop (1.40.0)
rubocop (1.57.1)
base64 (~> 0.1.1)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
parallel (~> 1.10)
parser (>= 3.1.2.1)
parser (>= 3.2.2.4)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml (>= 3.2.5, < 4.0)
rubocop-ast (>= 1.23.0, < 2.0)
rubocop-ast (>= 1.28.1, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 3.0)
rubocop-ast (1.24.0)
parser (>= 3.1.1.0)
ruby-progressbar (1.11.0)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.29.0)
parser (>= 3.2.1.0)
ruby-progressbar (1.13.0)
ruby2_keywords (0.0.5)
rubyzip (1.3.0)
simplecov (0.21.2)
simplecov (0.22.0)
docile (~> 1.1)
simplecov-html (~> 0.11)
simplecov_json_formatter (~> 0.1)
Expand All @@ -181,22 +187,22 @@ GEM
systemu (2.6.5)
test-unit-minitest (0.9.1)
minitest (~> 4.7)
thin (1.8.1)
thin (1.8.2)
daemons (~> 1.0, >= 1.0.9)
eventmachine (~> 1.0, >= 1.0.4)
rack (>= 1, < 3)
thread_safe (0.3.6)
timeout (0.3.1)
tzinfo (0.3.61)
timeout (0.4.0)
tzinfo (0.3.62)
unf (0.1.4)
unf_ext
unf_ext (0.0.8.2)
unicode-display_width (2.3.0)
unicode-display_width (2.5.0)
uuid (2.3.9)
macaddr (~> 1.0)

PLATFORMS
x86_64-darwin-21
x86_64-linux

DEPENDENCIES
activesupport (~> 4)
Expand Down Expand Up @@ -229,4 +235,4 @@ DEPENDENCIES
thin

BUNDLED WITH
2.3.15
2.3.26
43 changes: 33 additions & 10 deletions lib/ontologies_linked_data/serializers/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class JSON
def self.serialize(obj, options = {})
hash = obj.to_flex_hash(options) do |hash, hashed_obj|
current_cls = hashed_obj.respond_to?(:klass) ? hashed_obj.klass : hashed_obj.class
result_lang = self.get_languages(get_object_submission(hashed_obj), options[:lang])

# Add the id to json-ld attribute
if current_cls.ancestors.include?(LinkedData::Hypermedia::Resource) && !current_cls.embedded? && hashed_obj.respond_to?(:id)
Expand All @@ -27,7 +28,7 @@ def self.serialize(obj, options = {})
hash["links"].merge!(generate_links_context(hashed_obj)) if generate_context?(options)
end
end

# Generate context
if current_cls.ancestors.include?(Goo::Base::Resource) && !current_cls.embedded?
if generate_context?(options)
Expand All @@ -40,13 +41,36 @@ def self.serialize(obj, options = {})
context = {"@context" => context_hash}
hash.merge!(context)
end
hash['@context']['@language'] = options[:lang]
hash['@context']['@language'] = result_lang if hash['@context']
end
MultiJson.dump(hash)
end

private

def self.get_object_submission(obj)
obj.class.respond_to?(:attributes) && obj.class.attributes.include?(:submission) ? obj.submission : nil
end

def self.get_languages(submission, user_languages)
result_lang = user_languages

if submission
submission.bring :naturalLanguage
languages = get_submission_languages(submission.naturalLanguage)
# intersection of the two arrays , if the requested language is not :all
result_lang = user_languages == :all ? languages : Array(user_languages) & languages
result_lang = result_lang.first if result_lang.length == 1
end

result_lang
end

def self.get_submission_languages(submission_natural_language = [])
submission_natural_language = submission_natural_language.values.flatten if submission_natural_language.is_a?(Hash)
submission_natural_language.map { |natural_language| natural_language.to_s['iso639'] && natural_language.to_s.split('/').last[0..1].to_sym }.compact
end

def self.type(current_cls, hashed_obj)
if current_cls.respond_to?(:type_uri)
# For internal class
Expand Down Expand Up @@ -122,20 +146,19 @@ def self.generate_context?(options)
params = options[:params]
params.nil? ||
(params["no_context"].nil? ||
!params["no_context"].eql?("true")) &&
(params["display_context"].nil? ||
!params["display_context"].eql?("false"))
!params["no_context"].eql?("true")) &&
(params["display_context"].nil? ||
!params["display_context"].eql?("false"))
end

def self.generate_links?(options)
params = options[:params]
params.nil? ||
(params["no_links"].nil? ||
!params["no_links"].eql?("true")) &&
(params["display_links"].nil? ||
!params["display_links"].eql?("false"))
!params["no_links"].eql?("true")) &&
(params["display_links"].nil? ||
!params["display_links"].eql?("false"))
end
end
end
end

end

0 comments on commit f44f7ba

Please sign in to comment.