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

Replace all characters that cannot be used as method or class name #105

Merged
merged 1 commit into from
Jul 6, 2018

Conversation

bzwei
Copy link
Contributor

@bzwei bzwei commented May 30, 2018

Any key in the original hash is used in the base model for two purpose:

  1. underscore version is used as a method
  2. camelized version is used as a class
    Either case only accepts alphabetic letters, numbers, and _. All other characters are now replaced.

fixes https://bugzilla.redhat.com/show_bug.cgi?id=1583844

@bzwei
Copy link
Contributor Author

bzwei commented May 30, 2018

In my dev env I don't see any spec failure.

@miq-bot
Copy link
Collaborator

miq-bot commented May 30, 2018

Checked commit bzwei@6993b25 with ruby 2.3.3, rubocop 0.52.1, haml-lint 0.20.0, and yamllint 1.10.0
2 files checked, 0 offenses detected
Everything looks fine. 👍

Copy link

@tinaafitz tinaafitz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bzwei Looks good.
@mkanoor Please review.

@bzwei
Copy link
Contributor Author

bzwei commented May 31, 2018

@bdunne do you know why the spec failed the way?

@@ -5,7 +5,8 @@
"name" => "jeff",
"address" => { "street" => "22 charlotte rd"},
"serialized_json" => { "key" => "value"}.to_json,
"serialized_yaml" => { "key" => "value"}.to_yaml
"serialized_yaml" => { "key" => "value"}.to_yaml,
":Special$ Key3" => { "key" => "value"}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did Tower create a new attribute with special characters in it? In the BZ and the PR description, it is not obvious to me what the problem is.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the BZ, he was creating an ec2 instance with a /dev/xvda in it. When /dev/xvda being the key, the parsing and conversion code fails. I could change the test to use /dev/xvda.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It sounds like this is buried in extra vars or something that we probably shouldn't be parsing and turning into methods, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am attempting to give a general solution to avoid coming back when another similar case arises.
Consider the following json

{
  "abc" : {
    "xyz": "value1",
    "/dev/xvda": {
      "mykey": "myvalue"
    }
  }
}

Ideally we should want to access value1 through abc.xyz. The second key can be accessed either by abc._dev_xvda or abc['/dev/xvda'], but its value will still be a converted model rather than raw hash.

The problem reported in BZ is not an extra var. Those we should not parse and convert are already wrapped in string (not parsable).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess what I'm trying to say is that the code is currently trying to parse everything and turn it into classes and methods and we should probably only do that for the known classes.

Copy link
Contributor

@syncrou syncrou Jun 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am leaning with @bdunne with this. /dev/xda caught us off guard here, which means more odd cases will show up later. I think we should be white listing classes and only constantize name spaced AnsibleTowerClient classes a lot like how safe_constantize works. Depending on a regex feels brittle.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The purpose of auto conversion in base model is to provide convenience for method style access to (nested) properties. It does NOT fundamentally change the data. You can always access the raw data treating it as a hash, ignoring any auto generated methods and classes.

Secondly the name of the auto-generated embedded class does not matter. It can be anonymous or any random valid name because what we really want is the methods. Here we just try to make the class name more meaningful.

If we want to white list classes, then we should white list methods too. Since we need to white list everything we should really abandon the base class, at least the auto conversion part. In stead we should create every class with known attributes. I am fine with this approach, but it will not to be addressed by this PR.

@gmcculloug
Copy link
Contributor

@syncrou Please review.

@@ -5,7 +5,8 @@
"name" => "jeff",
"address" => { "street" => "22 charlotte rd"},
"serialized_json" => { "key" => "value"}.to_json,
"serialized_yaml" => { "key" => "value"}.to_yaml
"serialized_yaml" => { "key" => "value"}.to_yaml,
":Special$ Key3" => { "key" => "value"}
Copy link
Contributor

@syncrou syncrou Jun 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am leaning with @bdunne with this. /dev/xda caught us off guard here, which means more odd cases will show up later. I think we should be white listing classes and only constantize name spaced AnsibleTowerClient classes a lot like how safe_constantize works. Depending on a regex feels brittle.

@JPrause
Copy link

JPrause commented Jun 21, 2018

@bzwei were there further edit/reviews needed for this PR.

@bzwei
Copy link
Contributor Author

bzwei commented Jun 21, 2018

@JPrause To me the fix is good for the linked BZ. The review comments were debating whether we should do auto modeling of the raw data which can be addressed in another PR.

@JPrause
Copy link

JPrause commented Jun 22, 2018

Thanks @bzwei
Who can merge this PR?

@gmcculloug
Copy link
Contributor

@Fryguy Are you ok with the suggestion in #105 (comment) to address concerns in a followup PR? If so I'll open a git issue to track it.

@Fryguy
Copy link
Contributor

Fryguy commented Jun 22, 2018

I'll defer to @bdunne

On first glance this seems reasonable, but I'm concerned with we choose this and then change our minds later, then we have an API compatibility break

@JPrause
Copy link

JPrause commented Jul 3, 2018

@bdunne ping. See prior comments

@bzwei
Copy link
Contributor Author

bzwei commented Jul 3, 2018

@JPrause
Copy link

JPrause commented Jul 6, 2018

@bzwei I spoke with Brandon yesterday and he said he discussed this PR with you. Is there further work needed on this PR.

@bdunne
Copy link
Contributor

bdunne commented Jul 6, 2018

I think I'm going to merge this for now to get the BZ out of the way.

I still think we should not be creating random classes for objects that are not defined by the api, but all this PR does is prevent us from trying to create class names with invalid characters.

@bdunne bdunne merged commit 1c61676 into ansible:master Jul 6, 2018
@bdunne
Copy link
Contributor

bdunne commented Jul 6, 2018

Normally I would expect this type of change to require a major version bump, but since I see it as an unintentional "feature", I'm going with a minor version bump since it doesn't affect any of the intentionally created classes (those defined by Tower's API).

@bzwei bzwei deleted the qualify_class_name branch July 6, 2018 19:59
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants