api.array :custom_fields do
  @custom_fields.each do |field|
    api.custom_field do
      api.id                field.id
      api.name              field.name
      api.description       field.description
      api.customized_type   field.class.customized_class.name.underscore if field.class.customized_class
      api.field_format      field.field_format
      api.regexp            field.regexp
      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?
      api.default_value     field[:default_value]
      api.default_value_mode(field.default_value_mode.presence || 'fixed_date') if field.field_format == 'date'
      api.visible           field.visible?
      api.editable          field.editable?

      values = field.possible_values_options
      if values.present?
        api.array :possible_values do
          values.each do |label, value|
            api.possible_value do
              api.value value || label
              api.label label
            end
          end
        end
      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
          end
        end
      end

      if %w(IssueCustomField TimeEntryCustomField ProjectCustomField VersionCustomField).include?(field.class.name)
        api.array :roles do
          field.roles.each do |role|
            api.role :id => role.id, :name => role.name
          end
        end
      end
    end
  end
end
