Skip to content
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

IBX-8947: Add 2 cart REST routes; update Cart examples #2501

Merged
merged 12 commits into from
Dec 3, 2024
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
{
"Cart": {
"_media-type": "application/vnd.ibexa.api.Cart+json",
"id": 12,
"identifier": "fafccc4b-cce1-40d7-8d23-29ad41638581",
"_media-type": "application\/vnd.ibexa.api.Cart+json",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

@adriendupuis adriendupuis Dec 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mnocon

I checked on https://www.json.org/json-en.html and further on https://ecma-international.org/publications-and-standards/standards/ecma-404/. The standard's PDF states:

All code points may
be placed within the quotation marks except for the code points that must be escaped: quotation mark
(U+0022), reverse solidus (U+005C), and the control characters U+0000 to U+001F.
[…]
\/ represents the solidus character (U+002F).

So escaping the slash (a.k.a solidus) is optional; it doesn't matter.

% echo '{ "slash": "\/" }' | jq                                                                                     
{
  "slash": "/"
}

jq understands it but considers it's not worth keeping it.

We could status for consistency and open a new PR about this. I would vote for removing them all.

"id": 1,
"identifier": "e79b5dba-c701-4c64-8032-73719a513e50",
"name": "Default",
"createdAt": "2023-10-12T10:02:51+00:00",
"updatedAt": "2023-10-12T10:02:51+00:00",
"createdAt": "2024-09-26T09:16:56+00:00",
"updatedAt": "2024-09-26T09:16:56+00:00",
"owner": {
"_media-type": "application/vnd.ibexa.api.User+json",
"_href": "/api/ibexa/v2/user/users/14"
"_media-type": "application\/vnd.ibexa.api.User+json",
"_href": "\/api\/ibexa\/v2\/user\/users\/10"
},
"Currency": {
"_media-type": "application/vnd.ibexa.api.Currency+json",
"_media-type": "application\/vnd.ibexa.api.Currency+json",
"id": 2,
"code": "EUR",
"subunits": 2,
"enabled": true
},
"entries": []
"entries": [],
"context": {
"foo": "bar"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<Cart media-type="application/vnd.ibexa.api.Cart+xml">
<id>1</id>
<identifier>e67d8f4c-95b5-423f-89d8-b2b81c37609b</identifier>
<name>Default</name>
<createdAt>2024-09-26T09:16:56+00:00</createdAt>
<updatedAt>2024-09-26T09:16:56+00:00</updatedAt>
<owner media-type="application/vnd.ibexa.api.User+xml" href="/api/ibexa/v2/user/users/10"/>
<Currency media-type="application/vnd.ibexa.api.Currency+xml">
<id>2</id>
<code>EUR</code>
<subunits>2</subunits>
<enabled>1</enabled>
</Currency>
</Cart>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<CartCreate>
<name>Default</name>
<currencyCode>EUR</currencyCode>
</CartCreate>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"Token": {
"_media-type": "application\/vnd.ibexa.api.Token+json",
"value": "7b19308.RiDPaorwmJRyQoGajJiLMp843r30WaW-PH1CeH4oQyU.dkOXWOuFrvBfDPXY1t27YMx0hvyFH8fPUwoDNRl_D0sWDb4O3bb-20Q32A"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<Token media-type="application/vnd.ibexa.api.Token+xml">
<value>eff5.w9r2Ws_zpjcopNJ8FRNVaplGlx19y4Mp2QP-eQWZDKc.-pe4Pam133xKzKQZLCdiIMkj2EgQ5rMdtlGTEEPPaOyHv6Y1iKTsZ2_OtQ</value>
</Token>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"CartConstraintViolationList": {
"_media-type": "application\/vnd.ibexa.api.CartConstraintViolationList+json",
"violations": []
Copy link
Contributor Author

@adriendupuis adriendupuis Sep 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't manage to insert invalid data on purpose.

I tried to add a product with no stock, it's added without any error. I thought https://github.com/ibexa/cart/blob/main/src/lib/Validation/Constraint/ProductAvailableValidator.php would complain, but no. On the storefront, the product is indicated as Out of stock but if I edit the HTML and remove the disabled attribute from the "Add to cart" button, I can add it to the cart without error. It seems there is no product availability around there.

I tried to add a product/entry called 'test' to match the example without having configuring proper TVA for the region.

curl "$baseUri/cart/$identifier/entry" \
  -b anonymous_cookies.txt -H "X-CSRF-Token: $anonymous_token" \
  -H "Content-Type: $mimeTypePrefix.CartEntryAdd+$format" \
  --data "@$restRefDir/input/examples/cart/entry/POST/CartEntryAdd.json.example" \
;

The cart is validated on the fly and I get the following error:

<?xml version="1.0" encoding="UTF-8"?>
<ErrorMessage media-type="application/vnd.ibexa.api.ErrorMessage+xml">
 <errorCode>406</errorCode>
 <errorMessage>Not Acceptable</errorMessage>
 <errorDescription>Argument '$entryAddStruct' is invalid: Product &quot;test&quot; has no VAT Category set.</errorDescription>
 <trace>…</trace>
 <file>/var/www/html/vendor/ibexa/cart/src/lib/Service/CartService.php</file>
 <line>239</line>
</ErrorMessage>

I tried to fix the error and reintroduce it later but fail. I had error 500 on every page of the DXP for a region VAT not found.

I stopped my test there.

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<CartConstraintViolationList media-type="application/vnd.ibexa.api.CartConstraintViolationList+xml"/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"CartView": {
"_media-type": "application\/vnd.ibexa.api.CartView+json",
"identifier": "loadUserCarts",
"Result": {
"_media-type": "application\/vnd.ibexa.api.ViewResult+json",
"count": 1,
"CartList": {
"_media-type": "application\/vnd.ibexa.api.CartList+json",
"Cart": [
{
"_media-type": "application\/vnd.ibexa.api.Cart+json",
"id": 1,
"identifier": "e79b5dba-c701-4c64-8032-73719a513e50",
"name": "Default",
"createdAt": "2024-09-26T09:16:56+00:00",
"updatedAt": "2024-09-26T09:16:56+00:00",
"owner": {
"_media-type": "application\/vnd.ibexa.api.User+json",
"_href": "\/api\/ibexa\/v2\/user\/users\/10"
},
"Currency": {
"_media-type": "application\/vnd.ibexa.api.Currency+json",
"id": 2,
"code": "EUR",
"subunits": 2,
"enabled": true
},
"entries": [],
"context": {
"foo": "bar"
}
}
]
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<CartView media-type="application/vnd.ibexa.api.CartView+xml">
<identifier>loadUserCarts</identifier>
<Result media-type="application/vnd.ibexa.api.ViewResult+xml">
<count>1</count>
<CartList media-type="application/vnd.ibexa.api.CartList+xml">
<Cart media-type="application/vnd.ibexa.api.Cart+xml">
<id>1</id>
<identifier>e67d8f4c-95b5-423f-89d8-b2b81c37609b</identifier>
<name>Default</name>
<createdAt>2024-09-26T09:16:56+00:00</createdAt>
<updatedAt>2024-09-26T09:16:56+00:00</updatedAt>
<owner media-type="application/vnd.ibexa.api.User+xml" href="/api/ibexa/v2/user/users/10"/>
<Currency media-type="application/vnd.ibexa.api.Currency+xml">
<id>2</id>
<code>EUR</code>
<subunits>2</subunits>
<enabled>1</enabled>
</Currency>
</Cart>
</CartList>
</Result>
</CartView>
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"CartQuery": {
"offset": 0,
"limit": 10,
"ownerId": 123
"ownerId": 10
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<CartViewInput>
<identifier>loadUserCarts</identifier>
<CartQuery>
<offset>0</offset>
<limit>10</limit>
<ownerId>10</ownerId>
</CartQuery>
</CartViewInput>
42 changes: 42 additions & 0 deletions docs/api/rest_api/rest_api_reference/input/ibexa-cart.raml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ post:
body:
application/vnd.ibexa.api.CartCreate+xml:
type: CartCreate
example: !include examples/cart/POST/CartCreate.xml.example
application/vnd.ibexa.api.CartCreate+json:
type: CartCreateWrapper
example: !include examples/cart/POST/CartCreate.json.example
Expand All @@ -24,6 +25,7 @@ post:
body:
application/vnd.ibexa.api.Cart+xml:
type: Cart
example: !include examples/cart/POST/Cart.xml.example
application/vnd.ibexa.api.Cart+json:
type: CartWrapper
example: !include examples/cart/POST/Cart.json.example
Expand All @@ -47,6 +49,7 @@ post:
body:
application/vnd.ibexa.api.CartViewInput+xml:
type: CartViewInput
example: !include examples/cart/view/CartViewInput.xml.example
application/vnd.ibexa.api.CartViewInput+json:
type: CartViewInputWrapper
example: !include examples/cart/view/CartViewInput.json.example
Expand All @@ -58,6 +61,7 @@ post:
type: CartView
application/vnd.ibexa.api.CartView+json:
type: CartViewWrapper
example: !include examples/cart/view/CartView.json.example
/{identifier}:
get:
displayName: Get Cart
Expand All @@ -74,6 +78,7 @@ post:
body:
application/vnd.ibexa.api.Cart+xml:
type: Cart
example: !include examples/cart/POST/Cart.xml.example
application/vnd.ibexa.api.Cart+json:
type: CartWrapper
example: !include examples/cart/POST/Cart.json.example
Expand Down Expand Up @@ -165,3 +170,40 @@ post:
description: Error - The user is not authorized to read this Cart.
404:
description: Error - Cart not found.
/validate:
post:
displayName: Cart Validation
description: Validates a Cart against constraints
headers:
Accept:
description: If set, the Cart Constraint Violations are returned in XML or JSON format.
example: |
application/vnd.ibexa.api.CartConstraintViolationList+xml
application/vnd.ibexa.api.CartConstraintViolationList+json
responses:
200:
body:
application/vnd.ibexa.api.CartConstraintViolationList+xml:
type: CartConstraintViolationList
example: !include examples/cart/identifier/validate/CartConstraintViolationList.xml.example
application/vnd.ibexa.api.CartConstraintViolationList+json:
type: CartConstraintViolationListWrapper
example: !include examples/cart/identifier/validate/CartConstraintViolationList.json.example
/authorize:
post:
displayName: Cart Authorization
description: Returns the CSRF token required by anonymous user to interact with their own cart.
headers:
Accept:
example: |
application/vnd.ibexa.api.Token+xml
application/vnd.ibexa.api.Token+json
responses:
200:
body:
application/vnd.ibexa.api.Token+xml:
type: Token
example: !include examples/cart/authorize/Token.xml.example
application/vnd.ibexa.api.CartConstraintViolationList+json:
type: Token
example: !include examples/cart/authorize/Token.json.example
30 changes: 29 additions & 1 deletion docs/api/rest_api/rest_api_reference/input/ibexa-types.raml
Original file line number Diff line number Diff line change
Expand Up @@ -3593,7 +3593,7 @@ CartListViewResult:
type: object
properties:
count: integer
Result: CartList
CartList: CartList

CartList:
description: 'This class represents a cart list.'
Expand All @@ -3614,6 +3614,34 @@ CartMetadataUpdate:
names: Value[]
descriptions: Value[]

CartConstraintViolationListWrapper:
description: 'JSON object with only a CartConstraintViolationList property.'
type: object
properties:
CartConstraintViolationList: CartConstraintViolationList

CartConstraintViolationList:
description: 'This class represents a list of cart constraint violations.'
type: object
properties:
violations:
type: array
items: CartConstraintViolation
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a CartConstraintViolationWrapper is needed but I didn't manage to product constraint violation to study it.


CartConstraintViolation:
description: 'This class represents a cart constraint violation.'
type: object
properties:
code: string
propertyPath: string
message: string

Token:
description: 'This class represents a CSRF token'
type: object
properties:
value: string

CatalogWrapper:
description: 'JSON object with only a Catalog property.'
type: object
Expand Down
Loading
Loading