Skip to content

Commit

Permalink
perf: Avoid copy.deepcopy calls in some cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Stranger6667 committed Oct 31, 2021
1 parent 94b642d commit 722ed8a
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
7 changes: 4 additions & 3 deletions src/schemathesis/specs/openapi/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
from ...utils import traverse_schema


def to_json_schema(schema: Dict[str, Any], nullable_name: str) -> Dict[str, Any]:
def to_json_schema(schema: Dict[str, Any], *, nullable_name: str, copy: bool = True) -> Dict[str, Any]:
"""Convert Open API parameters to JSON Schema.
NOTE. This function is applied to all keywords (including nested) during a schema resolving, thus it is not recursive.
See a recursive version below.
"""
schema = deepcopy(schema)
if copy:
schema = deepcopy(schema)
if schema.get(nullable_name) is True:
del schema[nullable_name]
schema = {"anyOf": [schema, {"type": "null"}]}
Expand All @@ -21,4 +22,4 @@ def to_json_schema(schema: Dict[str, Any], nullable_name: str) -> Dict[str, Any]


def to_json_schema_recursive(schema: Dict[str, Any], nullable_name: str) -> Dict[str, Any]:
return traverse_schema(schema, to_json_schema, nullable_name)
return traverse_schema(schema, to_json_schema, nullable_name=nullable_name)
2 changes: 1 addition & 1 deletion src/schemathesis/specs/openapi/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ def _rewritten_components(self) -> Dict[str, Any]:
if not hasattr(self, "__rewritten_components"):

def callback(_schema: Dict[str, Any], nullable_name: str) -> Dict[str, Any]:
_schema = to_json_schema(_schema, nullable_name)
_schema = to_json_schema(_schema, nullable_name=nullable_name, copy=False)
return self._rewrite_references(_schema, self.resolver)

# pylint: disable=attribute-defined-outside-init
Expand Down
2 changes: 1 addition & 1 deletion test/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@
),
)
def test_to_jsonschema_recursive(schema, expected):
assert traverse_schema(schema, converter.to_json_schema, "x-nullable") == expected
assert traverse_schema(schema, converter.to_json_schema, nullable_name="x-nullable") == expected

0 comments on commit 722ed8a

Please sign in to comment.