nelmio / NelmioApiDocBundle

Generates documentation for your REST API from annotations
MIT License
2.23k stars 833 forks source link

Support for collections to output #306

Closed Drachenkaetzchen closed 6 years ago

Drachenkaetzchen commented 10 years ago

My API returns a list of objects. The annotation is as follows:

@ApiDoc(
description="Retrieves all SI Prefixes in the database",
output="PartKeepr\SiPrefixBundle\Entity\SiPrefix"
 )

This works fine, however, it doesn't indicate that PartKeepr\SiPrefixBundle\Entity\SiPrefix is an array. I could, of course, add a collection, but simply putting up a separate collection with proper type hints leads to lots of duplication. I would have expected to specify the output as JMSType specification, e.g. something like this:

@ApiDoc(
description="Retrieves all SI Prefixes in the database",
output="ArrayCollection<PartKeepr\SiPrefixBundle\Entity\SiPrefix>"
 )

However, this doesn't work. Is there any other way to specify that a collection is returned?

maphe commented 10 years ago

I add a complementary request : Suposing that we can now specify that the api returns a collection/array, how can we manage abstract entities using DiscriminatorMap ?

Example :

I have an abstract entity/document Vehicle extended by 2 classes Car and Truck. I want my api to return a list of vehicles, which can be one or another, how to specify it?

Supposition :

@ApiDoc( output="ArrayCollection<Namespace\To\Entity\Car|Namespace\To\Entity\Truck>" )

chrisapl commented 10 years ago

+1

mtotheikle commented 10 years ago

+1 In my case I have an abstract pager object that has information for pagination and then a results array. I need a way to document the results array for a particular endpoint will be a specific type

willdurand commented 10 years ago

Anyone to work on this feature?

lgoral commented 10 years ago

Hi My question is if that is correct, and create PR?

In my project i created service

    mybundle.api_doc.extractor.custom_parser:
        class: My\Bundle\Parser\ApiDocOutputArray
        arguments: ['@nelmio_api_doc.parser.jms_metadata_parser']
        tags:
            - { name: nelmio_api_doc.extractor.parser, priority: 2 }

and code which read output parameter

output="ArrayCollection<Namespace\To\Entity\Car>"
use Nelmio\ApiDocBundle\Parser\JmsMetadataParser;
use Nelmio\ApiDocBundle\Parser\ParserInterface;

class ApiDocOutputArray implements ParserInterface
{

    /**
     * @var JmsMetadataParser
     */
    private $jmsMetadataParser;

    public function __construct(JmsMetadataParser $jmsMetadataParser)
    {
        $this->jmsMetadataParser = $jmsMetadataParser;
    }

    /**
     * {@inheritdoc}
     */
    public function supports(array $item)
    {
        list($className, $type) = $this->getClassType($item);

        if (empty($className) || empty($type)) {
            return false;
        }

        $item['class'] = $className;

        return $this->jmsMetadataParser->supports($item);
    }

    /**
     * {@inheritdoc}
     */
    public function parse(array $item)
    {
        list($className, $type) = $this->getClassType($item);

        if (empty($className) || empty($type)) {
            return false;
        }

        $exp = explode("\\", $className);

        $item['class'] = $className;

        $returnData = array('dataType' => $type,
            'required' => true,
            'description' => sprintf("%s of objects (%s)", $type, end($exp)),
            'readonly' => false,
            'children' => $this->jmsMetadataParser->parse($item)
        );

        return array('[]' => $returnData);
    }

    /**
     * @param array $item
     *
     * @return array
     */
    private function getClassType(array $item)
    {
        $className = $type = '';
        if (preg_match('/(.+)\<(.+)\>/', $item['class'], $match)) {
            $className = $match[2];
            $type = $match[1];
        }

        return array($className, $type);
    }
}
mbedna commented 10 years ago

+1

darles commented 10 years ago

+1

noquepoaqui commented 10 years ago

+1

bigfoot90 commented 10 years ago

+1

andrewetter commented 10 years ago

+1

salcam commented 10 years ago

+1

fmeynard commented 10 years ago

+1

willdurand commented 10 years ago

people... -_-

lgoral commented 10 years ago

What about PR?

jaymecd commented 10 years ago

+1 it's very important

dev-ish commented 10 years ago

+1

pleerock commented 10 years ago

+1

adrienbrault commented 9 years ago

You can use https://github.com/schmittjoh/serializer/blob/master/src/JMS/Serializer/TypeParser.php#L26 ( https://github.com/schmittjoh/parser-lib/blob/master/src/JMS/Parser/AbstractParser.php#L44 ) to parse the type

magnetik commented 9 years ago

+1

cbrunnkvist commented 9 years ago

the same :+1:

psliwa commented 9 years ago

+1 :+1:

foaly-nr1 commented 9 years ago

+1

adnedelcu commented 8 years ago

+1

duncangrist commented 8 years ago

How is this requirement different from what was already merged here: https://github.com/nelmio/NelmioApiDocBundle/pull/469

?

dbu commented 6 years ago

i am closing old tickets without activity. feel free to open a new issue if needed.

version 3 should be able to do all that with the swagger php support.