pycontribs / jira

Python Jira library. Development chat available on https://matrix.to/#/#pycontribs:matrix.org
https://jira.readthedocs.io
BSD 2-Clause "Simplified" License
1.91k stars 856 forks source link

create_issue_link crashes when passing named argument type as IssueLinkType #1835

Open jojochims opened 4 months ago

jojochims commented 4 months ago

Bug summary

The create_issue_link function crashes when an IssueLinkType object is passed to the function as the link type.

There has been a similar previous issue #1604 which was fixed by converting the IssueLinkType to a string when calling the create_issue_link function using positional arguments. In this case, the create_issue_link function displays a warning and figures out the correct IssueLinkType on its own.

If an IssueLinkType is handed to create_issue_link using named arguments, the automatic conversion of the arguments is skipped (as it was added in #1604). The warning in create_issue_link is not displayed as the provided link type is found in the list of valid link types. In the next step, the IssueLinkType is however passed directly to the post function and thus creates a TypeError: Object of type IssueLinkType is not JSON serializable

Is there an existing issue for this?

Jira Instance type

Jira Server or Data Center (Self-hosted)

Jira instance version

9.4.15

jira-python version

3.6.0

Python Interpreter version

3.11

Which operating systems have you used?

Reproduction steps

jira: JIRA

jira_link_type = jira.issue_link_types()
# take any valid link type
link_type = link_types[0]

issue1 = "key1"
issue2 = "key2"

# link the issues
jira.create_issue_link(
     type=link_type, 
     inwardIssue = issue1,
     outwardIssue = issue2 
)

Stack trace

(...)
    (...)jira.create_issue_link(
  File "(...)\client.py", line 124, in wrapper
    result = func(*arg_list, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "(...)\client.py", line 2548, in create_issue_link
    return self._session.post(url, data=json.dumps(data))
                                        ^^^^^^^^^^^^^^^^
  File "(...)\json\__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "(...)\json\encoder.py", line 200, in encode
    chunks = self.iterencode(o, _one_shot=True)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "(...)\json\encoder.py", line 258, in iterencode
    return _iterencode(o, 0)
           ^^^^^^^^^^^^^^^^^
  File "(...)\json\encoder.py", line 180, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type IssueLinkType is not JSON serializable

Expected behaviour

In the create_issue_link function the type of the variable "type" needs to be checked before creating the data Dictionary. If it is of type IssueLinkType, the data Dictionary needs to be handed the name of the link type instead of the IssueLinkType object, e.g.:

data = {
    "type": {"name": type.name},
    "inwardIssue": {"key": inwardIssue},
    "outwardIssue": {"key": outwardIssue},
    "comment": comment,
}

Additional Context

No response