Skip to content

Commit

Permalink
feat: Allow bodies for GET requests if schema validation is disabled
Browse files Browse the repository at this point in the history
Ref: #383
  • Loading branch information
Stranger6667 committed Feb 7, 2020
1 parent 42222a0 commit c4df8d5
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 3 deletions.
7 changes: 7 additions & 0 deletions docs/changelog.rst
Expand Up @@ -6,6 +6,12 @@ Changelog
`Unreleased`_
-------------

Changed
~~~~~~~

- Schemas with GET endpoints accepting body are allowed now if schema validation is disabled (via ``--validate-schema=false`` for example).
The usecase is for tools like ElasticSearch that use GET requests with non empty bodies. `#383`_

`0.23.7`_ - 2020-01-30
----------------------

Expand Down Expand Up @@ -656,6 +662,7 @@ Fixed
.. _0.3.0: https://github.com/kiwicom/schemathesis/compare/v0.2.0...v0.3.0
.. _0.2.0: https://github.com/kiwicom/schemathesis/compare/v0.1.0...v0.2.0

.. _#383: https://github.com/kiwicom/schemathesis/issues/383
.. _#381: https://github.com/kiwicom/schemathesis/issues/381
.. _#378: https://github.com/kiwicom/schemathesis/issues/378
.. _#376: https://github.com/kiwicom/schemathesis/issues/376
Expand Down
2 changes: 1 addition & 1 deletion src/schemathesis/_hypothesis.py
Expand Up @@ -184,7 +184,7 @@ def _get_case_strategy(
endpoint: Endpoint, extra_static_parameters: Dict[str, Any], strategies: Dict[str, st.SearchStrategy]
) -> st.SearchStrategy:
static_parameters: Dict[str, Any] = {"endpoint": endpoint, **extra_static_parameters}
if endpoint.method == "GET":
if endpoint.schema.validate_schema and endpoint.method == "GET":
if endpoint.body is not None:
raise InvalidSchema("Body parameters are defined for GET request.")
static_parameters["body"] = None
Expand Down
18 changes: 16 additions & 2 deletions src/schemathesis/loaders.py
Expand Up @@ -117,13 +117,27 @@ def from_dict(
if "swagger" in raw_schema:
_maybe_validate_schema(raw_schema, spec_schemas.SWAGGER_20, validate_schema)
return SwaggerV20(
raw_schema, location=location, base_url=base_url, method=method, endpoint=endpoint, tag=tag, app=app
raw_schema,
location=location,
base_url=base_url,
method=method,
endpoint=endpoint,
tag=tag,
app=app,
validate_schema=validate_schema,
)

if "openapi" in raw_schema:
_maybe_validate_schema(raw_schema, spec_schemas.OPENAPI_30, validate_schema)
return OpenApi30(
raw_schema, location=location, base_url=base_url, method=method, endpoint=endpoint, tag=tag, app=app
raw_schema,
location=location,
base_url=base_url,
method=method,
endpoint=endpoint,
tag=tag,
app=app,
validate_schema=validate_schema,
)
raise ValueError("Unsupported schema type")

Expand Down
1 change: 1 addition & 0 deletions src/schemathesis/schemas.py
Expand Up @@ -47,6 +47,7 @@ class BaseSchema(Mapping):
tag: Optional[Filter] = attr.ib(default=None) # pragma: no mutate
app: Any = attr.ib(default=None) # pragma: no mutate
hooks: Dict[HookLocation, Hook] = attr.ib(factory=dict) # pragma: no mutate
validate_schema: bool = attr.ib(default=True) # pragma: no mutate

def __iter__(self) -> Iterator[str]:
return iter(self.endpoints)
Expand Down
20 changes: 20 additions & 0 deletions test/test_hypothesis.py
Expand Up @@ -3,6 +3,7 @@
import pytest
from hypothesis import HealthCheck, given, settings, strategies

import schemathesis
from schemathesis import Case, register_string_format
from schemathesis._hypothesis import PARAMETERS, get_case_strategy, get_examples, is_valid_query
from schemathesis.exceptions import InvalidSchema
Expand Down Expand Up @@ -60,6 +61,25 @@ def test_invalid_body_in_get(swagger_20):
get_case_strategy(endpoint)


def test_invalid_body_in_get_disable_validation(simple_schema):
schema = schemathesis.from_dict(simple_schema, validate_schema=False)
endpoint = Endpoint(
path="/foo",
method="GET",
definition={},
schema=schema,
body={"required": ["foo"], "type": "object", "properties": {"foo": {"type": "string"}}},
)
strategy = get_case_strategy(endpoint)

@given(strategy)
@settings(max_examples=1)
def test(case):
assert case.body is not None

test()


def test_warning(swagger_20):
example = {"name": "John"}
endpoint = make_endpoint(swagger_20, query={"example": example})
Expand Down

0 comments on commit c4df8d5

Please sign in to comment.