DaveTCode / confluence-python-lib

A python library wrapping the Confluence REST API
MIT License
40 stars 19 forks source link

KeyError: 'title' on confluence.client.Confluence.get_content() #22

Open justquick opened 5 years ago

justquick commented 5 years ago

When paginating through the confluence.client.Confluence.get_content() method I am running into a KeyError when trying to fetch the title field from a content response. The rest of the fields in content appear on that record and the rest of my application works just fine except for this one bad item. I feel like title should be a required field or at least be a blank string but now it just blows up in my face. I am running Confluence version 6.15.9

Traceback (most recent call last):
  File "/Users/jquick/.virtualenvs/matrix-7HUYJ2hm/lib/python3.6/site-packages/celery/app/trace.py", line 385, in trace_task
    R = retval = fun(*args, **kwargs)
  File "/Users/jquick/.virtualenvs/matrix-7HUYJ2hm/lib/python3.6/site-packages/celery/app/trace.py", line 648, in __protected_call__
    return self.run(*args, **kwargs)
  File "/Users/jquick/Box/Projects/matrix/confluence_app/tasks.py", line 23, in confluence_spaces
    confluence_space_content(space.key)
  File "/Users/jquick/.virtualenvs/matrix-7HUYJ2hm/lib/python3.6/site-packages/celery/local.py", line 191, in __call__
    return self._get_current_object()(*a, **kw)
  File "/Users/jquick/.virtualenvs/matrix-7HUYJ2hm/lib/python3.6/site-packages/celery/app/trace.py", line 649, in __protected_call__
    return orig(self, *args, **kwargs)
  File "/Users/jquick/.virtualenvs/matrix-7HUYJ2hm/lib/python3.6/site-packages/celery/app/task.py", line 394, in __call__
    return self.run(*args, **kwargs)
  File "/Users/jquick/Box/Projects/matrix/confluence_app/tasks.py", line 31, in confluence_space_content
    for content in client.get_content(ctype, space_key, expand=['history']):
  File "/Users/jquick/.virtualenvs/matrix-7HUYJ2hm/lib/python3.6/site-packages/confluence/client.py", line 139, in _get_paged_results
    yield item_type(result)
  File "/Users/jquick/.virtualenvs/matrix-7HUYJ2hm/lib/python3.6/site-packages/confluence/models/content.py", line 69, in __init__
    self.title = json['title']  # type: str
KeyError: 'title'

The site I am pulling this from is private but here is response content (with some info scrubbed for privacy)

{
    "_expandable": {
        "ancestors": "",
        "body": "",
        "children": "/rest/api/content/12345/child",
        "container": "/rest/api/space/webDomain",
        "descendants": "/rest/api/content/12345/descendant",
        "metadata": "",
        "operations": "",
        "restrictions": "/rest/api/content/12345/restriction/byOperation",
        "space": "/rest/api/space/webDomain",
        "version": ""
    },
    "_links": {
        "edit": "/pages/resumedraft.action?draftId=12345",
        "self": "https://confluence.internal.com/rest/api/content/12345",
        "tinyui": "/x/XXXXX",
        "webui": "/pages/viewpage.action?pageId=12345"
    },
    "extensions": {
        "position": "none"
    },
    "history": {
        "_expandable": {
            "contributors": "",
            "lastUpdated": "",
            "nextVersion": "",
            "previousVersion": ""
        },
        "_links": {
            "self": "https://confluence.internal.com/rest/api/content/12345/history"
        },
        "createdBy": {
            "_expandable": {
                "status": ""
            },
            "_links": {
                "self": "https://confluence.internal.com/rest/api/user?key=0987654321"
            },
            "displayName": "Justin Quick",
            "profilePicture": {
                "height": 48,
                "isDefault": false,
                "path": "/download/attachments/555/user-jquick-ldap-image.png",
                "width": 48
            },
            "type": "known",
            "userKey": "0987654321",
            "username": "jquick"
        },
        "createdDate": "2013-01-10T12:08:49.000-05:00",
        "latest": true
    },
    "id": "12345",
    "status": "current",
    "type": "page"
}
DaveTCode commented 5 years ago

Presumably it's somehow possible to have a page with no title in confluence?? Certainly wasn't expecting that when I wrote it.

I've pushed a change but will need to fix the broken travis build environment before I can release.

Since it's annoyingly an API change this will come as 2.0.0 despite it being basically a one line fix.

justquick commented 5 years ago

LGTM, thats exactly what I did locally to fix the issue too.

justquick commented 5 years ago

Got another problem, since self.title doesnt exist then line 97, in str will break. Suggest setting self.title = 'Untitled' as a default before inspecting json for a title