Skip to content
This repository has been archived by the owner on May 23, 2024. It is now read-only.

initializer modifies class #68

Open
durandom opened this issue Feb 9, 2017 · 5 comments
Open

initializer modifies class #68

durandom opened this issue Feb 9, 2017 · 5 comments

Comments

@durandom
Copy link
Contributor

durandom commented Feb 9, 2017

I'm a bit worried about

    def initialize(api, json_or_hash)
      @api = api

      raw_hash = json_or_hash.kind_of?(Hash) ? json_or_hash : JSON.parse(json_or_hash)
      self.class.send(:id_attr, *raw_hash['related'].keys) if raw_hash.key?('related')

      super(raw_hash)
    end

in base_model introduced in #17

Not only does it modify a class whenever an instance is created, which can lead to different behaviour across different instances. It's also not thread safe, potentially leading to data corruption.

I'm also seeing inconsistent behaviour when accessing these _id methods

(byebug) ap i.class
AnsibleTowerClient::JobTemplate < AnsibleTowerClient::BaseModel
(byebug) ap i.to_hash['related']
{
                        "created_by" => "/api/v1/users/1/",
                       "modified_by" => "/api/v1/users/1/",
                            "labels" => "/api/v1/job_templates/76/labels/",
                         "inventory" => "/api/v1/inventories/6/",
                           "project" => "/api/v1/projects/40/",
                        "credential" => "/api/v1/credentials/6/",
      "notification_templates_error" => "/api/v1/job_templates/76/notification_templates_error/",
    "notification_templates_success" => "/api/v1/job_templates/76/notification_templates_success/",
                              "jobs" => "/api/v1/job_templates/76/jobs/",
                      "object_roles" => "/api/v1/job_templates/76/object_roles/",
        "notification_templates_any" => "/api/v1/job_templates/76/notification_templates_any/",
                       "access_list" => "/api/v1/job_templates/76/access_list/",
                            "launch" => "/api/v1/job_templates/76/launch/",
                         "schedules" => "/api/v1/job_templates/76/schedules/",
                   "activity_stream" => "/api/v1/job_templates/76/activity_stream/",
                       "survey_spec" => "/api/v1/job_templates/76/survey_spec/"
}
(byebug) ap i.cloud_credential
nil
(byebug) ap i.cloud_credential_id
nil
(byebug) ap i.credential
*** NoMethodError Exception: undefined method `credential' for #<AnsibleTowerClient::JobTemplate:0x007f9a068e92f0>
Did you mean?  credential_id

nil
(byebug) ap i.credential_id
6

TBH, I would favour no magic id columns and stick as close to the response as possible

@durandom
Copy link
Contributor Author

durandom commented Feb 9, 2017

cc @bdunne @bzwei @syncrou

@bdunne
Copy link
Contributor

bdunne commented Feb 9, 2017

@durandom I would expect i.credential to be an AnsibleTowerCredential instance, not "/api/v1/credentials/6", especially since that referral may not be correct.

I agree that it shouldn't modify the class on instance create and your concern about thread safety.

@durandom
Copy link
Contributor Author

durandom commented Feb 9, 2017

I would expect i.credential to be an AnsibleTowerCredential instance, not "/api/v1/credentials/6"

The above hash is the related key from the api response. i.credential would be 6 if its was just the mapping to the response.

But you say it should be an instance of AnsibleTowerCredential and not and undefined method?

So there might be another bug?

@bdunne
Copy link
Contributor

bdunne commented Feb 9, 2017

yeah, sounds like a bug to me.

@miq-bot
Copy link
Collaborator

miq-bot commented Jun 12, 2020

This issue has been automatically marked as stale because it has not been updated for at least 3 months.

If you can still reproduce this issue on the current release or on master, please reply with all of the information you have about it in order to keep the issue open.

Thank you for all your contributions! More information about the ManageIQ triage process can be found in the traige process documentation.

@miq-bot miq-bot added the stale label Jun 12, 2020
@bdunne bdunne added pinned and removed stale labels Jun 15, 2020
@bdunne bdunne removed their assignment Oct 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants