[SOLVED] Error when struct has only array type

I have a truct with array types only. When I declare a mapping, it says: ā€œInternal or recursive type is not allowed for public state variablesā€

// SPDX-License-Identifier: MIT
// using 0.7.0+commit.9e61f92b
pragma solidity >=0.6.0 <0.8.0;

import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/solc-0.7/contracts/token/ERC721/ERC721.sol";

contract MyNFT is ERC721 {

    mapping (uint256 => Attributes) public tokenId2Attributes; // error: Internal or recursive type is not allowed for public state variables

    struct Attributes {
        string name; // comment out -> error on the line above
        uint256[] keys;
        uint256[] values;
    }

    constructor() ERC721("MyNFT", "MNFT"){
    }

}
1 Like

I found the solution.
Because when a variable is defined as public, the getter method is generated. For the struct that content a primitive property like uint, string, the getter will return this property, but not whole the struct because struct is not supported to be returned outside. When we remove ā€œnameā€ property, the getter have nothing to return so that why the error occurs.
The solution is declare as internal or private and we have 2 customer getters, one for keys and one for values because array can be returned outside but the struct is not.

3 Likes