Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Return shipping app id in ShippingMethodSchema.id (#17586)

Iga Karbowiak9680f7193357
diff --git a/saleor/webhook/response_schemas/shipping.py b/saleor/webhook/response_schemas/shipping.py
index 5aa752e222..9a3c69f789 100644
--- a/saleor/webhook/response_schemas/shipping.py
+++ b/saleor/webhook/response_schemas/shipping.py
@@ -9,15 +9,18 @@ from pydantic import (
Field,
RootModel,
ValidationError,
+ ValidationInfo,
ValidatorFunctionWrapHandler,
WrapValidator,
field_validator,
)
from pydantic_core import PydanticOmit
+from ...app.models import App
from ...graphql.core.utils import from_global_id_or_error
from ...shipping.models import ShippingMethod
from ..const import APP_ID_PREFIX
+from ..transport.shipping_helpers import to_shipping_app_id
from .annotations import DefaultIfNone, Metadata
logger = logging.getLogger(__name__)
@@ -54,6 +57,14 @@ class ShippingMethodSchema(BaseModel):
def price(self) -> Money:
return Money(self.amount, self.currency)
+ @field_validator("id", mode="after")
+ @classmethod
+ def clean_id(cls, shipping_method_id: str, info: ValidationInfo) -> str:
+ app: App | None = info.context.get("app", None) if info.context else None
+ if not app:
+ raise RuntimeError("Missing app in context")
+ return to_shipping_app_id(app, shipping_method_id)
+
class ListShippingMethodsSchema(RootModel):
root: DefaultIfNone[list[OnErrorSkipShippingMethod[ShippingMethodSchema]]] = []
diff --git a/saleor/webhook/tests/response_schemas/test_shipping.py b/saleor/webhook/tests/response_schemas/test_shipping.py
index 374c4da9ba..dff3325c53 100644
--- a/saleor/webhook/tests/response_schemas/test_shipping.py
+++ b/saleor/webhook/tests/response_schemas/test_shipping.py
@@ -13,6 +13,7 @@ from ...response_schemas.shipping import (
ShippingMethodSchema,
logger,
)
+from ...transport.shipping_helpers import to_shipping_app_id
def decode_id(id):
@@ -92,12 +93,14 @@ def decode_id(id):
},
],
)
-def test_shipping_method_schema_valid(data):
+def test_shipping_method_schema_valid(data, app):
# when
- shipping_method_model = ShippingMethodSchema.model_validate(data)
+ shipping_method_model = ShippingMethodSchema.model_validate(
+ data, context={"app": app}
+ )
# then
- assert shipping_method_model.id == str(data["id"])
+ assert shipping_method_model.id == to_shipping_app_id(app, data["id"])
assert shipping_method_model.name == data["name"]
assert shipping_method_model.amount == Decimal(data["amount"])
assert shipping_method_model.currency == data["currency"]
@@ -114,7 +117,7 @@ def test_shipping_method_schema_valid(data):
@pytest.mark.parametrize(
"metadata", [12345, "not_a_dict", {123: 123}, {"123": 123}, {123: "123"}]
)
-def test_shipping_method_schema_invalid_metadata_skipped(metadata):
+def test_shipping_method_schema_invalid_metadata_skipped(metadata, app):
# given
data = {
"id": "4",
@@ -125,10 +128,12 @@ def test_shipping_method_schema_invalid_metadata_skipped(metadata):
}
# when
- shipping_method_model = ShippingMethodSchema.model_validate(data)
+ shipping_method_model = ShippingMethodSchema.model_validate(
+ data, context={"app": app}
+ )
# then
- assert shipping_method_model.id == data["id"]
+ assert shipping_method_model.id == to_shipping_app_id(app, data["id"])
assert shipping_method_model.name == data["name"]
assert shipping_method_model.amount == data["amount"]
assert shipping_method_model.currency == data["currency"]
@@ -221,24 +226,24 @@ def test_shipping_method_schema_invalid_metadata_skipped(metadata):
},
],
)
-def test_shipping_method_schema_invalid(data):
+def test_shipping_method_schema_invalid(data, app):
with pytest.raises(ValidationError) as e:
- ShippingMethodSchema.model_validate(data)
+ ShippingMethodSchema.model_validate(data, context={"app": app})
assert len(e.value.errors()) == 1
@pytest.mark.parametrize("data", [None, []])
-def test_list_shipping_methods_schema_skipped_values(data):
+def test_list_shipping_methods_schema_skipped_values(data, app):
# when
- list_methods = ListShippingMethodsSchema.model_validate(data)
+ list_methods = ListShippingMethodsSchema.model_validate(data, context={"app": app})
# Then the root should be an empty list
assert list_methods.root == []
@patch.object(logger, "warning")
-def test_list_shipping_methods_schema_invalid_element_skipped(mocked_logger):
+def test_list_shipping_methods_schema_invalid_element_skipped(mocked_logger, app):
"""Test when the provided input has 2 elements, one valid and one invalid."""
# given a list with one valid and one invalid shipping method
data = [
@@ -261,11 +266,11 @@ def test_list_shipping_methods_schema_invalid_element_skipped(mocked_logger):
]
# when
- schema = ListShippingMethodsSchema.model_validate(data)
+ schema = ListShippingMethodsSchema.model_validate(data, context={"app": app})
# then only the valid shipping method should be included
assert len(schema.root) == 1
- assert schema.root[0].id == data[0]["id"]
+ assert schema.root[0].id == to_shipping_app_id(app, data[0]["id"])
assert schema.root[0].name == data[0]["name"]
assert mocked_logger.call_count == 1
diff --git a/saleor/webhook/transport/shipping.py b/saleor/webhook/transport/shipping.py
index dd5aa03230..c82fe5df68 100644
--- a/saleor/webhook/transport/shipping.py
+++ b/saleor/webhook/transport/shipping.py
@@ -22,7 +22,6 @@ from ..response_schemas.shipping import (
FilterShippingMethodsSchema,
ListShippingMethodsSchema,
)
-from .shipping_helpers import to_shipping_app_id
from .synchronous.transport import trigger_webhook_sync_if_not_cached
logger = logging.getLogger(__name__)
@@ -33,14 +32,14 @@ def parse_list_shipping_methods_response(
) -> list["ShippingMethodData"]:
try:
list_shipping_method_model = ListShippingMethodsSchema.model_validate(
- response_data
+ response_data, context={"app": app}
)
except ValidationError:
logger.warning("Skipping invalid shipping method response: %s", response_data)
return []
return [
ShippingMethodData(
- id=to_shipping_app_id(app, str(shipping_method.id)),
+ id=shipping_method.id,
name=shipping_method.name,
price=shipping_method.price,
maximum_delivery_days=shipping_method.maximum_delivery_days,