[yast-devel] WebYaST: how to pass empty string to rest-service?
Hi, in administrator module I have a POST request from client that contains the information about root password and mail aliases. Sometimes I would like to pass an empty value (for mail aliases, to reset it to no alias) and to distinguish the empty value from nil (nil has a meaning do not try to save this, while empty means save empty). So, for example I have this object in webclient controller: @administrator.password = nil @administrator.aliases = "" and I'm calling POST with @administrator.save However, on the server part, I get this as parameters: {"format"=>"xml", "action"=>"create", "administrator"=>{"aliases"=>nil, "password"=>nil}, "controller"=>"administrator"} So both values are set to nil. When debugging, we saw that the RAW_POST_DATA has a value "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<administrator>\n <aliases></aliases>\n <password nil=\"true\"></password>\n</administrator>\n" which is converted by from_xml this way even if aliases tag looks differently from password tag: Hash.from_xml("<?xml?>\n<administrator>\n <aliases></aliases>\n <password nil=\"true\"></password>\n</administrator>\n") {"administrator"=>{"aliases"=>nil, "password"=>nil}} It would work if the request would contain <aliases type='string'></aliases> instead of <aliases></aliases>, however I don't know how to achieve this; using xml.aliases aliases, {:type => "string"} in models/administrator.rb's to_xml doesn't seem to help. Jiri -- Jiri Suchomel SUSE LINUX, s.r.o. e-mail: jsuchome@suse.cz Lihovarská 1060/12 tel: +420 284 028 960 190 00 Praha 9, Czech Republic http://www.suse.cz -- To unsubscribe, e-mail: yast-devel+unsubscribe@opensuse.org For additional commands, e-mail: yast-devel+help@opensuse.org
On Wed, Sep 16, 2009 at 09:47:46AM +0200, Jiří Suchomel wrote:
Hi, in administrator module I have a POST request from client that contains the information about root password and mail aliases. Sometimes I would like to pass an empty value (for mail aliases, to reset it to no alias) and to distinguish the empty value from nil (nil has a meaning do not try to save this, while empty means save empty).
Rails will not let you :-( at least for hash values. Hash#from_xml has /usr/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/core_ext/hash/conversions.rb: # blank or nil parsed values are represented by nil elsif value.blank? || value['nil'] == 'true' nil ... "An object is blank if it‘s false, empty, or a whitespace string. For example, "", " ", nil, [], and {} are blank." http://api.rubyonrails.org/classes/Object.html#M000279 BTW, Hash#from_xml also looks if an array has a single element and unwraps it. Helpful, huh? If you cannot adapt your API to deal with this, I suggest writing (finding?) a stricter from_xml. -- Martin Vidner, YaST developer http://en.opensuse.org/User:Mvidner Kuracke oddeleni v restauraci je jako fekalni oddeleni v bazenu -- To unsubscribe, e-mail: yast-devel+unsubscribe@opensuse.org For additional commands, e-mail: yast-devel+help@opensuse.org
Martin Vidner napsal(a):
On Wed, Sep 16, 2009 at 09:47:46AM +0200, Jiří Suchomel wrote:
Hi, in administrator module I have a POST request from client that contains the information about root password and mail aliases. Sometimes I would like to pass an empty value (for mail aliases, to reset it to no alias) and to distinguish the empty value from nil (nil has a meaning do not try to save this, while empty means save empty).
Rails will not let you :-( at least for hash values.
Hash#from_xml has /usr/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/core_ext/hash/conversions.rb: # blank or nil parsed values are represented by nil elsif value.blank? || value['nil'] == 'true' nil ...
"An object is blank if it‘s false, empty, or a whitespace string. For example, "", " ", nil, [], and {} are blank." http://api.rubyonrails.org/classes/Object.html#M000279
BTW, Hash#from_xml also looks if an array has a single element and unwraps it. Helpful, huh?
If you cannot adapt your API to deal with this, I suggest writing (finding?) a stricter from_xml.
I think that for this case it is over-engineering. Because it is email, you have many values which should identify empty string. So I think you can set email to "NONE" and catch this value in backend. Pepa -- To unsubscribe, e-mail: yast-devel+unsubscribe@opensuse.org For additional commands, e-mail: yast-devel+help@opensuse.org
* Martin Vidner <mvidner@suse.cz> [Sep 16. 2009 10:19]:
On Wed, Sep 16, 2009 at 09:47:46AM +0200, Jiří Suchomel wrote:
Hi, in administrator module I have a POST request from client that contains the information about root password and mail aliases. Sometimes I would like to pass an empty value (for mail aliases, to reset it to no alias) and to distinguish the empty value from nil (nil has a meaning do not try to save this, while empty means save empty).
Rails will not let you :-( at least for hash values.
And even REST does not define this afaik. The payload for a POST request usually contains the complete instance (i.e. all attributes). The distinction of 'nil' (no change) vs. 'empty' (write empty value) is usually done on the service side, comparing the new with the current value and only writing changes. Klaus --- SUSE LINUX Products GmbH, GF: Markus Rex, HRB 16746 (AG Nürnberg) -- To unsubscribe, e-mail: yast-devel+unsubscribe@opensuse.org For additional commands, e-mail: yast-devel+help@opensuse.org
On Wed, Sep 16, 2009 at 10:33:18AM +0200, Klaus Kaempf wrote:
* Martin Vidner <mvidner@suse.cz> [Sep 16. 2009 10:19]:
On Wed, Sep 16, 2009 at 09:47:46AM +0200, Jiří Suchomel wrote:
Hi, in administrator module I have a POST request from client that contains the
And even REST does not define this afaik.
The payload for a POST request usually contains the complete instance (i.e. all attributes). The distinction of 'nil' (no change) vs. 'empty' (write empty value) is usually done on the service side, comparing the new with the current value and only writing changes.
Now that you mention it, maybe we could use the distinction between POST/create and PUT/update. -- Martin Vidner, YaST developer http://en.opensuse.org/User:Mvidner Kuracke oddeleni v restauraci je jako fekalni oddeleni v bazenu -- To unsubscribe, e-mail: yast-devel+unsubscribe@opensuse.org For additional commands, e-mail: yast-devel+help@opensuse.org
* Martin Vidner <mvidner@suse.cz> [Sep 16. 2009 10:42]:
On Wed, Sep 16, 2009 at 10:33:18AM +0200, Klaus Kaempf wrote:
* Martin Vidner <mvidner@suse.cz> [Sep 16. 2009 10:19]:
On Wed, Sep 16, 2009 at 09:47:46AM +0200, Jiří Suchomel wrote:
Hi, in administrator module I have a POST request from client that contains the
And even REST does not define this afaik.
The payload for a POST request usually contains the complete instance (i.e. all attributes). The distinction of 'nil' (no change) vs. 'empty' (write empty value) is usually done on the service side, comparing the new with the current value and only writing changes.
Now that you mention it, maybe we could use the distinction between POST/create and PUT/update.
Yes, thats possible. But it needs control over which attributes are passed via PUT, so you only pass those which changed. I do not know if ActiveResource provides control over this. Klaus --- SUSE LINUX Products GmbH, GF: Markus Rex, HRB 16746 (AG Nürnberg) -- To unsubscribe, e-mail: yast-devel+unsubscribe@opensuse.org For additional commands, e-mail: yast-devel+help@opensuse.org
participants (4)
-
Jiří Suchomel
-
josef reidinger
-
Klaus Kaempf
-
Martin Vidner