Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
trazaq opened this issue Nov 12, 2021 · 6 comments
Open

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

trazaq opened this issue Nov 12, 2021 · 6 comments

Comments

@trazaq
Copy link

trazaq commented Nov 12, 2021

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
Copy link
Author

trazaq commented Nov 12, 2021

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
Copy link

tugboat commented Nov 17, 2021

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
Copy link

tugboat commented Nov 17, 2021

@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
Copy link
Author

trazaq commented Nov 18, 2021

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
Copy link

antonivanopoulos commented Mar 24, 2022

@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
Copy link

tugboat commented Mar 31, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants