Skip to content

Commit

Permalink
fix: Internal error during verifying explicit examples if an example …
Browse files Browse the repository at this point in the history
…has no `value` key

Ref: #882
  • Loading branch information
Stranger6667 committed Nov 24, 2020
1 parent f5f8243 commit 2226a60
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
5 changes: 5 additions & 0 deletions docs/changelog.rst
Expand Up @@ -12,6 +12,10 @@ Changelog

- The ``content_type_conformance`` check now raises a well-formed error message when encounters a malformed media type value. `#877`_

**Fixed**

- Internal error during verifying explicit examples if an example has no ``value`` key. `#882`_

`2.8.0`_ - 2020-11-24
---------------------

Expand Down Expand Up @@ -1534,6 +1538,7 @@ Deprecated
.. _0.3.0: https://github.com/schemathesis/schemathesis/compare/v0.2.0...v0.3.0
.. _0.2.0: https://github.com/schemathesis/schemathesis/compare/v0.1.0...v0.2.0

.. _#882: https://github.com/schemathesis/schemathesis/issues/882
.. _#877: https://github.com/schemathesis/schemathesis/issues/877
.. _#876: https://github.com/schemathesis/schemathesis/issues/876
.. _#874: https://github.com/schemathesis/schemathesis/issues/874
Expand Down
4 changes: 2 additions & 2 deletions src/schemathesis/specs/openapi/examples.py
Expand Up @@ -21,7 +21,7 @@ def get_parameter_examples(endpoint_def: Dict[str, Any], examples_field: str) ->
{
"type": LOCATION_TO_CONTAINER.get(parameter["in"]),
"name": parameter["name"],
"examples": [example["value"] for example in parameter[examples_field].values()],
"examples": [example["value"] for example in parameter[examples_field].values() if "value" in example],
}
for parameter in endpoint_def.get("parameters", [])
if examples_field in parameter
Expand Down Expand Up @@ -55,7 +55,7 @@ def get_request_body_examples(endpoint_def: Dict[str, Any], examples_field: str)
parameter_type = "body" if media_type != "multipart/form-data" else "form_data"
return {
"type": parameter_type,
"examples": [example["value"] for example in schema.get(examples_field, {}).values()],
"examples": [example["value"] for example in schema.get(examples_field, {}).values() if "value" in example],
}


Expand Down
46 changes: 46 additions & 0 deletions test/specs/openapi/test_examples.py
Expand Up @@ -343,3 +343,49 @@ def test_static_parameters_union_0():
assert len(full_sp) == 1
assert "header1" in full_sp1["headers"] and full_sp1["headers"]["header1"] == "example1 string"
assert full_sp1["body"] == {"foo1": "example1 string"}


EXAMPLE_SCHEMA = {
"openapi": "3.0.0",
"info": {"title": "Sample API", "description": "API description in Markdown.", "version": "1.0.0"},
"paths": {
"/test": {
"post": {
"parameters": [
{
"in": "query",
"name": "id",
"schema": {"type": "string"},
"examples": {"foo": {"externalValue": "http://example.com/examples/example.pdf"}},
}
],
"requestBody": {
"required": True,
"content": {
"application/json": {
"schema": {
"type": "object",
},
"examples": {"foo": {"externalValue": "http://example.com/examples/example.pdf"}},
}
},
},
"responses": {"default": {"description": "test"}},
}
},
},
}


@pytest.mark.parametrize(
"func, expected",
(
(examples.get_request_body_examples, {"examples": [], "type": "body"}),
(examples.get_parameter_examples, [{"examples": [], "name": "id", "type": "query"}]),
),
)
def test_example_external_value_failure(func, expected):
# See GH-882
schema = schemathesis.from_dict(EXAMPLE_SCHEMA)
endpoint = schema["/test"]["POST"]
assert func(endpoint.definition.resolved, "examples") == expected

0 comments on commit 2226a60

Please sign in to comment.