violet_rails icon indicating copy to clipboard operation
violet_rails copied to clipboard

[API Namespace] cannot change the order of properties for API Form

Open Ayon95 opened this issue 3 years ago • 3 comments

Properties of an object cannot be rearranged. They seem to get rearranged back to their initial order after hitting Save.

Suppose, I want the properties to be arranged in this order: Capture

The new order doesn't get applied after saving: Capture2

Ayon95 avatar May 13 '22 00:05 Ayon95

@Pralish has the most context on this

donrestarone avatar May 14 '22 01:05 donrestarone

we see that the issue is not in the client / params/ controller because the object property order is preserved while it is in the controller layer:

[42, 51] in /var/app/app/controllers/comfy/admin/api_namespaces_controller.rb
   42:
   43:   # PATCH/PUT /api_namespaces/1 or /api_namespaces/1.json
   44:   def update
   45:     respond_to do |format|
   46:       byebug
=> 47:       if @api_namespace.update(api_namespace_params)
   48:         format.html { handle_success_redirect }
   49:         format.json { render :show, status: :ok, location: @api_namespace }
   50:       else
   51:         format.html { handle_error_redirect }
(byebug) params
#<ActionController::Parameters {"_method"=>"patch", "authenticity_token"=>"lov7X4x6YBk8stvY1Zva-0drGB7ibzSF2GiYyNsOB9Uc3gibQQKXL6p0bu26AfomHre40XUO_klAFepn397ZxA", "api_namespace"=>{"name"=>"test", "version"=>"1", "properties"=>"{\"Object\":{\"a\":\"b\",\"c\":\"d\"},\"Null\":null,\"Array\":[1,2,3],\"Number\":123,\"String\":\"Hello World\",\"Boolean\":true}", "non_primitive_properties_attributes"=>{"0"=>{"field_type"=>"file", "label"=>"dfhdex", "content"=>"<div class=\"trix-content\">\r\n  <div class=\"trix-content\">\r\n  <div class=\"trix-content\">\r\n  \r\n</div>\r\n</div>\r\n</div>\r\n", "_destroy"=>"false", "id"=>"1"}, "1"=>{"field_type"=>"richtext", "label"=>"hdrthdrth", "content"=>"<div class=\"trix-content\">\r\n  <div class=\"trix-content\">\r\n  <div class=\"trix-content\">\r\n  \r\n</div>\r\n</div>\r\n</div>\r\n", "_destroy"=>"false", "id"=>"2"}}, "requires_authentication"=>"1", "has_form"=>"1"}, "commit"=>"Save", "controller"=>"comfy/admin/api_namespaces", "action"=>"update", "id"=>"test"} permitted: false>
(byebug) params[:api_namespace][:properties]
"{\"Object\":{\"a\":\"b\",\"c\":\"d\"},\"Null\":null,\"Array\":[1,2,3],\"Number\":123,\"String\":\"Hello World\",\"Boolean\":true}"
(byebug)

the above is the same order I submitted the form in (notice how Object is first, followed by Null and then Array) Screenshot from 2022-05-13 21-43-52

donrestarone avatar May 14 '22 01:05 donrestarone

according to @Pralish

The cause is that the jsonb field doesn't preserve order of objects, Inorder to preserve order we wld have to either store data as array, or use json field instead of jsonb or save the order somewhere else.

https://stackoverflow.com/questions/52123133/rails-jsonb-prevent-json-keys-from-reordering-when-jsonb-is-saved-to-postgresq

donrestarone avatar May 15 '22 16:05 donrestarone