Skip to content

Commit

Permalink
fix: Precedence of produces keywords for Swagger 2.0 schemas (#464)
Browse files Browse the repository at this point in the history
  • Loading branch information
Stranger6667 committed Apr 3, 2020
1 parent b1d937c commit 1822eff
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
7 changes: 7 additions & 0 deletions docs/changelog.rst
Expand Up @@ -6,6 +6,12 @@ Changelog
`Unreleased`_
-------------

Fixed
~~~~~

- Precedence of ``produces`` keywords for Swagger 2.0 schemas. Now, operation-level ``produces`` overrides
schema-level ``produces`` as specified in the specification. `#463`_

`1.0.2`_ - 2020-04-02
---------------------

Expand Down Expand Up @@ -829,6 +835,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

.. _#463: https://github.com/kiwicom/schemathesis/issues/463
.. _#457: https://github.com/kiwicom/schemathesis/issues/457
.. _#451: https://github.com/kiwicom/schemathesis/issues/451
.. _#450: https://github.com/kiwicom/schemathesis/issues/450
Expand Down
5 changes: 2 additions & 3 deletions src/schemathesis/checks.py
Expand Up @@ -45,11 +45,10 @@ def _expand_responses(responses: Dict[Union[str, int], Any]) -> Generator[int, N


def content_type_conformance(response: GenericResponse, case: "Case") -> None:
produces = case.endpoint.definition.get("produces", None)
global_produces = case.endpoint.schema.raw_schema.get("produces", None)
if global_produces:
if global_produces and not produces:
produces = global_produces
else:
produces = case.endpoint.definition.get("produces", None)
if not produces:
return
content_type = response.headers["Content-Type"]
Expand Down
34 changes: 34 additions & 0 deletions test/runner/test_checks.py
Expand Up @@ -3,6 +3,7 @@
import pytest
import requests

import schemathesis
from schemathesis import models
from schemathesis.checks import (
content_type_conformance,
Expand Down Expand Up @@ -49,6 +50,39 @@ def test_content_type_conformance_valid(response, case):
assert content_type_conformance(response, case) is None


@pytest.mark.parametrize("content_type, is_error", (("application/json", False), ("application/xml", True),))
def test_global_produces_override(content_type, is_error):
# When "produces" is specified on the schema level and on the operation level
schema = schemathesis.from_dict(
{
"swagger": "2.0",
"info": {"title": "Sample API", "description": "API description in Markdown.", "version": "1.0.0"},
"host": "api.example.com",
"basePath": "/v1",
"schemes": ["https"],
"produces": ["application/xml"],
"paths": {
"/users": {
"get": {
"summary": "Returns a list of users.",
"description": "Optional extended description in Markdown.",
"produces": ["application/json"],
"responses": {"200": {"description": "OK"}},
}
}
},
}
)
endpoint = schema.endpoints["/v1/users"]["get"]
case = models.Case(endpoint)
response = make_response(content_type=content_type)
if not is_error:
assert content_type_conformance(response, case) is None
else:
with pytest.raises(AssertionError):
content_type_conformance(response, case)


@pytest.mark.parametrize("value", (500, 502))
def test_not_a_server_error(value, swagger_20):
response = make_response()
Expand Down

0 comments on commit 1822eff

Please sign in to comment.