Skip to content

Fix the json generation of SomethingLike #36

@jeanbaptistepriez

Description

@jeanbaptistepriez

Hello,

There is a small issue (on pact-python==0.6.2) when we try to generate those kind of interactions:

SomethingLike({
   'username': Term('.+', 'admin'),
   'email': ''
})

This raises :

Finding files... done.
Importing test modules ... [2017-07-12 17:43:58] INFO  WEBrick 1.3.1
[2017-07-12 17:43:58] INFO  ruby 2.2.2 (2015-04-13) [x86_64-darwin13]
[2017-07-12 17:43:58] INFO  WEBrick::HTTPServer#start: pid=14167 port=1234
done.

======================================================================
ERROR: test_sign_in (__init__.UserTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/EliX/git/test/api/__init__.py", line 35, in test_sign_in
    with pact:
  File "/Users/EliX/anaconda/lib/python2.7/site-packages/pact/pact.py", line 238, in __enter__
    self.setup()
  File "/Users/EliX/anaconda/lib/python2.7/site-packages/pact/pact.py", line 116, in setup
    json={"interactions": self._interactions})
  File "/Users/EliX/anaconda/lib/python2.7/site-packages/requests/api.py", line 124, in put
    return request('put', url, data=data, **kwargs)
  File "/Users/EliX/anaconda/lib/python2.7/site-packages/requests/api.py", line 56, in request
    return session.request(method=method, url=url, **kwargs)
  File "/Users/EliX/anaconda/lib/python2.7/site-packages/requests/sessions.py", line 474, in request
    prep = self.prepare_request(req)
  File "/Users/EliX/anaconda/lib/python2.7/site-packages/requests/sessions.py", line 407, in prepare_request
    hooks=merge_hooks(request.hooks, self.hooks),
  File "/Users/EliX/anaconda/lib/python2.7/site-packages/requests/models.py", line 305, in prepare
    self.prepare_body(data, files, json)
  File "/Users/EliX/anaconda/lib/python2.7/site-packages/requests/models.py", line 445, in prepare_body
    body = complexjson.dumps(json)
  File "/Users/EliX/anaconda/lib/python2.7/json/__init__.py", line 244, in dumps
    return _default_encoder.encode(obj)
  File "/Users/EliX/anaconda/lib/python2.7/json/encoder.py", line 207, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/Users/EliX/anaconda/lib/python2.7/json/encoder.py", line 270, in iterencode
    return _iterencode(o, 0)
  File "/Users/EliX/anaconda/lib/python2.7/json/encoder.py", line 184, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: <pact.matchers.Term object at 0x105931a90> is not JSON serializable

The problem is in file pact/matchers.py on line 120:

return {
  'json_class': 'Pact::SomethingLike',
  'contents': self.matcher}

https://github.com/pact-foundation/pact-python/blob/master/pact/matchers.py#L120

It should be

return {
  'json_class': 'Pact::SomethingLike',
  'contents': from_term(self.matcher)}

This fix should manage when self.matcher is a python dictionary.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions