From: adev Date: Sun, 27 Oct 2019 17:51:32 +0000 (+0100) Subject: API return an error with empty quote X-Git-Url: https://git.immae.eu/?a=commitdiff_plain;h=8197f08266256f55767666b90be47f585c7a6d28;hp=a4064347015e169b309153f214e2bb5ec17fde21;p=github%2Fwallabag%2Fwallabag.git API return an error with empty quote Fix #4137 --- diff --git a/src/Wallabag/AnnotationBundle/Entity/Annotation.php b/src/Wallabag/AnnotationBundle/Entity/Annotation.php index a180d504..ee7c1931 100644 --- a/src/Wallabag/AnnotationBundle/Entity/Annotation.php +++ b/src/Wallabag/AnnotationBundle/Entity/Annotation.php @@ -60,6 +60,7 @@ class Annotation /** * @var string * + * @Assert\NotNull() * @Assert\Length( * max = 10000, * maxMessage = "validator.quote_length_too_high" diff --git a/src/Wallabag/AnnotationBundle/Form/NewAnnotationType.php b/src/Wallabag/AnnotationBundle/Form/NewAnnotationType.php index c73c3ded..aac6445c 100644 --- a/src/Wallabag/AnnotationBundle/Form/NewAnnotationType.php +++ b/src/Wallabag/AnnotationBundle/Form/NewAnnotationType.php @@ -18,6 +18,7 @@ class NewAnnotationType extends AbstractType ]) ->add('quote', null, [ 'empty_data' => null, + 'trim' => false, ]) ->add('ranges', CollectionType::class, [ 'entry_type' => RangeType::class, diff --git a/src/Wallabag/ApiBundle/Controller/AnnotationRestController.php b/src/Wallabag/ApiBundle/Controller/AnnotationRestController.php index f59431e4..e5b3eb27 100644 --- a/src/Wallabag/ApiBundle/Controller/AnnotationRestController.php +++ b/src/Wallabag/ApiBundle/Controller/AnnotationRestController.php @@ -37,7 +37,7 @@ class AnnotationRestController extends WallabagRestController * @ApiDoc( * requirements={ * {"name"="ranges", "dataType"="array", "requirement"="\w+", "description"="The range array for the annotation"}, - * {"name"="quote", "dataType"="string", "required"=false, "description"="Optional, quote for the annotation"}, + * {"name"="quote", "dataType"="string", "required"=true, "description"="Quote for the annotation"}, * {"name"="text", "dataType"="string", "required"=true, "description"=""}, * } * ) diff --git a/tests/Wallabag/AnnotationBundle/Controller/AnnotationControllerTest.php b/tests/Wallabag/AnnotationBundle/Controller/AnnotationControllerTest.php index 2c46e0a1..74e9ba8f 100644 --- a/tests/Wallabag/AnnotationBundle/Controller/AnnotationControllerTest.php +++ b/tests/Wallabag/AnnotationBundle/Controller/AnnotationControllerTest.php @@ -107,6 +107,36 @@ class AnnotationControllerTest extends WallabagAnnotationTestCase $this->assertSame('my annotation', $annotation->getText()); } + public function testCouldNotSetAnnotationWithoutQuote() + { + $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); + + /** @var Entry $entry */ + $entry = $em + ->getRepository('WallabagCoreBundle:Entry') + ->findOneByUsernameAndNotArchived('admin'); + + $headers = ['CONTENT_TYPE' => 'application/json']; + $content = json_encode([ + 'text' => 'my annotation', + 'quote' => null, + 'ranges' => [ + ['start' => '', 'startOffset' => 24, 'end' => '', 'endOffset' => 31], + ], + ]); + $this->client->request('POST', '/api/annotations/' . $entry->getId() . '.json', [], [], $headers, $content); + + $this->assertSame(400, $this->client->getResponse()->getStatusCode()); + + $content = json_decode($this->client->getResponse()->getContent(), true); + + $this->assertCount( + 1, + $content['errors']['children']['quote']['errors'], + 'The quote field should contains an error' + ); + } + /** * @dataProvider dataForEachAnnotations */