api-platform / core

The server component of API Platform: hypermedia and GraphQL APIs in minutes
https://api-platform.com
MIT License
2.45k stars 882 forks source link

PathSegmentNameGenerator::getSegmentName always called with $collection = true? #1792

Closed CHiPs44 closed 6 years ago

CHiPs44 commented 6 years ago

I would like to change API path to something like :

Alas, PathSegmentNameGenerator::getSegmentName seems to always be called with $collection = true.

Is there a way to solve this, or something I didn't understand?

Even if most of it should not be relevant, here is some data about my dev env :

$ composer info
api-platform/api-pack             1.1.0              A pack for API Platform
api-platform/core                 v2.2.4             The ultimate solution to create web APIs.
doctrine/annotations              v1.6.0             Docblock Annotations Parser
doctrine/cache                    v1.7.1             Caching library offering an object-oriented API for many cache backends
doctrine/collections              v1.5.0             Collections Abstraction library
doctrine/common                   v2.8.1             Common Library for Doctrine projects
doctrine/data-fixtures            v1.3.0             Data Fixtures for all Doctrine Object Managers
doctrine/dbal                     v2.6.3             Database Abstraction Layer
doctrine/doctrine-bundle          1.8.1              Symfony DoctrineBundle
doctrine/doctrine-cache-bundle    1.3.2              Symfony Bundle for Doctrine Cache
doctrine/doctrine-fixtures-bundle 3.0.2              Symfony DoctrineFixturesBundle
doctrine/inflector                v1.3.0             Common String Manipulations with regard to casing and singular/plural rules.
doctrine/instantiator             1.1.0              A small, lightweight utility to instantiate objects in PHP without invoking their constructors
doctrine/lexer                    v1.0.1             Base library for a lexer that can be used in Top-Down, Recursive Descent Parsers.
doctrine/orm                      v2.6.1             Object-Relational-Mapper for PHP
jdorn/sql-formatter               v1.2.17            a PHP SQL highlighting library
nelmio/cors-bundle                1.5.4              Adds CORS (Cross-Origin Resource Sharing) headers support in your Symfony2 application
phpdocumentor/reflection-common   1.0.1              Common reflection classes used by phpdocumentor to reflect the code structure
phpdocumentor/reflection-docblock 4.3.0              With this component, a library can provide support for annotations via DocBlocks or otherwise retrieve information that is embedded in a DocBlock.
phpdocumentor/type-resolver       0.4.0             
psr/cache                         1.0.1              Common interface for caching libraries
psr/container                     1.0.0              Common Container Interface (PHP FIG PSR-11)
psr/log                           1.0.2              Common interface for logging libraries
psr/simple-cache                  1.0.1              Common interfaces for simple caching
symfony/asset                     v4.0.6             Symfony Asset Component
symfony/cache                     v4.0.6             Symfony Cache component with PSR-6, PSR-16, and tags
symfony/config                    v4.0.6             Symfony Config Component
symfony/console                   v4.0.6             Symfony Console Component
symfony/debug                     v4.0.6             Symfony Debug Component
symfony/dependency-injection      v4.0.6             Symfony DependencyInjection Component
symfony/doctrine-bridge           v4.0.6             Symfony Doctrine Bridge
symfony/dotenv                    v4.0.6             Registers environment variables from a .env file
symfony/event-dispatcher          v4.0.6             Symfony EventDispatcher Component
symfony/expression-language       v4.0.6             Symfony ExpressionLanguage Component
symfony/filesystem                v4.0.6             Symfony Filesystem Component
symfony/finder                    v4.0.6             Symfony Finder Component
symfony/flex                      v1.0.76           
symfony/framework-bundle          v4.0.6             Symfony FrameworkBundle
symfony/http-foundation           v4.0.6             Symfony HttpFoundation Component
symfony/http-kernel               v4.0.6             Symfony HttpKernel Component
symfony/inflector                 v4.0.6             Symfony Inflector Component
symfony/lts                       dev-master 396c5fc Enforces Long Term Supported versions of Symfony components
symfony/polyfill-mbstring         v1.7.0             Symfony polyfill for the Mbstring extension
symfony/process                   v4.0.6             Symfony Process Component
symfony/property-access           v4.0.6             Symfony PropertyAccess Component
symfony/property-info             v4.0.6             Symfony Property Info Component
symfony/routing                   v4.0.6             Symfony Routing Component
symfony/security                  v4.0.6             Symfony Security Component
symfony/security-bundle           v4.0.6             Symfony SecurityBundle
symfony/serializer                v4.0.6             Symfony Serializer Component
symfony/translation               v4.0.6             Symfony Translation Component
symfony/twig-bridge               v4.0.6             Symfony Twig Bridge
symfony/twig-bundle               v4.0.6             Symfony TwigBundle
symfony/validator                 v4.0.6             Symfony Validator Component
symfony/web-server-bundle         v4.0.6             Symfony WebServerBundle
symfony/yaml                      v4.0.6             Symfony Yaml Component
twig/twig                         v2.4.7             Twig, the flexible, fast, and secure template language for PHP
webmozart/assert                  1.3.0              Assertions to validate method input/output with nice error messages.
willdurand/negotiation            v2.3.1             Content Negotiation tools for PHP provided as a standalone library.

PHP version is :

$ php -v
PHP 7.1.15-1+ubuntu16.04.1+deb.sury.org+2 (cli) (built: Mar  6 2018 11:10:13) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.1.15-1+ubuntu16.04.1+deb.sury.org+2, Copyright (c) 1999-2018, by Zend Technologies
marmotz commented 6 years ago

Same issue and same question here :-/

soyuka commented 6 years ago

Please override the OperationPathResolver service instead:

https://github.com/api-platform/core/blob/35edfb6ae999cf77e04caf81d9c33cbda9aa3890/src/PathResolver/OperationPathResolver.php

It's using an OperationType that you can compare to these constants

antograssiot commented 6 years ago

If you don't want plurialization, you can just create your own PathSegmentNameGenerator by implementing ApiPlatform\Core\Operation\PathSegmentNameGeneratorInterface and set it in your config path_segment_name_generator

soyuka commented 6 years ago

Not really because as they said it's always called with true (see here) as second argument because we're always using pluralization and they want to use it only on item collections.

antograssiot commented 6 years ago

the POST /api/my-object/id is totally misleading so I assumed he would expects to POST to /api/my-object

CHiPs44 commented 6 years ago

@antograssiot Yes, I made an error with POST.

I'm going to try the OperationPathResolver solution.

CHiPs44 commented 6 years ago

I retried to make this issue work with API Platform 2.3.3, without success.

I saw several occurences of "The use of the default_operation_path_resolver has been deprecated in 2.1 and will be removed in 3.0. Use path_segment_name_generator instead." in the source code.

As I asked this essentially for cosmetic reasons, I close this issue.