Skip to content

Commit

Permalink
Merge pull request #209 from lust4life/master
Browse files Browse the repository at this point in the history
support passing pydantic original model as request body
  • Loading branch information
prkumar committed Nov 22, 2020
2 parents 54839a0 + 44d7dcc commit 47715bc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
18 changes: 18 additions & 0 deletions tests/unit/test_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,24 @@ class ComplexModel(pydantic.BaseModel):

assert result == expected_result

def test_create_request_body_converter_with_original_model(
self, pydantic_model_mock
):
expected_result = {"id": 0}

model_mock, model = pydantic_model_mock
model_mock.dict.return_value = expected_result

request_body = model()

converter = converters.PydanticConverter()
request_converter = converter.create_request_body_converter(model)

result = request_converter.convert(request_body)

assert result == expected_result
model_mock.dict.assert_called_once()

def test_create_request_body_converter_without_schema(self, mocker):
expected_result = None
converter = converters.PydanticConverter()
Expand Down
2 changes: 2 additions & 0 deletions uplink/converters/pydantic_.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ def __init__(self, model):
self._model = model

def convert(self, value):
if isinstance(value, self._model):
return _encode_pydantic(value)
return _encode_pydantic(self._model.parse_obj(value))


Expand Down

0 comments on commit 47715bc

Please sign in to comment.