Skip to content
This repository was archived by the owner on Feb 2, 2023. It is now read-only.

Commit d4057bc

Browse files
wbloszykphansys
andcommitted
Update conflict constraint for "nelmio/api-doc-bundle" in order to allow version 3
Thanks for your help Co-authored-by: Javier Spagnoletti <phansys@gmail.com>
1 parent a8f1849 commit d4057bc

24 files changed

+1067
-186
lines changed

UPGRADE-3.x.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
UPGRADE 3.x
22
===========
33

4+
UPGRADE FROM 3.x to 3.x
5+
=======================
6+
7+
### Support for NelmioApiDocBundle > 3.6 is added
8+
9+
Since version 3.x, support for nelmio/api-doc-bundle > 3.6 is added. Controllers for NelmioApiDocBundle v2 were moved under `Sonata\NewsBundle\Controller\Api\Legacy\` namespace and controllers for NelmioApiDocBundle v3 were added as replacement. If you extend them, you must ensure they are using the corresponding inheritance.
10+
411
UPGRADE FROM 3.13 to 3.14
512
=========================
613

docs/reference/api.rst

+18-9
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,20 @@ Here's the configuration we used, you may adapt it to your needs:
3030
enabled: true
3131
validate: true
3232
33+
.. code-block:: yaml
34+
35+
# config/packages/framework.yaml
36+
37+
framework:
38+
error_controller: 'FOS\RestBundle\Controller\ExceptionController::showAction'
39+
40+
.. code-block:: yaml
41+
42+
# config/packages/twig.yaml
43+
44+
twig:
45+
exception_controller: null
46+
3347
.. code-block:: yaml
3448
3549
# config/packages/sensio_framework_extra.yaml
@@ -62,24 +76,19 @@ In order to activate the ReST API, you'll also need to add this to your routing:
6276
6377
# config/routes.yaml
6478
65-
NelmioApiDocBundle:
66-
resource: "@NelmioApiDocBundle/Resources/config/routing.yml"
67-
prefix: /api/doc
68-
6979
sonata_api_news:
70-
type: rest
71-
prefix: /api
72-
resource: '@SonataNewsBundle/Resources/config/routing/api.xml'
80+
prefix: /api/news
81+
resource: "@SonataNewsBundle/Resources/config/routing/api_nelmio_v3.xml"
7382
7483
Serialization
7584
-------------
7685

77-
We're using `JMSSerializerBundle <https://jmsyst.com/bundles/JMSSerializerBundle>`_'s serializations groups to customize the inputs and outputs.
86+
We're using `JMSSerializerBundle <https://jmsyst.com/bundles/JMSSerializerBundle>`_'s serialization groups to customize the inputs and outputs.
7887

7988
The taxonomy is as follows:
8089

8190
* ``sonata_api_read`` is the group used to display entities
8291
* ``sonata_api_write`` is the group used for input entities (when used instead of forms)
8392

84-
If you wish to customize the outputted data, feel free to setup your own serialization options
93+
If you wish to customize the outputted data, feel free to set up your own serialization options
8594
by configuring `JMSSerializer <https://jmsyst.com/libs/serializer>`_ with those groups.

src/Controller/Api/CommentController.php

+31-21
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515

1616
use FOS\RestBundle\Controller\Annotations as REST;
1717
use FOS\RestBundle\View\View;
18-
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
18+
use Nelmio\ApiDocBundle\Annotation\Model;
19+
use Nelmio\ApiDocBundle\Annotation\Operation;
1920
use Sonata\NewsBundle\Model\Comment;
2021
use Sonata\NewsBundle\Model\CommentManagerInterface;
22+
use Swagger\Annotations as SWG;
2123
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
2224

2325
/**
@@ -41,16 +43,18 @@ public function __construct(CommentManagerInterface $commentManager)
4143
/**
4244
* Retrieves a specific comment.
4345
*
44-
* @ApiDoc(
45-
* resource=true,
46-
* requirements={
47-
* {"name"="id", "dataType"="string", "description"="Comment identifier"}
48-
* },
49-
* output={"class"="Sonata\NewsBundle\Model\Comment", "groups"={"sonata_api_read"}},
50-
* statusCodes={
51-
* 200="Returned when successful",
52-
* 404="Returned when comment is not found"
53-
* }
46+
* @Operation(
47+
* tags={"/api/news/comments/{id}"},
48+
* summary="Retrieves a specific comment.",
49+
* @SWG\Response(
50+
* response="200",
51+
* description="Returned when successful",
52+
* @SWG\Schema(ref=@Model(type="Sonata\NewsBundle\Model\Comment"))
53+
* ),
54+
* @SWG\Response(
55+
* response="404",
56+
* description="Returned when comment is not found"
57+
* )
5458
* )
5559
*
5660
* @REST\View(serializerGroups={"sonata_api_read"}, serializerEnableMaxDepthChecks=true)
@@ -69,15 +73,21 @@ public function getCommentAction($id)
6973
/**
7074
* Deletes a comment.
7175
*
72-
* @ApiDoc(
73-
* requirements={
74-
* {"name"="id", "dataType"="string", "description"="Comment identifier"}
75-
* },
76-
* statusCodes={
77-
* 200="Returned when comment is successfully deleted",
78-
* 400="Returned when an error has occurred while comment deletion",
79-
* 404="Returned when unable to find comment"
80-
* }
76+
* @Operation(
77+
* tags={"/api/news/comments/{id}"},
78+
* summary="Deletes a comment.",
79+
* @SWG\Response(
80+
* response="200",
81+
* description="Returned when comment is successfully deleted"
82+
* ),
83+
* @SWG\Response(
84+
* response="400",
85+
* description="Returned when an error has occurred while comment deletion"
86+
* ),
87+
* @SWG\Response(
88+
* response="404",
89+
* description="Returned when unable to find comment"
90+
* )
8191
* )
8292
*
8393
* @param string $id Comment identifier
@@ -113,7 +123,7 @@ protected function getComment($id)
113123
$comment = $this->commentManager->find($id);
114124

115125
if (null === $comment) {
116-
throw new NotFoundHttpException(sprintf('Comment (%d) not found', $id));
126+
throw new NotFoundHttpException(sprintf('Comment not found - identifier: %s', var_export($id, true)));
117127
}
118128

119129
return $comment;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Sonata Project package.
7+
*
8+
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Sonata\NewsBundle\Controller\Api\Legacy;
15+
16+
use FOS\RestBundle\Controller\Annotations as REST;
17+
use FOS\RestBundle\View\View;
18+
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
19+
use Sonata\NewsBundle\Model\Comment;
20+
use Sonata\NewsBundle\Model\CommentManagerInterface;
21+
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
22+
23+
/**
24+
* @author Hugo Briand <briand@ekino.com>
25+
*/
26+
class CommentController
27+
{
28+
/**
29+
* @var CommentManagerInterface
30+
*/
31+
protected $commentManager;
32+
33+
/**
34+
* @param CommentManagerInterface $commentManager Comment manager
35+
*/
36+
public function __construct(CommentManagerInterface $commentManager)
37+
{
38+
$this->commentManager = $commentManager;
39+
}
40+
41+
/**
42+
* Retrieves a specific comment.
43+
*
44+
* @ApiDoc(
45+
* resource=true,
46+
* requirements={
47+
* {"name"="id", "dataType"="string", "description"="Comment identifier"}
48+
* },
49+
* output={"class"="Sonata\NewsBundle\Model\Comment", "groups"={"sonata_api_read"}},
50+
* statusCodes={
51+
* 200="Returned when successful",
52+
* 404="Returned when comment is not found"
53+
* }
54+
* )
55+
*
56+
* @REST\View(serializerGroups={"sonata_api_read"}, serializerEnableMaxDepthChecks=true)
57+
*
58+
* @param string $id Comment identifier
59+
*
60+
* @throws NotFoundHttpException
61+
*
62+
* @return Comment
63+
*/
64+
public function getCommentAction($id)
65+
{
66+
return $this->getComment($id);
67+
}
68+
69+
/**
70+
* Deletes a comment.
71+
*
72+
* @ApiDoc(
73+
* requirements={
74+
* {"name"="id", "dataType"="string", "description"="Comment identifier"}
75+
* },
76+
* statusCodes={
77+
* 200="Returned when comment is successfully deleted",
78+
* 400="Returned when an error has occurred while comment deletion",
79+
* 404="Returned when unable to find comment"
80+
* }
81+
* )
82+
*
83+
* @param string $id Comment identifier
84+
*
85+
* @throws NotFoundHttpException
86+
*
87+
* @return View
88+
*/
89+
public function deleteCommentAction($id)
90+
{
91+
$comment = $this->getComment($id);
92+
93+
try {
94+
$this->commentManager->delete($comment);
95+
} catch (\Exception $e) {
96+
return View::create(['error' => $e->getMessage()], 400);
97+
}
98+
99+
return ['deleted' => true];
100+
}
101+
102+
/**
103+
* Returns a comment entity instance.
104+
*
105+
* @param string $id Comment identifier
106+
*
107+
* @throws NotFoundHttpException
108+
*
109+
* @return Comment
110+
*/
111+
protected function getComment($id)
112+
{
113+
$comment = $this->commentManager->find($id);
114+
115+
if (null === $comment) {
116+
throw new NotFoundHttpException(sprintf('Comment not found - identifier: %s', var_export($id, true)));
117+
}
118+
119+
return $comment;
120+
}
121+
}

0 commit comments

Comments
 (0)