0

Im using Magento 2.1.3 and PHP 7.0.8.

I would like to test some Magentos 2 import api.

When I'm trying to connect to swagger by http://localhost/swagger error 500 is received:
enter image description here

I'm also trying to connect to http://localhost/rest/default/schema?services=all
and return:
enter image description here

I am worried about that it doesn't works.
However I am able to connect via rest to eg. http://localhost/index.php/rest/V1/integration/admin/token and get user token.

I also tried to install https://www.npmjs.com/package/magento2-swagger
result:

root@2d426841549e:/var/www/html# npm install magento2-swagger --save
npm WARN saveError ENOENT: no such file or directory, open '/var/www/html/package.json'
/var/www/html
`-- [email protected]  

npm WARN enoent ENOENT: no such file or directory, open '/var/www/html/package.json'
npm WARN html No description
npm WARN html No repository field.
npm WARN html No README data
npm WARN html No license field.  
1
  • I think you have issue in you custom module. Dec 3, 2018 at 9:53

5 Answers 5

1

Swagger try to generate documentation from class annotation.

You use array as API type.

It is not support by Magento swagger implementation.

You can try to use anyType as a workaround or create your custom type.

Reference :

\Magento\Framework\Reflection\TypeProcessor

Allowed base type :

/**#@+
 * Normalized type constants
 */
const NORMALIZED_STRING_TYPE = 'string';
const NORMALIZED_INT_TYPE = 'int';
const NORMALIZED_FLOAT_TYPE = 'float';
const NORMALIZED_DOUBLE_TYPE = 'double';
const NORMALIZED_BOOLEAN_TYPE = 'boolean';
const NORMALIZED_ANY_TYPE = 'anyType';

You can also add array type but you need to do some custom development.

If your website is different than default, you will need to change the form URL hidden value to match your website.

9
  • Is there any easy way to find api's location using array type? I can not find it manually :c Feb 20, 2017 at 9:05
  • Try to search array[] in API folders ? Did you create custom API ? Do you install custom modules ? Search there. Feb 20, 2017 at 9:15
  • Add log inside the error function near \Magento\Webapi\Model\Config\ClassReflector::extractMethodData and \Magento\Webapi\Model\Config\ClassReflector::reflectClassMethods to log which class is responsible of the error. Feb 20, 2017 at 9:16
  • oh crap. There are a lot of array type in Core Magento Api... Feb 20, 2017 at 9:29
  • I do not think so! I do not have to change Magento core to make swagger works. Make sure to debug the class, to see exactly what is the class responsible of the error. Feb 20, 2017 at 10:47
1

For anyone encountering the same issue as I have where the error was: <message>The "this" class doesn't exist and the namespace must be specified. Verify and try again.</message> I was only able to find the issue debuggin this file /vendor/magento/framework/Reflection/TypeProcessor.php addin the following at around line 224:

$typeIs = $returnMetadata['type'];
            if ($typeIs === 'this') {
                // adding my custom logger here:
            $writer = new \Zend_Log_Writer_Stream(BP . '/var/log/customApi.log');
            $this->myLogger = new \Zend_Log();
            $this->myLogger->addWriter($writer);
                $this->myLogger->info('methodName: '.$methodReflection->getName());
                $this->myLogger->info(print_r($typeIs, true));
            }

Only then I was able to find which method was the culprit. Magento should improve this and print more info at this issue on services pages such as this: https://yourmagento.com/rest/all/schema?services=all

0

looks like some of your api declare parameter/return type as array[] that not supported by Magento.

Please fix error and swagger will start to work

0

Solution:

  • Delete vendor, delete composer.lock.
  • do composer install, setup:upgrade, setup:di:compile, indexer:reindex

And it start works

0

So for anyone running into this and totally at loss which module provider has not read the docs, here are a few tips&tricks.

I was confronted with the very annoying Internal Server Error above (magento 2.4.4) and found in the developer tools the underlying error

The "array" class doesn't exist and the namespace must be specified. Verify and try again.

enter image description here

This means someone has provided API functionality that returns a mixed or undefined array which is not allowed. In my case it was Addwish_Awext and I found this by adding the following logging to the above mentioned vendor\magento\module-webapi\Model\Config\ClassReflector.php

public function __construct(TypeProcessor $typeProcessor)
{
    $this->_typeProcessor = $typeProcessor;
    // adding my custom logger here:
    $writer = new \Zend_Log_Writer_Stream(BP . '/var/log/customApi.log');
    $this->myLogger = new \Zend_Log();
    $this->myLogger->addWriter($writer);
}
public function extractMethodData(MethodReflection $method)
{
    $methodData = ['documentation' => $this->extractMethodDescription($method), 'interface' => []];
    /** @var \Laminas\Code\Reflection\ParameterReflection $parameter */
    foreach ($method->getParameters() as $parameter) {
        $parameterData = [
            'type' => $this->_typeProcessor->register($this->_typeProcessor->getParamType($parameter)),
            'required' => !$parameter->isOptional(),
            'documentation' => $this->_typeProcessor->getParamDescription($parameter),
        ];
        $methodName = $method->getName();
        $typeIs = $this->_typeProcessor->register($this->_typeProcessor->getParamType($parameter));
        // try to find something that returns array
        $pos = strpos($typeIs, 'array');
        if ($pos !== false) {
            $this->myLogger->info('methodName: '.$methodName);
            $this->myLogger->info(print_r($typeIs, true));
        }
        if ($parameter->isOptional()) {
            $parameterData['default'] = $parameter->getDefaultValue();
        }
        $methodData['interface']['in']['parameters'][$parameter->getName()] = $parameterData;
    }
    $returnType = $this->_typeProcessor->getGetterReturnType($method);
    // try to find something that returns array
    $pos = strpos($returnType['type'], 'array');
    if ($pos !== false) {
        $this->myLogger->info('(getter) methodName:'.$method->getName());
        $this->myLogger->info(print_r($method, true));
    }
    if ($returnType['type'] != 'void' && $returnType['type'] != 'null') {
    ....

Opening the CustomApi.log I found:

(getter) methodName: getState Laminas\Code\Reflection\MethodReflection Object ( [name] => getState [class] => Addwish\Awext\Api\Service\StateInterface )

Disabling the module, clear cache and do a full frontend deploy and... swagger works again.

2
  • this helped me but my issue is <message>The "this" class doesn't exist and the namespace must be specified. Verify and try again.</message> Could not find this anywhere in the log by the way I've remove if condition so it print everything. I'm not sure where this is coming from any other ideas I can try? Aug 15, 2023 at 20:04
  • @JulianoVargas try setting a breakpoint at the try/catch in \Magento\Framework\Reflection\TypeProcessor::register. When it triggers, you can find the method name and the class that probably claims @returns this in its PHPdoc comment.
    – bdbdbd
    Oct 6, 2023 at 12:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.