Solved: Failed to Load API Definition in Magento 2 Swagger
While developing Magento 2 extensions, we often need to create a custom API to pass the data that has been fetched from the client. For example, while working with order tracking, we need to get the order id and email id from the customer and then pass it through API to perform further operations.
In order to maintain the standards of coding in API, we mostly prefer to add phpdoc in the file and if the editor is PHPStorm then we use the Ctrl+Alt+Ins shortcut key to do so.
The phpdoc contains code that seems like a comment but anyhow it affects the output in API creation.
For example, if you’ve not added the variable’s datatype in phpdoc of API, it generates an error of failed to load API definition in Magento 2 swagger,
Failed to Load API Definition
and displays in the swagger as below:
So, the solution to this error is to add the missing datatype in the code as mentioned below:
Solution to Failed to Load API definition in Magento 2 Swagger
When we add phpdoc in API creation it adds like:
1 2 3 4 5 |
/** * @param $orderId * @param $mailId * @return mixed */ |
Replace the above code with:
1 2 3 4 5 |
/** * @param int $orderId * @param mixed $mailId * @return mixed */ |
Done!
The solution is simple but difficult to identify!
If you have any doubts regarding this post, just mention them in the Comments section below.
I would be happy to help.
Feel free to share the solution with Magento Community via social media.
Thank You.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
4 Comments
Hello,
Please let me know where i need to change these
/**
* @param $orderId
* @param $mailId
* @return mixed
*/
File path ?
Hello Vipin,
You need to change the file path from Vendor\Module\Api folder before your custom method.
Thank You
Great one…this saved me a lot of time
Thank you
Hello Balu,
We’re glad that this solution helps you!
Thank You