/**
  * {@inheritdoc}
  */
 public function save(\Magento\Catalog\Api\Data\CategoryInterface $category)
 {
     $existingData = $category->toFlatArray();
     if ($category->getId()) {
         $existingCategory = $this->get($category->getId());
         if (isset($existingData['image']) && is_array($existingData['image'])) {
             $existingData['image_additional_data'] = $existingData['image'];
             unset($existingData['image']);
         }
         $existingData['id'] = $existingCategory->getId();
         $existingData['parent_id'] = $existingCategory->getParentId();
         $existingData['path'] = $existingCategory->getPath();
         $existingData['is_active'] = $existingCategory->getIsActive();
         $existingData['include_in_menu'] = isset($existingData['include_in_menu']) ? (bool) $existingData['include_in_menu'] : false;
         $category->setData($existingData);
     } else {
         $parentId = $category->getParentId() ?: $this->storeManager->getStore()->getRootCategoryId();
         $parentCategory = $this->get($parentId);
         $existingData['include_in_menu'] = isset($existingData['include_in_menu']) ? (bool) $existingData['include_in_menu'] : false;
         /** @var  $category Category */
         $category->setData($existingData);
         $category->setPath($parentCategory->getPath());
     }
     try {
         $this->validateCategory($category);
         $this->categoryResource->save($category);
     } catch (\Exception $e) {
         throw new CouldNotSaveException(__('Could not save category: %1', $e->getMessage()), $e);
     }
     unset($this->instances[$category->getId()]);
     return $category;
 }