Skip to content

Commit

Permalink
FIX: allow array values for custom fields in category params. (#17692)
Browse files Browse the repository at this point in the history
Previously, when we used `params[:custom_fields].try(:keys)` code it worked for all the custom fields unless it's an array. It created the problem in the discourse-restricted-replies plugin.

discourse/discourse-restricted-replies#37 (comment)
  • Loading branch information
vinothkannans committed Jul 28, 2022
1 parent ff78a1e commit 72b24f3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
11 changes: 10 additions & 1 deletion app/controllers/categories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ def category_params
:read_only_banner,
:default_list_filter,
:reviewable_by_group_id,
custom_fields: [params[:custom_fields].try(:keys)],
custom_fields: [custom_field_params],
permissions: [*p.try(:keys)],
allowed_tags: [],
allowed_tag_groups: [],
Expand All @@ -386,6 +386,15 @@ def category_params
end
end

def custom_field_params
keys = params[:custom_fields].try(:keys)
return if keys.blank?

keys.map do |key|
params[:custom_fields][key].is_a?(Array) ? { key => [] } : key
end
end

def fetch_category
@category = Category.find_by_slug(params[:id]) || Category.find_by(id: params[:id].to_i)
raise Discourse::NotFound if @category.blank?
Expand Down
5 changes: 3 additions & 2 deletions spec/requests/categories_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,8 @@
"staff" => create_post
},
custom_fields: {
"dancing" => "frogs"
"dancing" => "frogs",
"running" => ["turtle", "salamander"]
},
minimum_required_tags: "",
allow_global_tags: 'true',
Expand All @@ -539,7 +540,7 @@
expect(category.slug).to eq("hello-category")
expect(category.color).to eq("ff0")
expect(category.auto_close_hours).to eq(72)
expect(category.custom_fields).to eq("dancing" => "frogs")
expect(category.custom_fields).to eq("dancing" => "frogs", "running" => ["turtle", "salamander"])
expect(category.minimum_required_tags).to eq(0)
expect(category.allow_global_tags).to eq(true)
expect(category.category_required_tag_groups.count).to eq(1)
Expand Down

1 comment on commit 72b24f3

@discoursebot
Copy link

Choose a reason for hiding this comment

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

This commit has been mentioned on Discourse Meta. There might be relevant details there:

https://meta.discourse.org/t/500-error-when-saving-category-security-settings/231055/6

Please sign in to comment.