diff --git a/CHANGELOG.md b/CHANGELOG.md index e9c1018b5..e65dedb43 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,14 @@ # 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 ## +- [#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..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.0" +release = "2.0.0-rc.2" # -- General configuration --------------------------------------------------- diff --git a/ocpp/charge_point.py b/ocpp/charge_point.py index 250a562bd..cba622a5b 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") @@ -363,7 +365,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. @@ -384,6 +388,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)) @@ -402,7 +409,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. @@ -423,7 +431,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/pyproject.toml b/pyproject.toml index ababf6f09..bdf99cd8c 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.2" description = "Python package implementing the JSON version of the Open Charge Point Protocol (OCPP)." authors = [ "André Duarte ", diff --git a/tests/test_charge_point.py b/tests/test_charge_point.py index 1abc8535c..5a9914fa5 100644 --- a/tests/test_charge_point.py +++ b/tests/test_charge_point.py @@ -69,6 +69,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}), @@ -95,7 +96,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}), 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