Skip to content

Commit

Permalink
[MIG] base_rest_pydantic: migrate to 18.0
Browse files Browse the repository at this point in the history
  • Loading branch information
pierre-halleux committed Mar 4, 2025
1 parent dc36045 commit 2928646
Show file tree
Hide file tree
Showing 13 changed files with 101 additions and 96 deletions.
71 changes: 35 additions & 36 deletions base_rest_pydantic/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ Base Rest Datamodel
:target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html
:alt: License: LGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Frest--framework-lightgray.png?logo=github
:target: https://github.com/OCA/rest-framework/tree/16.0/base_rest_pydantic
:target: https://github.com/OCA/rest-framework/tree/18.0/base_rest_pydantic
:alt: OCA/rest-framework
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/rest-framework-16-0/rest-framework-16-0-base_rest_pydantic
:target: https://translation.odoo-community.org/projects/rest-framework-18-0/rest-framework-18-0-base_rest_pydantic
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
:target: https://runboat.odoo-community.org/builds?repo=OCA/rest-framework&target_branch=16.0
:target: https://runboat.odoo-community.org/builds?repo=OCA/rest-framework&target_branch=18.0
:alt: Try me on Runboat

|badge1| |badge2| |badge3| |badge4| |badge5|

This addon allows you to use Pydantic objects as params and/or response with your
REST API methods.
This addon allows you to use Pydantic objects as params and/or response
with your REST API methods.

**Table of contents**

Expand All @@ -39,65 +39,64 @@ REST API methods.
Usage
=====

To use Pydantic instances as request and/or response of a REST service endpoint
you must:
To use Pydantic instances as request and/or response of a REST service
endpoint you must:

* Define your Pydantic classes;
* Provides the information required to the ``odoo.addons.base_rest.restapi.method`` decorator;
- Define your Pydantic classes;
- Provides the information required to the
``odoo.addons.base_rest.restapi.method`` decorator;

.. code:: python
.. code-block:: python
from odoo.addons.base_rest import restapi
from odoo.addons.component.core import Component
from odoo.addons.pydantic.models import BaseModel
class PingMessage(BaseModel):
message: str
from odoo.addons.base_rest import restapi
from odoo.addons.component.core import Component
from odoo.addons.pydantic.models import BaseModel
class PingMessage(BaseModel):
message: str
class PingService(Component):
_inherit = 'base.rest.service'
_name = 'ping.service'
_usage = 'ping'
_collection = 'my_module.services'
class PingService(Component):
_inherit = 'base.rest.service'
_name = 'ping.service'
_usage = 'ping'
_collection = 'my_module.services'
@restapi.method(
[(["/pong"], "GET")],
input_param=restapi.PydanticModel(PingMessage),
output_param=restapi.PydanticModel(PingMessage),
auth="public",
)
def pong(self, ping_message):
return PingMessage(message = "Received: " + ping_message.message)
@restapi.method(
[(["/pong"], "GET")],
input_param=restapi.PydanticModel(PingMessage),
output_param=restapi.PydanticModel(PingMessage),
auth="public",
)
def pong(self, ping_message):
return PingMessage(message = "Received: " + ping_message.message)
Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/OCA/rest-framework/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
`feedback <https://github.com/OCA/rest-framework/issues/new?body=module:%20base_rest_pydantic%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
`feedback <https://github.com/OCA/rest-framework/issues/new?body=module:%20base_rest_pydantic%0Aversion:%2018.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Credits
=======

Authors
~~~~~~~
-------

* ACSONE SA/NV

Contributors
~~~~~~~~~~~~
------------

* Laurent Mignon <laurent.mignon@acsone.eu>
- Laurent Mignon <laurent.mignon@acsone.eu>

Maintainers
~~~~~~~~~~~
-----------

This module is maintained by the OCA.

Expand All @@ -109,6 +108,6 @@ OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.

This module is part of the `OCA/rest-framework <https://github.com/OCA/rest-framework/tree/16.0/base_rest_pydantic>`_ project on GitHub.
This module is part of the `OCA/rest-framework <https://github.com/OCA/rest-framework/tree/18.0/base_rest_pydantic>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
3 changes: 1 addition & 2 deletions base_rest_pydantic/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
"name": "Base Rest Datamodel",
"summary": """
Pydantic binding for base_rest""",
"version": "16.0.2.0.1",
"version": "18.0.1.0.0",
"license": "LGPL-3",
"author": "ACSONE SA/NV,Odoo Community Association (OCA)",
"website": "https://github.com/OCA/rest-framework",
"depends": ["base_rest"],
"installable": False,
"external_dependencies": {
"python": [
"pydantic>=2.0.0",
Expand Down
3 changes: 3 additions & 0 deletions base_rest_pydantic/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build-system]
requires = ["whool"]
build-backend = "whool.buildapi"
1 change: 1 addition & 0 deletions base_rest_pydantic/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Laurent Mignon \<laurent.mignon@acsone.eu\>
1 change: 0 additions & 1 deletion base_rest_pydantic/readme/CONTRIBUTORS.rst

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
This addon allows you to use Pydantic objects as params and/or response with your
REST API methods.
This addon allows you to use Pydantic objects as params and/or response
with your REST API methods.
32 changes: 32 additions & 0 deletions base_rest_pydantic/readme/USAGE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
To use Pydantic instances as request and/or response of a REST service
endpoint you must:

- Define your Pydantic classes;
- Provides the information required to the
`odoo.addons.base_rest.restapi.method` decorator;

``` python
from odoo.addons.base_rest import restapi
from odoo.addons.component.core import Component
from odoo.addons.pydantic.models import BaseModel

class PingMessage(BaseModel):
message: str


class PingService(Component):
_inherit = 'base.rest.service'
_name = 'ping.service'
_usage = 'ping'
_collection = 'my_module.services'


@restapi.method(
[(["/pong"], "GET")],
input_param=restapi.PydanticModel(PingMessage),
output_param=restapi.PydanticModel(PingMessage),
auth="public",
)
def pong(self, ping_message):
return PingMessage(message = "Received: " + ping_message.message)
```
33 changes: 0 additions & 33 deletions base_rest_pydantic/readme/USAGE.rst

This file was deleted.

10 changes: 8 additions & 2 deletions base_rest_pydantic/restapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,15 +175,21 @@ def _do_validate(self, values, direction):
if self._min_items is not None and len(values) < self._min_items:
raise ExceptionClass(
_(
"BadRequest: Not enough items in the list (%(current)s < %(expected)s)",
(
"BadRequest: Not enough items in the list (%(current)s < "
"%(expected)s)"
),
current=len(values),
expected=self._min_items,
)
)
if self._max_items is not None and len(values) > self._max_items:
raise ExceptionClass(
_(
"BadRequest: Too many items in the list (%(current)s > %(expected)s)",
(
"BadRequest: Too many items in the list (%(current)s > "
"%(expected)s)"
),
current=len(values),
expected=self._max_items,
)
Expand Down
30 changes: 15 additions & 15 deletions base_rest_pydantic/static/description/index.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
Expand Down Expand Up @@ -369,9 +368,9 @@ <h1 class="title">Base Rest Datamodel</h1>
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:ed7868cc1a1d1a63a8b53b8e25e19ed162638e6f7eb3246bcc6cad2c5c0ca1a6
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/lgpl-3.0-standalone.html"><img alt="License: LGPL-3" src="https://img.shields.io/badge/licence-LGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/rest-framework/tree/16.0/base_rest_pydantic"><img alt="OCA/rest-framework" src="https://img.shields.io/badge/github-OCA%2Frest--framework-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/rest-framework-16-0/rest-framework-16-0-base_rest_pydantic"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/rest-framework&amp;target_branch=16.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p>This addon allows you to use Pydantic objects as params and/or response with your
REST API methods.</p>
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/lgpl-3.0-standalone.html"><img alt="License: LGPL-3" src="https://img.shields.io/badge/licence-LGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/rest-framework/tree/18.0/base_rest_pydantic"><img alt="OCA/rest-framework" src="https://img.shields.io/badge/github-OCA%2Frest--framework-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/rest-framework-18-0/rest-framework-18-0-base_rest_pydantic"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/rest-framework&amp;target_branch=18.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p>This addon allows you to use Pydantic objects as params and/or response
with your REST API methods.</p>
<p><strong>Table of contents</strong></p>
<div class="contents local topic" id="contents">
<ul class="simple">
Expand All @@ -387,22 +386,23 @@ <h1 class="title">Base Rest Datamodel</h1>
</div>
<div class="section" id="usage">
<h1><a class="toc-backref" href="#toc-entry-1">Usage</a></h1>
<p>To use Pydantic instances as request and/or response of a REST service endpoint
you must:</p>
<p>To use Pydantic instances as request and/or response of a REST service
endpoint you must:</p>
<ul class="simple">
<li>Define your Pydantic classes;</li>
<li>Provides the information required to the <tt class="docutils literal">odoo.addons.base_rest.restapi.method</tt> decorator;</li>
<li>Provides the information required to the
<tt class="docutils literal">odoo.addons.base_rest.restapi.method</tt> decorator;</li>
</ul>
<pre class="code python literal-block">
<span class="kn">from</span> <span class="nn">odoo.addons.base_rest</span> <span class="kn">import</span> <span class="n">restapi</span><span class="w">
</span><span class="kn">from</span> <span class="nn">odoo.addons.component.core</span> <span class="kn">import</span> <span class="n">Component</span><span class="w">
</span><span class="kn">from</span> <span class="nn">odoo.addons.pydantic.models</span> <span class="kn">import</span> <span class="n">BaseModel</span><span class="w">
<span class="kn">from</span><span class="w"> </span><span class="nn">odoo.addons.base_rest</span><span class="w"> </span><span class="kn">import</span> <span class="n">restapi</span><span class="w">
</span><span class="kn">from</span><span class="w"> </span><span class="nn">odoo.addons.component.core</span><span class="w"> </span><span class="kn">import</span> <span class="n">Component</span><span class="w">
</span><span class="kn">from</span><span class="w"> </span><span class="nn">odoo.addons.pydantic.models</span><span class="w"> </span><span class="kn">import</span> <span class="n">BaseModel</span><span class="w">

</span><span class="k">class</span> <span class="nc">PingMessage</span><span class="p">(</span><span class="n">BaseModel</span><span class="p">):</span><span class="w">
</span><span class="k">class</span><span class="w"> </span><span class="nc">PingMessage</span><span class="p">(</span><span class="n">BaseModel</span><span class="p">):</span><span class="w">
</span> <span class="n">message</span><span class="p">:</span> <span class="nb">str</span><span class="w">


</span><span class="k">class</span> <span class="nc">PingService</span><span class="p">(</span><span class="n">Component</span><span class="p">):</span><span class="w">
</span><span class="k">class</span><span class="w"> </span><span class="nc">PingService</span><span class="p">(</span><span class="n">Component</span><span class="p">):</span><span class="w">
</span> <span class="n">_inherit</span> <span class="o">=</span> <span class="s1">'base.rest.service'</span><span class="w">
</span> <span class="n">_name</span> <span class="o">=</span> <span class="s1">'ping.service'</span><span class="w">
</span> <span class="n">_usage</span> <span class="o">=</span> <span class="s1">'ping'</span><span class="w">
Expand All @@ -415,7 +415,7 @@ <h1><a class="toc-backref" href="#toc-entry-1">Usage</a></h1>
</span> <span class="n">output_param</span><span class="o">=</span><span class="n">restapi</span><span class="o">.</span><span class="n">PydanticModel</span><span class="p">(</span><span class="n">PingMessage</span><span class="p">),</span><span class="w">
</span> <span class="n">auth</span><span class="o">=</span><span class="s2">&quot;public&quot;</span><span class="p">,</span><span class="w">
</span> <span class="p">)</span><span class="w">
</span> <span class="k">def</span> <span class="nf">pong</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ping_message</span><span class="p">):</span><span class="w">
</span> <span class="k">def</span><span class="w"> </span><span class="nf">pong</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ping_message</span><span class="p">):</span><span class="w">
</span> <span class="k">return</span> <span class="n">PingMessage</span><span class="p">(</span><span class="n">message</span> <span class="o">=</span> <span class="s2">&quot;Received: &quot;</span> <span class="o">+</span> <span class="n">ping_message</span><span class="o">.</span><span class="n">message</span><span class="p">)</span>
</pre>
</div>
Expand All @@ -424,7 +424,7 @@ <h1><a class="toc-backref" href="#toc-entry-2">Bug Tracker</a></h1>
<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/rest-framework/issues">GitHub Issues</a>.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
<a class="reference external" href="https://github.com/OCA/rest-framework/issues/new?body=module:%20base_rest_pydantic%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
<a class="reference external" href="https://github.com/OCA/rest-framework/issues/new?body=module:%20base_rest_pydantic%0Aversion:%2018.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
<p>Do not contact contributors directly about support or help with technical issues.</p>
</div>
<div class="section" id="credits">
Expand All @@ -448,7 +448,7 @@ <h2><a class="toc-backref" href="#toc-entry-6">Maintainers</a></h2>
<p>OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.</p>
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/rest-framework/tree/16.0/base_rest_pydantic">OCA/rest-framework</a> project on GitHub.</p>
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/rest-framework/tree/18.0/base_rest_pydantic">OCA/rest-framework</a> project on GitHub.</p>
<p>You are welcome to contribute. To learn how please visit <a class="reference external" href="https://odoo-community.org/page/Contribute">https://odoo-community.org/page/Contribute</a>.</p>
</div>
</div>
Expand Down
5 changes: 2 additions & 3 deletions base_rest_pydantic/tests/test_from_params.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Copyright 2021 Wakari SRL
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
from typing import Type
from unittest import mock

from odoo.exceptions import UserError
Expand All @@ -13,15 +12,15 @@

class TestPydantic(TransactionCase):
def setUp(self):
super(TestPydantic, self).setUp()
super().setUp()

class Model1(BaseModel):
name: str
description: str = None

self.Model1: BaseModel = Model1

def _from_params(self, pydantic_cls: Type[BaseModel], params: dict, **kwargs):
def _from_params(self, pydantic_cls: type[BaseModel], params: dict, **kwargs):
restapi_pydantic_cls = (
restapi.PydanticModelList
if isinstance(params, list)
Expand Down
3 changes: 1 addition & 2 deletions base_rest_pydantic/tests/test_response.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Copyright 2021 Wakari SRL
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
from typing import List
from unittest import mock

from odoo.tests.common import TransactionCase
Expand All @@ -17,7 +16,7 @@ def _to_response(self, instance: BaseModel):
mock_service.env = self.env
return restapi_pydantic.to_response(mock_service, instance)

def _to_response_list(self, instance: List[BaseModel]):
def _to_response_list(self, instance: list[BaseModel]):
restapi_pydantic = restapi.PydanticModelList(instance[0].__class__)
mock_service = mock.Mock()
mock_service.env = self.env
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
a2wsgi>=1.10.6
fastapi>=0.110.0
parse-accept-language
pydantic>=2.0.0
python-multipart
ujson

0 comments on commit 2928646

Please sign in to comment.