dimaportenko / mma-customapi

Magento 2 add custom rest api endpoints
18 stars 14 forks source link

Extension breask Swagger UI generation #6

Open 0xMatt opened 4 years ago

0xMatt commented 4 years ago

I'm no longer able to access swagger because of two errors

  1. Message: The service interface name "MMA\CustomApi\Model\PaymentTokenManagement" is invalid.
  2. After fixing this endpoint on my local copy, another error with @return array causes the APIgen to fail, changing @return mixed fixes the issue
dimaportenko commented 4 years ago

@0xMatt After fixing this endpoint on my local copy.

  1. What was the issue and how did you fix it?
  2. What is Magento version you are using?
0xMatt commented 4 years ago
  1. The issue is:
    <route url="/V1/mma/me/vault/items" method="GET">
        <service class="MMA\CustomApi\Model\PaymentTokenManagement" method="getListByCustomerId"/>
        <resources>
            <resource ref="self" />
        </resources>
        <data>
            <parameter name="customerId" force="true">%customer_id%</parameter>
        </data>
    </route>

    You are pointing to a methodrather than an interface.

There should be a method defined for this within MMA\CustomApi\Api\PaymentTokenManagementInterface and it should be mapped in di.xml like the other methods have.

Also, you are using param docblocks of @param array or @return array, those actually need to be changed to mixed, rather than array. Fixing those allows Swagger UI to properly generate. I believe I had to fix 7 occurrences of this.

  1. Magento version 2.3.5
dimaportenko commented 4 years ago

From https://devdocs.magento.com/guides/v2.3/coding-standards/technical-guidelines.html#1-basic-programming-principles

6.4.3.1. Strict typing is enforced for Service and Data interfaces located under MyCompany/MyModuleApi/Api. Only the following types are allowed:

- Scalar types: string (including Date and DateTime); int; float; boolean

- Data interfaces

- One-dimensional indexed arrays of scalars or data interfaces: for example string[], \MyCompany\MyModuleApi\Api\Data\SomeInterface[]. Hash maps (associative arrays) are not supported.

- Nullable scalars or data interfaces: for example string|null. Using just null is prohibited.

- void

So using mixed isn't allowed and I remember it causes some issues on one of my projects. I don't remember exactly what it was. Also, I'm using array more like a hack to avoid spending time on the creation of a proper interface. So feel free to use mixed if it works for you, just be aware it can cause something.

dimaportenko commented 4 years ago
You are pointing to a methodrather than an interface.

There should be a method defined for this within MMA\CustomApi\Api\PaymentTokenManagementInterface and it should be mapped in di.xml like the other methods have.

If you like to create PR for this I'll appreciate it