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

[14.0] base_rest_pydantic: New addon to use pydantic model into base_rest #221

Merged
merged 2 commits into from
Jun 14, 2022
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
10 changes: 7 additions & 3 deletions base_rest/apispec/rest_method_param_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,14 @@ def _generate_pamareters(self, routing, method, params):
if method == "get":
# get quey params from RequestMethodParam object
parameters.extend(
input_param.to_openapi_query_parameters(self._service)
input_param.to_openapi_query_parameters(self._service, self.spec)
)
else:
# get requestBody from RequestMethodParam object
request_body = params.get("requestBody", {})
request_body.update(input_param.to_openapi_requestbody(self._service))
request_body.update(
input_param.to_openapi_requestbody(self._service, self.spec)
)
params["requestBody"] = request_body
# sort paramters to ease comparison into unittests
parameters.sort(key=lambda a: a["name"])
Expand All @@ -68,5 +70,7 @@ def _generate_responses(self, routing, method, params):
responses = params.get("responses", {})
# get response from RequestMethodParam object
responses.update(self._default_responses.copy())
responses.update(output_param.to_openapi_responses(self._service))
responses.update(
output_param.to_openapi_responses(self._service, self.spec)
)
return responses
18 changes: 9 additions & 9 deletions base_rest/restapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,13 @@ def to_response(self, service, result):
:return: http.Response or JSON dict
"""

def to_openapi_query_parameters(self, service):
def to_openapi_query_parameters(self, service, spec):
return {}

def to_openapi_requestbody(self, service):
def to_openapi_requestbody(self, service, spec):
return {}

def to_openapi_responses(self, service):
def to_openapi_responses(self, service, spec):
return {}


Expand All @@ -161,10 +161,10 @@ def _binary_content_schema(self):
for mediatype in self._mediatypes
}

def to_openapi_requestbody(self, service):
def to_openapi_requestbody(self, services, spec):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have the feeling that services is an error here and should be singular service

return {"content": self._binary_content_schema}

def to_openapi_responses(self, service):
def to_openapi_responses(self, service, spec):
return {"200": {"content": self._binary_content_schema}}

def to_response(self, service, result):
Expand Down Expand Up @@ -209,7 +209,7 @@ def to_response(self, service, result):
return validator.document
raise SystemError(_("Invalid Response %s") % validator.errors)

def to_openapi_query_parameters(self, service):
def to_openapi_query_parameters(self, service, spec):
json_schema = self.to_json_schema(service, "input")
parameters = []
for prop, spec in list(json_schema["properties"].items()):
Expand Down Expand Up @@ -238,11 +238,11 @@ def to_openapi_query_parameters(self, service):

return parameters

def to_openapi_requestbody(self, service):
def to_openapi_requestbody(self, service, spec):
json_schema = self.to_json_schema(service, "input")
return {"content": {"application/json": {"schema": json_schema}}}

def to_openapi_responses(self, service):
def to_openapi_responses(self, service, spec):
json_schema = self.to_json_schema(service, "output")
return {"200": {"content": {"application/json": {"schema": json_schema}}}}

Expand Down Expand Up @@ -293,7 +293,7 @@ def from_params(self, service, params):
def to_response(self, service, result):
return self._do_validate(service, data=result, direction="output")

def to_openapi_query_parameters(self, service):
def to_openapi_query_parameters(self, service, spec):
raise NotImplementedError("List are not (?yet?) supported as query paramters")

def _do_validate(self, service, data, direction):
Expand Down
10 changes: 5 additions & 5 deletions base_rest/tests/test_cerberus_list_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def setUpClass(cls):
cls.maxDiff = None

def test_to_openapi_responses(self):
res = self.simple_schema_list_validator.to_openapi_responses(None)
res = self.simple_schema_list_validator.to_openapi_responses(None, None)
self.assertDictEqual(
res,
{
Expand Down Expand Up @@ -78,7 +78,7 @@ def test_to_openapi_responses(self):
}
},
)
res = self.nested_schema_list_validator.to_openapi_responses(None)
res = self.nested_schema_list_validator.to_openapi_responses(None, None)
self.assertDictEqual(
res,
{
Expand Down Expand Up @@ -113,7 +113,7 @@ def test_to_openapi_responses(self):
)

def test_to_openapi_requestbody(self):
res = self.simple_schema_list_validator.to_openapi_requestbody(None)
res = self.simple_schema_list_validator.to_openapi_requestbody(None, None)
self.assertEqual(
res,
{
Expand Down Expand Up @@ -141,7 +141,7 @@ def test_to_openapi_requestbody(self):
}
},
)
res = self.nested_schema_list_validator.to_openapi_requestbody(None)
res = self.nested_schema_list_validator.to_openapi_requestbody(None, None)
self.assertDictEqual(
res,
{
Expand Down Expand Up @@ -175,7 +175,7 @@ def test_to_openapi_requestbody(self):

def test_to_openapi_query_parameters(self):
with self.assertRaises(NotImplementedError):
self.simple_schema_list_validator.to_openapi_query_parameters(None)
self.simple_schema_list_validator.to_openapi_query_parameters(None, None)

def test_from_params_ignore_unknown(self):
params = [{"name": "test", "unknown": True}]
Expand Down
16 changes: 10 additions & 6 deletions base_rest/tests/test_cerberus_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def setUpClass(cls):
)

def test_to_openapi_responses(self):
res = self.simple_schema_cerberus_validator.to_openapi_responses(None)
res = self.simple_schema_cerberus_validator.to_openapi_responses(None, None)
self.assertDictEqual(
res,
{
Expand Down Expand Up @@ -80,7 +80,7 @@ def test_to_openapi_responses(self):
}
},
)
res = self.nested_schema_cerberus_validator.to_openapi_responses(None)
res = self.nested_schema_cerberus_validator.to_openapi_responses(None, None)
self.assertDictEqual(
res,
{
Expand Down Expand Up @@ -113,7 +113,7 @@ def test_to_openapi_responses(self):
)

def test_to_openapi_requestbody(self):
res = self.simple_schema_cerberus_validator.to_openapi_requestbody(None)
res = self.simple_schema_cerberus_validator.to_openapi_requestbody(None, None)
self.assertEqual(
res,
{
Expand All @@ -140,7 +140,7 @@ def test_to_openapi_requestbody(self):
}
},
)
res = self.nested_schema_cerberus_validator.to_openapi_requestbody(None)
res = self.nested_schema_cerberus_validator.to_openapi_requestbody(None, None)
self.assertDictEqual(
res,
{
Expand Down Expand Up @@ -168,7 +168,9 @@ def test_to_openapi_requestbody(self):
)

def test_to_openapi_query_parameters(self):
res = self.simple_schema_cerberus_validator.to_openapi_query_parameters(None)
res = self.simple_schema_cerberus_validator.to_openapi_query_parameters(
None, None
)
self.assertListEqual(
res,
[
Expand Down Expand Up @@ -206,7 +208,9 @@ def test_to_openapi_query_parameters(self):
},
],
)
res = self.nested_schema_cerberus_validator.to_openapi_query_parameters(None)
res = self.nested_schema_cerberus_validator.to_openapi_query_parameters(
None, None
)
self.assertListEqual(
res,
[
Expand Down
6 changes: 3 additions & 3 deletions base_rest_datamodel/restapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,22 @@ def to_response(self, service, result):
raise SystemError(_("Invalid Response %s") % errors)
return json

def to_openapi_query_parameters(self, service):
def to_openapi_query_parameters(self, service, spec):
converter = self._get_converter()
schema = self._get_schema(service)
return converter.schema2parameters(schema, location="query")

# TODO, we should probably get the spec as parameters. That should
# allows to add the definition of a schema only once into the specs
# and use a reference to the schema into the parameters
def to_openapi_requestbody(self, service):
def to_openapi_requestbody(self, service, spec):
return {
"content": {
"application/json": {"schema": self.to_json_schema(service, "input")}
}
}

def to_openapi_responses(self, service):
def to_openapi_responses(self, service, spec):
return {
"200": {
"content": {
Expand Down
1 change: 1 addition & 0 deletions base_rest_demo/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from . import controllers
from . import datamodels
from . import pydantic_models
from . import services
12 changes: 10 additions & 2 deletions base_rest_demo/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,17 @@
"author": "ACSONE SA/NV, " "Odoo Community Association (OCA)",
"maintainers": ["lmignon"],
"website": "https://github.com/OCA/rest-framework",
"depends": ["base_rest", "base_rest_datamodel", "component"],
"depends": [
"base_rest",
"base_rest_datamodel",
"base_rest_pydantic",
"component",
"extendable",
],
"data": [],
"demo": [],
"external_dependencies": {"python": ["jsondiff"]},
"external_dependencies": {
"python": ["jsondiff", "extendable-pydantic", "pydantic"]
},
"installable": True,
}
Loading