Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect minproperties and maxproperties #15

Merged
merged 2 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/OpenApiDriver/openapi_executors.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,7 @@ def test_endpoint(self, path: str, method: str, status_code: int) -> None:
params = request_data.get_required_params()
headers = request_data.get_required_headers()
json_data = (
request_data.get_required_properties_dict()
if request_data.has_body
else None
request_data.get_minimal_body_dict() if request_data.has_body else None
)
original_data = None
if method == "PATCH":
Expand Down
2 changes: 1 addition & 1 deletion src/OpenApiLibCore/dto_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def merge_schemas(first: Dict[str, Any], second: Dict[str, Any]) -> Dict[str, An
# if the key holds a list, extend the values (e.g. 'required')
merged_schema[key].extend(value)
else:
logger.warning(
logger.debug(
f"key '{key}' with value '{merged_schema[key]}' not "
f"updated to '{value}'"
)
Expand Down
53 changes: 49 additions & 4 deletions src/OpenApiLibCore/openapi_libcore.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@
from itertools import zip_longest
from logging import getLogger
from pathlib import Path
from random import choice
from random import choice, sample
from typing import Any, Dict, List, Optional, Set, Tuple, Type, Union
from uuid import uuid4

Expand Down Expand Up @@ -358,6 +358,33 @@ def get_required_properties_dict(self) -> Dict[str, Any]:
required_properties_dict[key] = value
return required_properties_dict

def get_minimal_body_dict(self) -> Dict[str, Any]:
required_properties_dict = self.get_required_properties_dict()

min_properties = self.dto_schema.get("minProperties", 0)
number_of_optional_properties_to_add = min_properties - len(
required_properties_dict
)

if number_of_optional_properties_to_add < 1:
return required_properties_dict

optional_properties_dict = {
k: v
for k, v in self.dto.as_dict().items()
if k not in required_properties_dict
}
optional_properties_to_keep = sample(
sorted(optional_properties_dict), number_of_optional_properties_to_add
)
optional_properties_dict = {
k: v
for k, v in optional_properties_dict.items()
if k in optional_properties_to_keep
}

return {**required_properties_dict, **optional_properties_dict}

def get_required_params(self) -> Dict[str, str]:
"""Get the params dict containing only the required query parameters."""
required_parameters = [
Expand Down Expand Up @@ -1083,7 +1110,27 @@ def get_dependent_id(

json_data: Dict[str, Any] = {}

property_names = []
for property_name in schema.get("properties", []):
if constrained_values := get_constrained_values(property_name):
# do not add properties that are configured to be ignored
if IGNORE in constrained_values:
continue
property_names.append(property_name)

max_properties = schema.get("maxProperties")
if max_properties and len(property_names) > max_properties:
required_properties = schema.get("required", [])
number_of_optional_properties = max_properties - len(required_properties)
optional_properties = [
name for name in property_names if name not in required_properties
]
selected_optional_properties = sample(
optional_properties, number_of_optional_properties
)
property_names = required_properties + selected_optional_properties

for property_name in property_names:
properties_schema = schema["properties"][property_name]

property_type = properties_schema.get("type")
Expand All @@ -1102,9 +1149,6 @@ def get_dependent_id(
if properties_schema.get("readOnly", False):
continue
if constrained_values := get_constrained_values(property_name):
# do not add properties that are configured to be ignored
if IGNORE in constrained_values:
continue
json_data[property_name] = choice(constrained_values)
continue
if (
Expand All @@ -1131,6 +1175,7 @@ def get_dependent_id(
json_data[property_name] = [array_data]
continue
json_data[property_name] = value_utils.get_valid_value(properties_schema)

return json_data

@keyword
Expand Down
Loading