Skip to content

Commit f8b2c43

Browse files
DimaSherstuykDariaHanzenkowebevtkisakova
authored
BB-24252: Create new API endpoint for recalculating order prices during order editing/creation (#39224)
- added new backend ordersubtotals resource - added an orderSubtotals relation to orders endpoint - added meta validate operation flag - added enable_validation entity api config option - added new rollback_validated_request event for customize_form_data action - updated add orderSubtotals logic for create and update api actions - added new --validate option for oro:api:dump command - added validate operation description for matched API resources - added reuse of an existing entity for response to the validated operation - updated orderSubtotals field collapse logic for the orders resource --------- Co-authored-by: Daria Hanzenko <[email protected]> Co-authored-by: Yefim Yevtushenko <[email protected]> Co-authored-by: Kateryna Isakova <[email protected]> Co-authored-by: Yefim Yevtushenko <[email protected]>
1 parent 53a6333 commit f8b2c43

File tree

7 files changed

+128
-0
lines changed

7 files changed

+128
-0
lines changed

api/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Find more information about Web API in the following topics:
4949
filters
5050
create-update-related-resources
5151
upsert-operation
52+
validate-operation
5253
client-requirements
5354
batch-api
5455
sync-batch-api

api/validate-operation.rst

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
.. _web-services-api--validate-operation:
2+
3+
Validating a Resource
4+
=====================
5+
6+
When creating or updating a primary API resource, you may need to validate it without persisting.
7+
8+
The validation is triggered in the |JSON:API: Meta Section| section via the "validate" option set to ``true``. The validation operation is supported by POST and PATCH requests.
9+
10+
How to Enable
11+
-------------
12+
13+
Resource that supports a validation should be marked with `enable_validation` flag in api.yml:
14+
15+
.. code-block:: yaml
16+
:caption: config/api.yml
17+
18+
api:
19+
// ... skipped
20+
entities:
21+
Oro\Bundle\OrderBundle\Entity\Order:
22+
documentation_resource: '@OroOrderBundle/Resources/doc/api/order.md'
23+
enable_validation: true # Enables validation operation
24+
// ... skipped
25+
26+
Then such resource would also contain the corresponding note in the API Sandbox. For example:
27+
28+
.. image:: /img/backend/api/validate_operation_note.png
29+
:alt: An example of the validate operation note
30+
31+
Implementation Details
32+
----------------------
33+
34+
Unlike the regular create or update action, the `validate` meta flag forces a database transaction rollback in the ``\Oro\Bundle\ApiBundle\Processor\CustomizeFormData\FlushDataHandler``. Keep in mind that the rollback also prevents asynchronous operations (e.g., those triggered by a message in MQ), including search reindexing, sending emails, etc. However, it cannot instantly reverse actions that have already been completed during the processing of an API request (for example, if a file is deleted in one of the API processors). Therefore, if you enable validation on an entity, you need to ensure that no irreversible operations are being carried out during the creation and update actions for that entity. If there are, it is important to either delay or make these operations asynchronous where possible.
35+
36+
Examples
37+
--------
38+
39+
An example of a POST validate requests:
40+
41+
**Request**
42+
43+
.. code-block:: http
44+
45+
POST /api/orders HTTP/1.1
46+
Content-Type: application/vnd.api+json
47+
Accept: application/vnd.api+json
48+
49+
**Request Body**
50+
51+
.. code-block:: json
52+
53+
{"data": {
54+
"type": "orders",
55+
"meta": {
56+
"validate": true
57+
},
58+
"attributes": {
59+
"identifier": "FR1012401Z",
60+
"poNumber": "CV032342USDD",
61+
}
62+
}
63+
}
64+
65+
An example of a PATCH validate requests:
66+
67+
**Request**
68+
69+
.. code-block:: http
70+
71+
PATCH /api/orders/1 HTTP/1.1
72+
Content-Type: application/vnd.api+json
73+
Accept: application/vnd.api+json
74+
75+
**Request Body**
76+
77+
.. code-block:: json
78+
79+
{"data": {
80+
"type": "orders",
81+
"id": "1",
82+
"meta": {
83+
"validate": true
84+
},
85+
"attributes": {
86+
"customerNotes": "Please, call before delivery"
87+
}
88+
}
89+
}
90+
91+
.. include:: /include/include-links-dev.rst
92+
:start-after: begin

backend/api/actions.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,7 @@ This action is executed when the following |ApiEvents| are dispatched:
885885
"pre_flush_data","This event is dispatched after the database transaction is open but before data are flushed into the database. Do not call EntityManager::persist() and EntityManager::remove() during handling of this event, use addAdditionalEntity() and addAdditionalEntityToRemove() methods of the context instead."
886886
"post_flush_data","This event is dispatched after data are successfully flushed into the database but before the database transaction is committed."
887887
"post_save_data","This event is dispatched after data are successfully flushed into the database, and the database transaction is committed. It can be used to perform some not crucial operations after data are saved into the database. It means that failure of these operations will not roll back data saved into the database."
888+
"rollback_validated_request","This event is dispatched before the database transaction is rolled back for API requests with a meta `validate` operation flag."
888889

889890
.. note:: All these events use the same context, so it can be used to share data between events.
890891

backend/api/commands.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,12 @@ To get all entities that support :ref:`the upsert operation <web-services-api--u
165165
166166
php bin/console oro:api:dump --upsert
167167
168+
To get all entities that support :ref:`the validate operation <web-services-api--validate-operation>`, use the ``--validate`` option:
169+
170+
.. code-block:: none
171+
172+
php bin/console oro:api:dump --validate
173+
168174
.. _oroapidebug:
169175

170176
oro:api:debug

backend/api/configuration.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,8 @@ The ``entities`` section describes the configuration of entities.
222222

223223
* **disable\_partial\_load** *boolean* - The flag indicates whether using of |Doctrine partial objects| is disabled. By default ``false``. When using partial objects, the ``HINT_FORCE_PARTIAL_LOAD`` query hint is used together with them to avoid loading unneeded foreign keys.
224224

225+
* **enable\_validation** *boolean* - The flag indicates whether it is possible to use a `validate` meta flag on create and update actions. By default ``false``. When the value is ``true``, API requests with a `validate` meta flag make transaction rollback instead of commit. Please be advised that enabling this option will also roll back other component operations, such as sending a mail or triggering a search reindex.For details, see :ref:`Configure Validate Operation <configure-validate-operation>`.
226+
225227
* **hints** *array* - The |Doctrine query hints|. Each item can be a string or an array with ``name`` and ``value`` keys. The string value is a short form of ``[name: hint name]``.
226228

227229
* **inner\_join\_associations** *string[]* - A list of entity associations for which INNER JOIN should be used instead of LEFT JOIN. Use the ``dot`` notation to specify a path to a nested association, e.g., ``user.organization``. Each element in the path must be equal to the name of the existing property of an entity. This option can be used to optimize SQL query that is used to load data if some associations are mandatory and cannot be empty.

backend/api/how-to.rst

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,32 @@ the :ref:`oro:api:dump <oroapidump-command>` command with ``--upsert`` option ca
680680
681681
php bin/console oro:api:dump --upsert
682682
683+
.. _configure-validate-operation:
684+
685+
Configure Validate Operation
686+
----------------------------
687+
688+
By default, :ref:`the validate operation <web-services-api--validate-operation>` is disabled for API resources.
689+
690+
If the validate operation is disabled for an API resource by default, but you need to enable it, use
691+
the "enable_validation" configuration option in `Resources/config/oro/api.yml` :
692+
693+
.. code-block:: yaml
694+
695+
api:
696+
entities:
697+
Acme\Bundle\DemoBundle\Entity\SomeEntity:
698+
enable_validation: true
699+
700+
.. note:: Please be aware that when validation requests are processed, the ``rollback_validated_request`` event is dispatched instead of ``post_flush_data`` and ``post_save_data``. Additionally, make sure that no extra actions, such as data indexing or sending emails, are performed when using validation requests. These extra actions can significantly impact the application's performance. For example, if data is indexed but not stored in the database, or if an email is sent for a record that was not created or updated in the database.
701+
702+
To check which entities support the validate operation, use
703+
the :ref:`oro:api:dump <oroapidump-command>` command with ``--validate`` option:
704+
705+
.. code-block:: none
706+
707+
php bin/console oro:api:dump --validate
708+
683709
.. _using-a-non-primary-key-to-identify-an-entity:
684710

685711
Using a Non-Primary Key to Identify an Entity
8.23 KB
Loading

0 commit comments

Comments
 (0)