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

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

adriendupuis
Copy link
Contributor

@adriendupuis adriendupuis commented Sep 20, 2024

Question Answer
JIRA Ticket IBX-8947
Versions 4.6, master
Edition All

Add two routes:

Add XML examples and update JSON examples.

Examples generation

Done with

  • Commerce 4.6.11
  • DDEV
  • ibexa/cart#97
set -x;

host='http://ddev-ibexa-tmp.ddev.site:8080';
baseUri="$host/api/ibexa/v2";
mimeTypePrefix='application/vnd.ibexa.api';
restRefDir='docs/api/rest_api/rest_api_reference';

for format in 'json' 'xml'; do

parser='jq';
if [ 'xml' = "$format" ]; then
  parser='xq';
fi;

curl "$baseUri/cart/authorize" \
  -c anonymous_cookies.txt \
  -X 'POST' \
  -H "Accept: $mimeTypePrefix.Token+$format" \
> $restRefDir/input/examples/cart/authorize/Token.$format.example;
anonymous_token=`cat $restRefDir/input/examples/cart/authorize/Token.$format.example | $parser -r '.Token.value'`;
if [ 'json' = "$format" ]; then
  echo '' >> $restRefDir/input/examples/cart/authorize/Token.$format.example;
fi;

curl "$baseUri/cart" \
  -b anonymous_cookies.txt -H "X-CSRF-Token: $anonymous_token" \
  -H "Content-Type: $mimeTypePrefix.CartCreate+$format" \
  --data "@$restRefDir/input/examples/cart/POST/CartCreate.$format.example" \
  -H "Accept: application/$format" \
> $restRefDir/input/examples/cart/POST/Cart.$format.example;
identifier=`cat $restRefDir/input/examples/cart/POST/Cart.$format.example | $parser -r '.Cart.identifier'`;
if [ 'json' = "$format" ]; then
  echo '' >> $restRefDir/input/examples/cart/POST/Cart.$format.example;
fi;

curl "$baseUri/cart/$identifier" \
  -b anonymous_cookies.txt -H "X-CSRF-Token: $anonymous_token" \
  -H "Accept: $mimeTypePrefix.Cart+$format" \
> $restRefDir/input/examples/cart/identifier/Cart.$format.example;
if [ 'json' = "$format" ]; then
  echo '' >> $restRefDir/input/examples/cart/identifier/Cart.$format.example;
fi;
if [ '' = "$(diff $restRefDir/input/examples/cart/POST/Cart.$format.example $restRefDir/input/examples/cart/identifier/Cart.$format.example)" ]; then
  rm $restRefDir/input/examples/cart/identifier/Cart.$format.example;
fi;

curl "$baseUri/cart/view" \
  -b anonymous_cookies.txt -H "X-CSRF-Token: $anonymous_token" \
  --data "@$restRefDir/input/examples/cart/view/CartViewInput.$format.example" \
  -H "Content-Type: $mimeTypePrefix.CartViewInput+$format" \
  -H "Accept: $mimeTypePrefix.CartView+$format" \
> $restRefDir/input/examples/cart/view/CartView.$format.example;
if [ 'json' = "$format" ]; then
  echo '' >> $restRefDir/input/examples/cart/view/CartView.$format.example;
fi;

curl "$baseUri/cart/$identifier/validate" \
  -b anonymous_cookies.txt -H "X-CSRF-Token: $anonymous_token" \
  -X 'POST' \
  -H "Accept: $mimeTypePrefix.CartConstraintViolationList+$format" \
> $restRefDir/input/examples/cart/identifier/validate/CartConstraintViolationList.$format.example;
if [ 'json' = "$format" ]; then
  echo '' >> $restRefDir/input/examples/cart/identifier/validate/CartConstraintViolationList.$format.example;
fi;

curl "$baseUri/cart/$identifier" \
  -b anonymous_cookies.txt -H "X-CSRF-Token: $anonymous_token" \
  -X 'DELETE' \
;

rm anonymous_cookies.txt;

done;
exit 0;

Checklist

  • Text renders correctly
  • Text has been checked with vale
  • Description metadata is up to date
  • Redirects cover removed/moved pages
  • Code samples are working
  • PHP code samples have been fixed with PHP CS fixer
  • Added link to this PR in relevant JIRA ticket or code PR

/api/ibexa/v2/cart/{identifier}/validate
/api/ibexa/v2/cart/authorize
@adriendupuis adriendupuis changed the title Add missing cart REST routes IBX-8947: Add missing cart REST routes Sep 25, 2024
@adriendupuis adriendupuis changed the title IBX-8947: Add missing cart REST routes IBX-8947: Add 2 cart REST routes; update Cart examples Sep 26, 2024
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not able to find a way to add a context like JSON does.

<CartCreate>
    <name>Default</name>
    <currencyCode>EUR</currencyCode>
    <context>
        <foo>bar</foo>
    </context>
</CartCreate>
?xml version="1.0" encoding="UTF-8"?>
<ErrorMessage media-type="application/vnd.ibexa.api.ErrorMessage+xml">
 <errorCode>500</errorCode>
 <errorMessage>Internal Server Error</errorMessage>
 <errorDescription>Array to string conversion</errorDescription>
 <trace>…</trace>
 <file>/var/www/html/vendor/ibexa/rest/src/lib/Output/Generator/Xml.php</file>
 <line>169</line>
</ErrorMessage>

{
"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.

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.

@adriendupuis adriendupuis marked this pull request as ready for review September 27, 2024 14:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant