Skip to content

Commit 99b5896

Browse files
committed
ACP2E-3660: REST endpoint Product Import Json not validate the mandatory fields
1 parent db01736 commit 99b5896

File tree

4 files changed

+66
-19
lines changed

4 files changed

+66
-19
lines changed

app/code/Magento/CatalogImportExport/Model/Import/Product/Type/AbstractType.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2011 Adobe
4+
* All Rights Reserved.
55
*/
66
namespace Magento\CatalogImportExport\Model\Import\Product\Type;
77

@@ -27,6 +27,8 @@
2727
*/
2828
abstract class AbstractType
2929
{
30+
private const NON_REQUIRED_ATTRIBUTES_EXISTING_PRODUCTS = [Product::COL_NAME];
31+
3032
/**
3133
* @var array
3234
*/
@@ -581,7 +583,9 @@ public function isRowValid(array $rowData, $rowNum, $isNewProduct = true)
581583
// For the default scope - if this is a new product or
582584
// for an old product, if the imported doc has the column present for the attrCode
583585
if (Product::SCOPE_DEFAULT == $rowScope &&
584-
($isNewProduct || array_key_exists($attrCode, $rowData))) {
586+
($isNewProduct || !in_array($attrCode,self::NON_REQUIRED_ATTRIBUTES_EXISTING_PRODUCTS)) &&
587+
array_key_exists($attrCode, $rowData)
588+
) {
585589
$this->_entityModel->addRowError(
586590
RowValidatorInterface::ERROR_VALUE_IS_REQUIRED,
587591
$rowNum,

app/code/Magento/CatalogImportExport/Model/Import/Product/Validator.php

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -381,31 +381,36 @@ public function getInvalidAttribute()
381381
/**
382382
* Is valid attributes
383383
*
384-
* @return bool
384+
* @return array
385385
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
386386
*/
387-
protected function isValidAttributes()
387+
protected function isValidAttributes(): array
388388
{
389389
$this->_clearMessages();
390390
$this->setInvalidAttribute(null);
391+
$attributeValidationResult['result'] = true;
392+
391393
if (!isset($this->_rowData['product_type'])) {
392-
return false;
394+
$attributeValidationResult['result'] = false;
395+
return $attributeValidationResult;
393396
}
394397
$entityTypeModel = $this->context->retrieveProductTypeByName($this->_rowData['product_type']);
395398
if ($entityTypeModel) {
399+
$result = true;
396400
foreach ($this->_rowData as $attrCode => $attrValue) {
397401
$attrParams = $entityTypeModel->retrieveAttributeFromCache($attrCode);
398402
if ($attrCode === Product::COL_CATEGORY && $attrValue) {
399-
$this->isCategoriesValid($attrValue);
403+
$result = $this->isCategoriesValid($attrValue);
400404
} elseif ($attrParams) {
401-
$this->isAttributeValid($attrCode, $attrParams, $this->_rowData);
405+
$result = $this->isAttributeValid($attrCode, $attrParams, $this->_rowData);
402406
}
407+
$attributeValidationResult['attributes'][$attrCode] = $result;
403408
}
404409
if ($this->getMessages()) {
405-
return false;
410+
$attributeValidationResult['result'] = false;
406411
}
407412
}
408-
return true;
413+
return $attributeValidationResult;
409414
}
410415

411416
/**
@@ -415,14 +420,28 @@ public function isValid($value)
415420
{
416421
$this->_rowData = $value;
417422
$this->_clearMessages();
418-
$returnValue = $this->isValidAttributes();
423+
$validatedAttributes = $this->isValidAttributes();
424+
/** @var Product\Validator\AbstractImportValidator $validator */
419425
foreach ($this->validators as $validator) {
420426
if (!$validator->isValid($value)) {
421-
$returnValue = false;
422427
$this->_addMessages($validator->getMessages());
428+
} else {
429+
//prioritize specialized validation
430+
if ($validator->getFieldName() &&
431+
$validatedAttributes['attributes'][$validator->getFieldName()] === false
432+
) {
433+
$validatedAttributes['attributes'][$validator->getFieldName()] = true;
434+
foreach ($this->_messages as $key => $message) {
435+
if (str_contains($message, $validator->getFieldName())) {
436+
unset($this->_messages[$key]);
437+
}
438+
}
439+
$this->_messages = array_values($this->_messages);
440+
}
423441
}
424442
}
425-
return $returnValue;
443+
444+
return !in_array(false, array_values($validatedAttributes['attributes']));
426445
}
427446

428447
/**
@@ -439,6 +458,16 @@ public function getRowScope(array $rowData)
439458
return Product::SCOPE_STORE;
440459
}
441460

461+
private function getValidatorFields(): array
462+
{
463+
$validatorFields = [];
464+
/** @var Product\Validator\AbstractImportValidator $validator */
465+
foreach($this->validators as $validator) {
466+
$validatorFields[] = $validator->getFieldName();
467+
}
468+
return $validatorFields;
469+
}
470+
442471
/**
443472
* Validate category names
444473
*

app/code/Magento/CatalogImportExport/Model/Import/Product/Validator/AbstractImportValidator.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2015 Adobe
4+
* All Rights Reserved.
55
*/
66
namespace Magento\CatalogImportExport\Model\Import\Product\Validator;
77

@@ -10,6 +10,11 @@
1010

1111
abstract class AbstractImportValidator extends AbstractValidator implements RowValidatorInterface
1212
{
13+
/**
14+
* @var string|null
15+
*/
16+
protected ?string $fieldName = null;
17+
1318
/**
1419
* @var \Magento\CatalogImportExport\Model\Import\Product
1520
*/
@@ -24,4 +29,12 @@ public function init($context)
2429
$this->context = $context;
2530
return $this;
2631
}
32+
33+
/**
34+
* @return string|null
35+
*/
36+
public function getFieldName(): ?string
37+
{
38+
return $this->fieldName;
39+
}
2740
}

app/code/Magento/CatalogImportExport/Model/Import/Product/Validator/Name.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class Name extends AbstractImportValidator implements RowValidatorInterface
2424
public function __construct(SkuStorage $skuStorage)
2525
{
2626
$this->skuStorage = $skuStorage;
27+
$this->fieldName = Product::COL_NAME;
2728
}
2829

2930
/**
@@ -41,7 +42,7 @@ public function isValid($value)
4142
[
4243
sprintf(
4344
$this->context->retrieveMessageTemplate(self::ERROR_INVALID_ATTRIBUTE_TYPE),
44-
'name',
45+
$this->fieldName,
4546
'not empty'
4647
)
4748
]
@@ -59,9 +60,9 @@ public function isValid($value)
5960
*/
6061
private function hasNameValue(array $rowData): bool
6162
{
62-
return array_key_exists(Product::COL_NAME, $rowData) &&
63-
!empty($rowData[Product::COL_NAME]) &&
64-
$rowData[Product::COL_NAME] !== $this->context->getEmptyAttributeValueConstant();
63+
return array_key_exists($this->fieldName, $rowData) &&
64+
!empty($rowData[$this->fieldName]) &&
65+
$rowData[$this->fieldName] !== $this->context->getEmptyAttributeValueConstant();
6566
}
6667

6768
/**

0 commit comments

Comments
 (0)