Skip to content

Commit 9665899

Browse files
rihito0907starmandeluxe
authored andcommitted
Internal change
PiperOrigin-RevId: 482409723
1 parent 00d10ab commit 9665899

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

shoptimizer_api/main.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ def optimize() -> Tuple[str, http.HTTPStatus]:
131131
'condition_optimizer_config_override':
132132
flask.request.headers.get('condition_optimizer_config_override', ''),
133133
'free_shipping_optimizer_config_override':
134-
flask.request.headers.get(
135-
'free_shipping_optimizer_config_override', ''),
134+
flask.request.headers.get('free_shipping_optimizer_config_override',
135+
''),
136136
'gender_optimizer_config_override':
137137
flask.request.headers.get('gender_optimizer_config_override', ''),
138138
'promo_text_removal_optimizer_config_override':
@@ -163,6 +163,8 @@ def optimize() -> Tuple[str, http.HTTPStatus]:
163163

164164
product_batch = flask.request.json
165165

166+
_sanitize_batch(product_batch)
167+
166168
optimized_product_batch, builtin_optimizer_results = _run_optimizers(
167169
product_batch, lang_url_parameter, country_url_parameter,
168170
currency_url_parameter, _builtin_optimizer_cache)
@@ -240,6 +242,18 @@ def _check_request_valid(lang_url_parameter: str) -> (bool, str):
240242
return True, ''
241243

242244

245+
def _sanitize_batch(product_batch: Dict[str, Any]) -> None:
246+
"""Cleans up the product batch.
247+
248+
Args:
249+
product_batch: A batch of product data.
250+
"""
251+
for entry in product_batch['entries']:
252+
product = entry['product']
253+
for attribute in ('title', 'description'):
254+
product[attribute] = str(product.get(attribute, ''))
255+
256+
243257
def _run_optimizers(
244258
product_batch: Dict[str, Any],
245259
language: str,

shoptimizer_api/main_test.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,27 @@ def test_entries_with_no_list_of_products_returns_error(self):
7575
response_dict['error-msg'])
7676
self.assertEqual(http.HTTPStatus.BAD_REQUEST, response.status_code)
7777

78+
@parameterized.named_parameters([{
79+
'testcase_name': 'title_is_numeric',
80+
'attribute': 'title'
81+
}, {
82+
'testcase_name': 'description_is_numeric',
83+
'attribute': 'description'
84+
}])
85+
def test_numeric_attribute_is_converted_to_string(self, attribute):
86+
request_body = requests_bodies.build_request_body(
87+
properties_to_be_updated={attribute: 1.234})
88+
89+
response = self.test_client.post(
90+
f'{main._V1_BASE_URL}/batch/optimize', json=request_body)
91+
92+
response_data = response.data.decode('utf-8')
93+
response_dict = json.loads(response_data)
94+
95+
self.assertIsInstance(
96+
response_dict['optimized-data']['entries'][0]['product'][attribute],
97+
str)
98+
7899
def test_unmapped_route_returns_404(self):
79100
response = self.test_client.get(f'{main._V1_BASE_URL}/unmapped-route')
80101
self.assertEqual(http.HTTPStatus.NOT_FOUND, response.status_code)

0 commit comments

Comments
 (0)