From c2730d5295bf2d2bd63830967717257ccd52f830 Mon Sep 17 00:00:00 2001 From: MAEDA Go Date: Sun, 7 Jun 2026 17:05:34 +0900 Subject: [PATCH] Add projects to issue custom fields API response --- app/controllers/custom_fields_controller.rb | 7 +++- app/views/custom_fields/index.api.rsb | 7 ++++ .../api_test/custom_fields_test.rb | 35 +++++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/app/controllers/custom_fields_controller.rb b/app/controllers/custom_fields_controller.rb index b170dbae3..66f2b12b9 100644 --- a/app/controllers/custom_fields_controller.rb +++ b/app/controllers/custom_fields_controller.rb @@ -34,7 +34,12 @@ class CustomFieldsController < ApplicationController IssueCustomField.where(is_for_all: false).joins(:projects).group(:custom_field_id).count end format.api do - @custom_fields = CustomField.includes(:roles).all + @custom_fields = CustomField.includes(:roles).all.to_a + issue_custom_fields = @custom_fields.grep(IssueCustomField) + ActiveRecord::Associations::Preloader.new( + records: issue_custom_fields, + associations: [:projects, :trackers] + ).call end end end diff --git a/app/views/custom_fields/index.api.rsb b/app/views/custom_fields/index.api.rsb index 82431805a..62c643bcc 100644 --- a/app/views/custom_fields/index.api.rsb +++ b/app/views/custom_fields/index.api.rsb @@ -10,6 +10,7 @@ api.array :custom_fields do api.min_length field.min_length api.max_length field.max_length api.is_required field.is_required? + api.is_for_all field.is_for_all? api.is_filter field.is_filter? api.searchable field.searchable api.multiple field.multiple? @@ -30,6 +31,12 @@ api.array :custom_fields do end if field.is_a?(IssueCustomField) + api.array :projects do + field.projects.each do |project| + api.project :id => project.id, :name => project.name + end + end + api.array :trackers do field.trackers.each do |tracker| api.tracker :id => tracker.id, :name => tracker.name diff --git a/test/integration/api_test/custom_fields_test.rb b/test/integration/api_test/custom_fields_test.rb index f26983686..713d297c0 100644 --- a/test/integration/api_test/custom_fields_test.rb +++ b/test/integration/api_test/custom_fields_test.rb @@ -31,10 +31,12 @@ class Redmine::ApiTest::CustomFieldsTest < Redmine::ApiTest::Base assert_select 'description', :text => 'Select one of the databases' assert_select 'id', :text => '2' assert_select 'customized_type', :text => 'issue' + assert_select 'is_for_all', :text => 'true' assert_select 'possible_values[type=array]' do assert_select 'possible_value>value', :text => 'PostgreSQL' assert_select 'possible_value>label', :text => 'PostgreSQL' end + assert_select 'projects[type=array]' assert_select 'trackers[type=array]' assert_select 'roles[type=array]' assert_select 'visible', :text => 'true' @@ -57,6 +59,39 @@ class Redmine::ApiTest::CustomFieldsTest < Redmine::ApiTest::Base end end + test "GET /custom_fields.xml should include projects for issue custom fields limited to projects" do + get '/custom_fields.xml', :headers => credentials('admin') + assert_response :success + + xml = Hash.from_xml(response.body) + field = xml['custom_fields'].detect {|f| f['id'] == '9'} + assert_kind_of Hash, field + assert_equal 'false', field['is_for_all'] + assert_kind_of Array, field['projects'] + assert_equal [{'id' => '1', 'name' => 'eCookbook'}], field['projects'] + end + + test "GET /custom_fields.xml should include empty projects for issue custom fields for all projects" do + get '/custom_fields.xml', :headers => credentials('admin') + assert_response :success + + xml = Hash.from_xml(response.body) + field = xml['custom_fields'].detect {|f| f['id'] == '1'} + assert_kind_of Hash, field + assert_equal 'true', field['is_for_all'] + assert_equal [], field['projects'] + end + + test "GET /custom_fields.xml should not include projects for custom fields that are not issue custom fields" do + get '/custom_fields.xml', :headers => credentials('admin') + assert_response :success + + xml = Hash.from_xml(response.body) + field = xml['custom_fields'].detect {|f| f['id'] == '3'} + assert_kind_of Hash, field + assert_not field.has_key?('projects') + end + test "GET /custom_fields.xml should include roles for custom fields visible by role" do custom_fields = [ IssueCustomField.generate!(:visible => false, :role_ids => [1, 2]), -- 2.50.1