Defect #22745

Rest API for Custom Fields does not return keys for key/value types

Added by Stephen Stuart over 2 years ago. Updated over 2 years ago.

Status:ClosedStart date:
Priority:NormalDue date:
Assignee:Jean-Philippe Lang% Done:

0%

Category:REST API
Target version:3.3.0
Resolution:Fixed Affected version:3.2.1

Description

When using the REST API to retrieve the list of custom_fields by calling http://localhost:3000/custom_fields.json , if there is a Key/Value list type custom fields only the value comes back, as in the example below:

"id": 5,
"name": "Lifecycle Status",
"customized_type": "version",
"field_format": "enumeration",
"regexp": "",
"visible": true,
"possible_values": [
  {
    "value": "2",
  },
  {
    "value": "3",
  },
  {
    "value": "4",
  }
]

to replace this block:
It would be much more useful to get the name back as well, like:
"id": 5,
"name": "Lifecycle Status",
"customized_type": "version",
"field_format": "enumeration",
"regexp": "",
"visible": true,
"possible_values": [
  {
    "value": "2",
    "label": "Planned" 
  },
  {
    "value": "3",
    "label": "In Progress" 
  },
  {
    "value": "4",
    "label": "Complete" 
  }
]

I did this in a plugin by overriding app/views/custom_fields/index.api.rsb and adding and if block checking for EnumerationFormat.

      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
              if field.format.class.name.demodulize == 'EnumerationFormat'
                  api.label label
              end
            end
          end
        end
      end

I was going to just put the 'api.label label' line in without the if statement but it also takes effect for list types where label and value appear to be the same. There is almost certainly a better way to do this.


Related issues

Related to Redmine - Feature #26017: Get also labels for key/value custom fields by API New

Associated revisions

Revision 15401
Added by Jean-Philippe Lang over 2 years ago

Include value and label of possible values in custom fields API (#22745).

History

#1 Updated by Jean-Philippe Lang over 2 years ago

  • Target version set to 3.2.3

#2 Updated by Jean-Philippe Lang over 2 years ago

  • Status changed from New to Closed
  • Assignee set to Jean-Philippe Lang
  • Target version changed from 3.2.3 to 3.3.0
  • Resolution set to Fixed

Stephen Stuart wrote:

I was going to just put the 'api.label label' line in without the if statement but it also takes effect for list types where label and value appear to be the same.

I've added the label for all custom fields that have possible values
It's better to have the same behaviour no matter the custom field type, even if value and label are the same.
Thanks for pointing this out.

#3 Updated by Maxim KruĊĦina over 1 year ago

  • Related to Feature #26017: Get also labels for key/value custom fields by API added

Also available in: Atom PDF