Skip to content

Commit

Permalink
fix: Unescaped quotes in generated Python code samples on some schemas
Browse files Browse the repository at this point in the history
Ref: #1030
  • Loading branch information
Stranger6667 committed Nov 2, 2021
1 parent 239315a commit 0ad3437
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Changelog

- Pass ``data_generation_method`` to ``Case`` for GraphQL schemas.
- Generation of invalid headers in some cases. `#1142`_
- Unescaped quotes in generated Python code samples on some schemas. `#1030`_

**Performance**

Expand Down Expand Up @@ -2209,6 +2210,7 @@ Deprecated
.. _#1039: https://github.com/schemathesis/schemathesis/issues/1039
.. _#1036: https://github.com/schemathesis/schemathesis/issues/1036
.. _#1033: https://github.com/schemathesis/schemathesis/issues/1033
.. _#1030: https://github.com/schemathesis/schemathesis/issues/1030
.. _#1028: https://github.com/schemathesis/schemathesis/issues/1028
.. _#1022: https://github.com/schemathesis/schemathesis/issues/1022
.. _#1020: https://github.com/schemathesis/schemathesis/issues/1020
Expand Down
3 changes: 2 additions & 1 deletion src/schemathesis/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,8 @@ def should_display(key: str, value: Any) -> bool:
printed_kwargs = ", ".join(
f"{key}={repr(value)}" for key, value in kwargs.items() if should_display(key, value)
)
args_repr = f"'{kwargs['url']}'"
url = kwargs["url"].replace("'", "\\'")
args_repr = f"'{url}'"
if printed_kwargs:
args_repr += f", {printed_kwargs}"
return f"requests.{method}({args_repr})"
Expand Down
28 changes: 28 additions & 0 deletions test/code_samples/test_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
import pytest
import requests
from flask import Flask
from hypothesis import HealthCheck, given, settings

import schemathesis
from schemathesis.constants import USER_AGENT
from schemathesis.runner import from_schema

Expand Down Expand Up @@ -61,6 +63,32 @@ def test_code_sample_from_request(openapi_case):
assert openapi_case.get_code_to_reproduce(request=request) == f"requests.get('{url}')"


@pytest.mark.hypothesis_nested
def test_get_code_sample_code_validity(empty_open_api_2_schema):
# See GH-1030
# When the input schema is too loose
empty_open_api_2_schema["paths"] = {
"/test/{key}": {
"post": {
"parameters": [{"name": "key", "in": "path"}],
"responses": {"default": {"description": "OK"}},
}
}
}
schema = schemathesis.from_dict(empty_open_api_2_schema, base_url="http://127.0.0.1:1", validate_schema=False)
strategy = schema["/test/{key}"]["POST"].as_strategy()

@given(case=strategy)
@settings(max_examples=30, suppress_health_check=[HealthCheck.too_slow, HealthCheck.filter_too_much], deadline=None)
def test(case):
code = case.get_code_to_reproduce()
# Then generated code should always be syntactically valid
with pytest.raises(requests.exceptions.ConnectionError):
eval(code)

test()


@pytest.mark.filterwarnings("ignore:.*method is good for exploring strategies.*")
def test_graphql_code_sample(graphql_url, graphql_schema, graphql_strategy):
case = graphql_strategy.example()
Expand Down

0 comments on commit 0ad3437

Please sign in to comment.