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

Support for additionalProperties #63

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ openapi3.egg-info/*
build/*
dist/*
.idea
/.eggs/
10 changes: 6 additions & 4 deletions openapi3/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def get_type(self):
self._model_type = type(
type_name,
(Model,),
{"__slots__": self.properties.keys()}, # pylint: disable=attribute-defined-outside-init
({} if self.additionalProperties else {"__slots__": self.properties.keys()}), # pylint: disable=attribute-defined-outside-init
)

return self._model_type
Expand Down Expand Up @@ -278,14 +278,16 @@ def __init__(self, data, schema):
setattr(self, s, None)

keys = set(data.keys()) - frozenset(self.__slots__)
if keys:
if keys and not schema.additionalProperties:
raise ModelError("Schema {} got unexpected attribute keys {}".format(self.__class__.__name__, keys))

# collect the data into this model
for k, v in data.items():
prop = schema.properties[k]
prop = schema.properties.get(k) if schema.properties else None

if prop.type == "array":
if (not prop) and schema.additionalProperties:
setattr(self, k, v)
elif prop.type == "array":
# handle arrays
item_schema = prop.items
setattr(self, k, [item_schema.model(c) for c in v])
Expand Down
15 changes: 15 additions & 0 deletions tests/additional_properties_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import pytest

from unittest.mock import patch, MagicMock


def test_additional_properties(additional_properties_spec):
resp = MagicMock(status_code=200, headers={"Content-Type": "application/json"}, json=lambda: {"foo": "bar", "additional": "property"})
with patch("requests.sessions.Session.send", return_value=resp) as s:
additional_properties_spec.call_test_operation1()


def test_only_additional_properties(additional_properties_spec):
resp = MagicMock(status_code=200, headers={"Content-Type": "application/json"}, json=lambda: {"additional": "property"})
with patch("requests.sessions.Session.send", return_value=resp) as s:
additional_properties_spec.call_test_operation2()
7 changes: 7 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,10 @@ def with_ref_allof():
an allOf
"""
yield _get_parsed_yaml("ref-allof.yaml")

@pytest.fixture
def additional_properties_spec():
"""
Provides an OpenAPI version of the additional-properties.yaml spec
"""
yield _get_parsed_spec("additional-properties.yaml")
34 changes: 34 additions & 0 deletions tests/fixtures/additional-properties.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
openapi: "3.0.0"
info:
version: 1.0.0
title: This uses additionalProperties
servers:
- url: http://test.example/api
paths:
/additional_prop:
get:
operationId: test_operation1
responses:
"200":
description: Success!
content:
application/json:
schema:
type: object
properties:
foo:
type: string
additionalProperties:
type: string
/only_additional_prop2:
get:
operationId: test_operation2
responses:
"200":
description: Success!
content:
application/json:
schema:
type: object
additionalProperties:
type: string