fhir-crucible / fhir_client

Ruby FHIR Client
Apache License 2.0
163 stars 61 forks source link

NoMethodError: undefined method `has_key?' for nil:NilClass #147

Open trazaq opened 2 years ago

trazaq commented 2 years ago

HI,

Thanks for this gem!

I'm getting a error after following the transaction example in the README.

reply = client.end_transaction
I, [2021-11-12T22:50:03.778516 #1]  INFO -- : POSTING: https://apporchard.epic.com/interconnect-aocurprd-oauth/api/FHIR/R4/
NoMethodError: undefined method `has_key?' for nil:NilClass
from /usr/local/bundle/ruby/2.6.0/gems/fhir_models-4.2.1/lib/fhir_models/bootstrap/json.rb:10:in `pretty_generate'

Any ideas?

trazaq commented 2 years ago

It doesn't seem to like the JSON.pretty_generate(to_hash, opts) if I'm reading it correctly

require 'json'

module FHIR
  module Json
    #
    #  This module includes methods to serialize or deserialize FHIR resources to and from JSON.
    #

    def to_json(opts = nil)
      JSON.pretty_generate(to_hash, opts)
    end

    def self.from_json(json)
      hash = JSON.parse(json)
      resource = nil
      begin
        resource_type = hash['resourceType']
        klass = Module.const_get("FHIR::#{resource_type}")
        resource = klass.new(hash)
      rescue => e
        FHIR.logger.error("Failed to deserialize JSON:\n#{e.backtrace}")
        FHIR.logger.debug("JSON:\n#{json}")
        resource = nil
      end
      resource
    end
  end
end
tugboat commented 2 years ago

I am having the same issue. We are running ruby version 3.0.1, could that be a potential cause of this?

If I make a call to to_json and pass an empty hash as an argument it seems to serialize correctly.

tugboat commented 2 years ago

@trazaq, for now I was able to work around this by overriding the to_json method to pass an empty hash.

I am using the gem in a rails app so I was able to create a file at config/initializers/overrides.rb with the following contents.

module FHIR
  module Json
    def to_json(opts = {})
      super
    end
  end
end

That has me going for now, but I am not sure what the implications of doing something like that might be for your code, so YMMV.

trazaq commented 2 years ago

I appreciate the authors of this gem! It does indeed come in handy!

@tugboat

Ah! That's one way of going about it!

Anyway, For me, I couldn't really wait around for an answer because I'm on a timeline so I decided to use FHIR models only of this gem. (I do realize there's a separate gem for models only, too)

I'm not using this gem's internal HTTP client.

I construct the models and use HTTParty to do the actual sending/receiving of data as my HTTP client.

Works well so far. Just have to remember to convert your model to a hash, then to json in my case before sending it to the receiving system.

antonivanopoulos commented 2 years ago

@trazaq @tugboat Do either of you use Oj? I ran into the same issue and it was due to our current use of Oj's optimize_rails method that redefines the JSON module, introducing the behaviour you're seeing on #pretty_generate. I found that if just using the json gem, it doesn't behave that way.

tugboat commented 2 years ago

@antonivanopoulos we are using oj in the application where I was running into this issue. That is probably the cause for me.