-
Notifications
You must be signed in to change notification settings - Fork 26
refactor: migration validation #117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
larskemper
wants to merge
29
commits into
feature/migration-logging-refactor
Choose a base branch
from
feat/add-validation-of-resolution-values
base: feature/migration-logging-refactor
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
1fc5d46
refactor: filter optional database fields
larskemper 9d5ab13
test: nullable validation fields
larskemper 5bfc5e4
refactor: implement reset interface
larskemper 2260f76
fix: search not flushed mappings for associations
larskemper e35fa66
Merge branch 'feature/migration-logging-refactor' into refactor/filte…
larskemper b13a0b2
refactor: validation of migration
larskemper 99968d0
test: fix
larskemper 49d4f79
refactor: validation
larskemper 54de53b
feat: cleanup and add tests
larskemper 2be5679
fix: null order custom field
larskemper 89d8147
Merge branch 'feature/migration-logging-refactor' into refactor/filte…
larskemper 9526887
fix: test
larskemper 9036ca7
feat: add validation of resolution values
larskemper 9cb5a7a
test: fix
larskemper 6e7a457
feat: add defaults to examples
larskemper 7dd489b
test: error resolution controller
larskemper 4b4a984
test: jest
larskemper 82a5c79
refactor: unify controller annoations
larskemper ec3f5da
refactor: rebase & adjust to changes
larskemper 0de2bed
fix: typo
larskemper f29f329
fix: card border
larskemper 6b22087
feat: add nested field validation
larskemper 4a4ddef
refactor: validation
larskemper 6b242c5
test: refine php tests
larskemper 534d4d1
refactor: simplify
larskemper 2f81945
test: add indexed generator keys
larskemper 9583ee2
fix: remove old invalid code
larskemper a2554ff
Merge remote-tracking branch 'origin/feature/migration-logging-refact…
larskemper 3e0ea79
fix: resolve threads
larskemper File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| <?php declare(strict_types=1); | ||
| /* | ||
| * (c) shopware AG <info@shopware.com> | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace SwagMigrationAssistant\Controller; | ||
|
|
||
| use Shopware\Core\Framework\Context; | ||
| use Shopware\Core\Framework\Log\Package; | ||
| use Shopware\Core\Framework\Routing\ApiRouteScope; | ||
| use Shopware\Core\Framework\Validation\WriteConstraintViolationException; | ||
| use Shopware\Core\PlatformRequest; | ||
| use SwagMigrationAssistant\Exception\MigrationException; | ||
| use SwagMigrationAssistant\Migration\ErrorResolution\MigrationFieldExampleGenerator; | ||
| use SwagMigrationAssistant\Migration\Validation\Exception\MigrationValidationException; | ||
| use SwagMigrationAssistant\Migration\Validation\MigrationFieldValidationService; | ||
| use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | ||
| use Symfony\Component\HttpFoundation\JsonResponse; | ||
| use Symfony\Component\HttpFoundation\Request; | ||
| use Symfony\Component\Routing\Attribute\Route; | ||
|
|
||
| #[Route(defaults: [PlatformRequest::ATTRIBUTE_ROUTE_SCOPE => [ApiRouteScope::ID]])] | ||
| #[Package('fundamentals@after-sales')] | ||
| class ErrorResolutionController extends AbstractController | ||
| { | ||
| /** | ||
| * @internal | ||
| */ | ||
| public function __construct( | ||
| private readonly MigrationFieldValidationService $fieldValidationService, | ||
| ) { | ||
| } | ||
|
|
||
| #[Route( | ||
| path: '/api/_action/migration/error-resolution/validate', | ||
| name: 'api.admin.migration.error-resolution.validate', | ||
| defaults: [PlatformRequest::ATTRIBUTE_ACL => ['swag_migration.viewer']], | ||
| methods: [Request::METHOD_POST] | ||
| )] | ||
| public function validateResolution(Request $request, Context $context): JsonResponse | ||
| { | ||
| $entityName = (string) $request->request->get('entityName'); | ||
| $fieldName = (string) $request->request->get('fieldName'); | ||
|
|
||
| if ($entityName === '') { | ||
| throw MigrationException::missingRequestParameter('entityName'); | ||
| } | ||
|
|
||
| if ($fieldName === '') { | ||
| throw MigrationException::missingRequestParameter('fieldName'); | ||
| } | ||
|
|
||
| $fieldValue = $this->decodeFieldValue($request->request->all()['fieldValue'] ?? null); | ||
|
|
||
| if ($fieldValue === null) { | ||
| throw MigrationException::missingRequestParameter('fieldValue'); | ||
| } | ||
|
|
||
| try { | ||
| $this->fieldValidationService->validateField( | ||
| $entityName, | ||
| $fieldName, | ||
| $fieldValue, | ||
| $context, | ||
| ); | ||
| } catch (MigrationValidationException $exception) { | ||
| $previous = $exception->getPrevious(); | ||
|
|
||
| if ($previous instanceof WriteConstraintViolationException) { | ||
| return new JsonResponse([ | ||
| 'valid' => false, | ||
| 'violations' => $previous->toArray(), | ||
| ]); | ||
| } | ||
|
|
||
| return new JsonResponse([ | ||
| 'valid' => false, | ||
| 'violations' => [['message' => $exception->getMessage()]], | ||
| ]); | ||
| } catch (\Throwable $exception) { | ||
| return new JsonResponse([ | ||
| 'valid' => false, | ||
| 'violations' => [['message' => $exception->getMessage()]], | ||
| ]); | ||
| } | ||
|
|
||
| return new JsonResponse([ | ||
| 'valid' => true, | ||
| 'violations' => [], | ||
| ]); | ||
| } | ||
|
|
||
| #[Route( | ||
| path: '/api/_action/migration/error-resolution/example-field-structure', | ||
| name: 'api.admin.migration.error-resolution.example-field-structure', | ||
| defaults: [PlatformRequest::ATTRIBUTE_ACL => ['swag_migration.viewer']], | ||
| methods: [Request::METHOD_POST] | ||
| )] | ||
| public function getExampleFieldStructure(Request $request): JsonResponse | ||
| { | ||
| $entityName = (string) $request->request->get('entityName'); | ||
| $fieldName = (string) $request->request->get('fieldName'); | ||
|
|
||
| if ($entityName === '') { | ||
| throw MigrationException::missingRequestParameter('entityName'); | ||
| } | ||
|
|
||
| if ($fieldName === '') { | ||
| throw MigrationException::missingRequestParameter('fieldName'); | ||
| } | ||
|
|
||
| $resolved = $this->fieldValidationService->resolveFieldPath($entityName, $fieldName); | ||
|
|
||
| if ($resolved === null) { | ||
| throw MigrationValidationException::entityFieldNotFound($entityName, $fieldName); | ||
| } | ||
|
|
||
| [, $field] = $resolved; | ||
|
|
||
| $response = [ | ||
| 'fieldType' => MigrationFieldExampleGenerator::getFieldType($field), | ||
| 'example' => MigrationFieldExampleGenerator::generateExample($field), | ||
| ]; | ||
|
|
||
| return new JsonResponse($response); | ||
| } | ||
|
|
||
| /** | ||
| * @return array<array-key, mixed>|bool|float|int|string|null | ||
| */ | ||
| private function decodeFieldValue(mixed $value): array|bool|float|int|string|null | ||
| { | ||
| if ($value === null || $value === '' || $value === []) { | ||
| return null; | ||
| } | ||
|
|
||
| if (!\is_string($value)) { | ||
| return $value; | ||
| } | ||
|
|
||
| $decoded = \json_decode($value, true); | ||
|
|
||
| return \json_last_error() === \JSON_ERROR_NONE ? $decoded : $value; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.