Asana / python-asana

Official Python client library for the Asana API v1
MIT License
303 stars 105 forks source link

Adding a custom field to a project raises exception #188

Closed cwbusacker closed 11 months ago

cwbusacker commented 11 months ago

Using Asana Package version 5.0.0

Trying to add a custom field in the following way:

 project_gid = project["gid"]
 api_response = projects_api.add_custom_field_setting_for_project( {"data": {"custom_field": "1201039880834721"}}, project_gid)

causes the following exception:

  File "/****/create_asana_projects.py", line 106, in command
    api_response = projects_api.add_custom_field_setting_for_project( {"data": {"custom_field": "1201039880834721"}}, project_gid)
  File "/****/lib/python3.10/site-packages/asana/api/projects_api.py", line 55, in add_custom_field_setting_for_project
    (data) = self.add_custom_field_setting_for_project_with_http_info(body, project_gid, **kwargs)  # noqa: E501
  File "/****/lib/python3.10/site-packages/asana/api/projects_api.py", line 144, in add_custom_field_setting_for_project_with_http_info
    (data) = self.api_client.call_api(
  File "/****/lib/python3.10/site-packages/asana/api_client.py", line 336, in call_api
    query_params = [(k, v) for k, v in query_params.items()]
AttributeError: 'list' object has no attribute 'items'

Editing the source code in call_api appears to fix it, but not sure if that could break it elsewhere.

jv-asana commented 11 months ago

Hi @cwbusacker,

Thank you for reporting this issue. Because of this we also discovered that we were missing a definition for the opt_fields query params the documentation for Add a custom field to a project endpoint in our developer docs.

I have gone ahead and sent a fix for this issue you reported under v5.0.3. Thank you for reporting this and helping make our client library better. If you encounter any other issues please don't hesitate to create another GitHub issue.

Example request: NOTE: since opt_fields was missing from this endpoint and we added it in our docs the method generated now takes in an opts argument. If you don't have any query params to provide just pass in empty dict {}

import asana
from asana.rest import ApiException
from pprint import pprint

configuration = asana.Configuration()
configuration.access_token = "<YOUR_ASANA_PERSONAL_ACCESS_TOKEN>"
api_client = asana.ApiClient(configuration)

# create an instance of the API class
projects_api_instance = asana.ProjectsApi(api_client)
body = {"data": {"custom_field": "1205866071064406"}}
project_gid = "<YOUR_PROJECT_GID>" # str | Globally unique identifier for the project.
opts = {}

try:
    # Add a custom field to a project
    api_response = projects_api_instance.add_custom_field_setting_for_project(body, project_gid, opts)
    pprint(api_response)
except ApiException as e:
    print("Exception when calling ProjectsApi->add_custom_field_setting_for_project: %s\n" % e)

Here's break down of the cause:

  1. add_custom_field_setting_for_project method calls the call_api method with an empty list of query params (NOTE: even though you did not provide query params for this endpoint by default it sends an empty list of query params -> this should be an empty dict)
  2. There's a section of code in the api_client.py file where call_api -> __call_api tries to convert the query param provided from a dict into a list of tuples. Because the default argument for query params provided was a empty list you run into the AttributeError: 'list' object has no attribute 'items' error

TODO to Fix:

  1. Change default query_params in add_custom_field_setting_for_project method from list to dict
  2. Add a check in api_client.py where call_api tries to convert the query param provided from a dict into a list of tuples -> check if it is empty before trying to run this code

Where to perform fix:

Since the new libraries are auto generated we want to modify the templates that the generator uses to generate the the methods and client

  1. To change the default query params from list to dict we'll need to modify the api.mustache template file (this file is used by the generator to generate all the endpoints under this the api folder)
  2. Similarly the client (api_client.py) is also auto generated by the generator from the api_client.mustache template - This is where the conversion is being done and where we need to fix

Commits: