From 0d90fd420fe61cf9c0166f386b11b82b22619151 Mon Sep 17 00:00:00 2001 From: Auke Willem Oosterhoff <1565144+OrangeTux@users.noreply.github.com> Date: Mon, 3 Jun 2024 16:16:21 +0200 Subject: [PATCH 1/4] Fix serialization of 'ocpp_csms_url' (#642) 'camelCasing' `ocpp_csms_url` ends up as `ocppCSMSURL`. This is wrong. The spec uses [ocppCsmsUrl](https://github.com/mobilityhouse/ocpp/blob/774a507b348da35de7dee81177665c8f88ff174b/ocpp/v201/schemas/SetNetworkProfileRequest.json#L146). This commit fixes the conversion. --- ocpp/charge_point.py | 8 +++++--- tests/test_charge_point.py | 4 +++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/ocpp/charge_point.py b/ocpp/charge_point.py index 7046cada5..601871488 100644 --- a/ocpp/charge_point.py +++ b/ocpp/charge_point.py @@ -25,8 +25,7 @@ def camel_to_snake_case(data): if isinstance(data, dict): snake_case_dict = {} for key, value in data.items(): - key = key.replace("ocppCSMS", "ocpp_csms") - key = key.replace("V2X", "_v2x") + key = key.replace("ocppCSMSURL", "ocpp_csms_url") key = key.replace("V2X", "_v2x").replace("V2G", "_v2g") s1 = re.sub("(.)([A-Z][a-z]+)", r"\1_\2", key) key = re.sub("([a-z0-9])([A-Z])(?=\\S)", r"\1_\2", s1).lower() @@ -57,7 +56,10 @@ def snake_to_camel_case(data): for key, value in data.items(): key = key.replace("soc", "SoC") key = key.replace("_v2x", "V2X") - key = key.replace("ocpp_csms", "ocppCSMS") + # The spec uses inconsent casing for "csms" and "url". + # E.g. "OcppCsmsUrl" vs "ResponderURL" and "CSMSRootCertificate" + key = key.replace("ocpp_csms_url", "ocppCsmsUrl") + key = key.replace("csms", "CSMS") key = key.replace("_url", "URL") key = key.replace("soc", "SoC").replace("_SoCket", "Socket") key = key.replace("_v2x", "V2X") diff --git a/tests/test_charge_point.py b/tests/test_charge_point.py index b1ab1412b..00571f9fd 100644 --- a/tests/test_charge_point.py +++ b/tests/test_charge_point.py @@ -68,6 +68,7 @@ def heartbeat(self, **kwargs): ({"responderURL": "foo.com"}, {"responder_url": "foo.com"}), ({"url": "foo.com"}, {"url": "foo.com"}), ({"ocppCSMSURL": "foo.com"}, {"ocpp_csms_url": "foo.com"}), + ({"CSMSRootCertificate": "foo.com"}, {"csms_root_certificate": "foo.com"}), ({"InvalidURL": "foo.com"}, {"invalid_url": "foo.com"}), ({"evMinV2XEnergyRequest": 200}, {"ev_min_v2x_energy_request": 200}), ({"v2xChargingCtrlr": 200}, {"v2x_charging_ctrlr": 200}), @@ -94,7 +95,8 @@ def test_camel_to_snake_case(test_input, expected): ({"v2x_charging_ctrlr": 200}, {"v2xChargingCtrlr": 200}), ({"responder_url": "foo.com"}, {"responderURL": "foo.com"}), ({"url": "foo.com"}, {"url": "foo.com"}), - ({"ocpp_csms_url": "foo.com"}, {"ocppCSMSURL": "foo.com"}), + ({"ocpp_csms_url": "foo.com"}, {"ocppCsmsUrl": "foo.com"}), + ({"csms_root_certificate": "foo.com"}, {"CSMSRootCertificate": "foo.com"}), ({"invalid_url": "foo.com"}, {"invalidURL": "foo.com"}), ({"web_socket_ping_interval": 200}, {"webSocketPingInterval": 200}), ({"sign_v2g_certificate": 200}, {"signV2GCertificate": 200}), From c01f634d5514870bce6e8590a409d5ab0a864d2c Mon Sep 17 00:00:00 2001 From: Auke Willem Oosterhoff <1565144+OrangeTux@users.noreply.github.com> Date: Mon, 3 Jun 2024 16:38:13 +0200 Subject: [PATCH 2/4] Bump to 2.0.0-rc.1 (#643) --- CHANGELOG.md | 5 +++++ docs/source/conf.py | 2 +- pyproject.toml | 2 +- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e9c1018b5..172fddb64 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Change log +## 2.0.0-rc.1 (2024-06-01) + +## BREAKING ## +- [#642](https://github.com/mobilityhouse/ocpp/pull/642) Fix serializing of "ocpp_csms_url". + ## 2.0.0-rc.0 (2024-05-22) - [#631](https://github.com/mobilityhouse/ocpp/pull/631) Fix publishing to Pypi. diff --git a/docs/source/conf.py b/docs/source/conf.py index b083ceb8a..d910b7751 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -23,7 +23,7 @@ author = "Auke Willem Oosterhoff" # The full version, including alpha/beta/rc tags -release = "2.0.0-rc.0" +release = "2.0.0-rc.1" # -- General configuration --------------------------------------------------- diff --git a/pyproject.toml b/pyproject.toml index ababf6f09..b36db7aa9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "ocpp" -version = "2.0.0-rc.0" +version = "2.0.0-rc.1" description = "Python package implementing the JSON version of the Open Charge Point Protocol (OCPP)." authors = [ "André Duarte ", From 26fa0ab26acb169aae14aaaf47e611ad24fd974d Mon Sep 17 00:00:00 2001 From: esiebert <52153105+esiebert@users.noreply.github.com> Date: Tue, 18 Jun 2024 13:07:45 +0200 Subject: [PATCH 3/4] Skip schema validation on call (#650) Implements schema validation skipping when running `call`. `skip_schema_validation` is defaulted to `False`, and once set to `True`, will skip schema validation of both request and response. Implements https://github.com/mobilityhouse/ocpp/issues/315. --- ocpp/charge_point.py | 12 +++++++++--- tests/v16/conftest.py | 10 ++++++++++ tests/v16/test_v16_charge_point.py | 26 ++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/ocpp/charge_point.py b/ocpp/charge_point.py index 601871488..c2b6339eb 100644 --- a/ocpp/charge_point.py +++ b/ocpp/charge_point.py @@ -361,7 +361,9 @@ async def _handle_call(self, msg): pass return response - async def call(self, payload, suppress=True, unique_id=None): + async def call( + self, payload, suppress=True, unique_id=None, skip_schema_validation=False + ): """ Send Call message to client and return payload of response. @@ -382,6 +384,9 @@ async def call(self, payload, suppress=True, unique_id=None): set to False, an exception will be raised for users to handle this CallError. + Schema validation can be skipped for the request and the response + for this call by setting `skip_schema_validation` to `True`. + """ camel_case_payload = snake_to_camel_case(serialize_as_dict(payload)) @@ -400,7 +405,8 @@ async def call(self, payload, suppress=True, unique_id=None): payload=remove_nones(camel_case_payload), ) - validate_payload(call, self._ocpp_version) + if not skip_schema_validation: + validate_payload(call, self._ocpp_version) # Use a lock to prevent make sure that only 1 message can be send at a # a time. @@ -421,7 +427,7 @@ async def call(self, payload, suppress=True, unique_id=None): if suppress: return raise response.to_exception() - else: + elif not skip_schema_validation: response.action = call.action validate_payload(response, self._ocpp_version) diff --git a/tests/v16/conftest.py b/tests/v16/conftest.py index 73e4b5de8..301d49f12 100644 --- a/tests/v16/conftest.py +++ b/tests/v16/conftest.py @@ -1,3 +1,4 @@ +from dataclasses import dataclass from unittest.mock import AsyncMock import pytest @@ -50,6 +51,15 @@ def mock_boot_request(): ) +@pytest.fixture +def mock_invalid_boot_request(): + @dataclass + class BootNotification: + custom_field: str + + return BootNotification(custom_field="custom_field") + + @pytest.fixture def mock_base_central_system(base_central_system): mock_result_call = CallResult( diff --git a/tests/v16/test_v16_charge_point.py b/tests/v16/test_v16_charge_point.py index 37d8d8e3c..3204f60e3 100644 --- a/tests/v16/test_v16_charge_point.py +++ b/tests/v16/test_v16_charge_point.py @@ -263,3 +263,29 @@ async def test_call_without_unique_id_should_return_a_random_value( ) = mock_base_central_system._get_specific_response.call_args_list[0][0] # Check the actual unique id is equals to the one internally generated assert actual_unique_id == expected_unique_id + + +@pytest.mark.asyncio +async def test_call_skip_schema_validation( + mock_invalid_boot_request, mock_base_central_system +): + """ + Test that schema validation is skipped for an invalid boot notification request. + + """ + + expected_unique_id = "12345" + # Call the method being tested with an invalid boot notification request + # and a unique_id as a parameter + await mock_base_central_system.call( + mock_invalid_boot_request, + unique_id=expected_unique_id, + skip_schema_validation=True, + ) + ( + actual_unique_id, + _, + ) = mock_base_central_system._get_specific_response.call_args_list[0][0] + + # Check the actual unique id is equals to the one passed to the call method + assert actual_unique_id == expected_unique_id From d59ecf140eead945b234745d956d142c79a3816d Mon Sep 17 00:00:00 2001 From: Auke Willem Oosterhoff <1565144+OrangeTux@users.noreply.github.com> Date: Tue, 18 Jun 2024 13:21:45 +0200 Subject: [PATCH 4/4] Bump to 2.0.0-rc.2 (#651) Co-authored-by: esiebert <52153105+esiebert@users.noreply.github.com> --- CHANGELOG.md | 4 ++++ docs/source/conf.py | 2 +- pyproject.toml | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 172fddb64..e65dedb43 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Change log +## 2.0.0-rc.2 (2024-06-18) + +- [#315](https://github.com/mobilityhouse/ocpp/pull/315) Allow to skip schema validation in `ChargePoint.call()`. Thanks [@esiebert](https://github.com/esiebert)! + ## 2.0.0-rc.1 (2024-06-01) ## BREAKING ## diff --git a/docs/source/conf.py b/docs/source/conf.py index d910b7751..a17f17a84 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -23,7 +23,7 @@ author = "Auke Willem Oosterhoff" # The full version, including alpha/beta/rc tags -release = "2.0.0-rc.1" +release = "2.0.0-rc.2" # -- General configuration --------------------------------------------------- diff --git a/pyproject.toml b/pyproject.toml index b36db7aa9..bdf99cd8c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "ocpp" -version = "2.0.0-rc.1" +version = "2.0.0-rc.2" description = "Python package implementing the JSON version of the Open Charge Point Protocol (OCPP)." authors = [ "André Duarte ",