FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Array parameter default regression with SchemaPath by p1c2u · Pull Request #1158 · python-openapi/openapi-core · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (8) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def decode(
except KeyError:
if "default" not in prop_schema:
continue
properties[prop_name] = prop_schema["default"]
properties[prop_name] = (prop_schema / "default").read_value()

if schema_only:
return properties
Expand Down
7 changes: 4 additions & 3 deletions openapi_core/schema/servers.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ def get_server_default_variables(server: SchemaPath) -> Dict[str, Any]:
defaults = {}
variables = server / "variables"
for name, variable in list(variables.str_items()):
defaults[name] = variable["default"]
defaults[name] = (variable / "default").read_value()
return defaults


def get_server_url(server: SchemaPath, **variables: Any) -> str:
if not variables:
variables = get_server_default_variables(server)
assert isinstance(server["url"], str)
return server["url"].format(**variables)
url = (server / "url").read_value()
assert isinstance(url, str)
return url.format(**variables)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def _unmarshal_properties(
except KeyError:
if "default" not in prop_schema:
continue
prop_value = prop_schema["default"]
prop_value = (prop_schema / "default").read_value()

properties[prop_name] = self.schema_unmarshaller.evolve(
prop_schema
Expand Down
2 changes: 1 addition & 1 deletion openapi_core/validation/validators.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def _get_simple_param_or_header(
except KeyError:
if "default" not in schema:
raise
return schema["default"], schema
return (schema / "default").read_value(), schema
if allow_empty_values is not None:
warnings.warn(
"Use of allowEmptyValue property is deprecated",
Expand Down
16 changes: 16 additions & 0 deletions tests/integration/schema/test_spec.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest
from jsonschema_path import SchemaPath

from openapi_core.schema.servers import get_server_default_variables
from openapi_core.schema.servers import get_server_url
from openapi_core.schema.specs import get_spec_url

Expand Down Expand Up @@ -352,3 +353,18 @@ def test_spec(self, schema_path, spec_dict):
schemas = components.get("schemas", {})
for schema_name, schema in schemas.items():
assert spec_dict["components"]["schemas"][schema_name] is not None


def test_get_server_default_variables():
server_spec = {
"url": "https://{host}.example.com:{port}/v1",
"variables": {
"host": {"default": "api"},
"port": {"default": "8080"},
},
}
server = SchemaPath.from_dict(server_spec)

defaults = get_server_default_variables(server)

assert defaults == {"host": "api", "port": "8080"}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -423,3 +423,47 @@ def test_get_pet(self, request_unmarshaller):
assert result.security == {
"petstore_auth": self.api_key_encoded,
}

def test_request_body_with_object_default(self):
from openapi_core import OpenAPI

spec = OpenAPI.from_dict(
{
"openapi": "3.1.0",
"info": {"version": "0", "title": "test"},
"paths": {
"/test": {
"post": {
"requestBody": {
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"tags": {
"type": "array",
"default": [],
}
},
}
}
}
},
"responses": {"200": {"description": ""}},
},
}
},
}
)
request = MockRequest(
"http://localhost",
"post",
"/test",
content_type="application/json",
data=b"{}",
)

result = spec.unmarshal_request(request)

assert result.errors == []
assert result.body == {"tags": []}
57 changes: 57 additions & 0 deletions tests/integration/validation/test_request_validators.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,60 @@ def test_valid(self, request_validator):
result = request_validator.validate(request)

assert result is None

def test_array_parameter_with_empty_default(self):
spec = OpenAPI.from_dict(
{
"openapi": "3.1.0",
"info": {"version": "0", "title": "test"},
"paths": {
"/test": {
"get": {
"parameters": [
{
"name": "foo",
"in": "query",
"schema": {"type": "array", "default": []},
}
],
"responses": {"200": {"description": ""}},
},
}
},
}
)
request = MockRequest("http://localhost", "get", "/test")

result = spec.validate_request(request)

assert result is None

def test_array_parameter_with_populated_default(self):
spec = OpenAPI.from_dict(
{
"openapi": "3.1.0",
"info": {"version": "0", "title": "test"},
"paths": {
"/test": {
"get": {
"parameters": [
{
"name": "foo",
"in": "query",
"schema": {
"type": "array",
"default": ["a", "b", "c"],
},
}
],
"responses": {"200": {"description": ""}},
},
}
},
}
)
request = MockRequest("http://localhost", "get", "/test")

result = spec.validate_request(request)

assert result is None
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -637,3 +637,22 @@ def test_urlencoded_oneof_boolean_field(self, spec, deserializer_factory):
"enabled": True,
"mode": "auto",
}

def test_urlencoded_form_with_array_default(self, deserializer_factory):
mimetype = "application/x-www-form-urlencoded"
schema_dict = {
"type": "object",
"properties": {
"tags": {
"type": "array",
"default": [],
},
},
}
schema = SchemaPath.from_dict(schema_dict)
deserializer = deserializer_factory(mimetype, schema=schema)
value = b""

result = deserializer.deserialize(value)

assert result == {"tags": []}
Loading

Back | FazBrowse Home | New Git URL