api-platform / schema-generator

PHP Model Scaffolding from Schema.org and other RDF vocabularies
https://api-platform.com/docs/schema-generator/
MIT License
453 stars 108 forks source link

Declare multiple operations #405

Closed vincentchalamon closed 1 year ago

vincentchalamon commented 1 year ago

API Platform version(s) affected: ^3.1

Description
It's not possible to declare multiple operations using the same class:

operations:
  Post:
    uriTemplate: /books/{bookId}/borrow
  Post:
    uriTemplate: /books/{bookId}/return

In API Platform 3.*, to prevent this limitation, it is possible to declare the operations as following:

operations:
  - class: ApiPlatform\Metadata\Post
    uriTemplate: /books/{bookId}/borrow
  - class: ApiPlatform\Metadata\Post
    uriTemplate: /books/{bookId}/return

But the code generated by the Schema Generator is invalid:

#[ApiResource(
    operations: [
        new 0(class: 'ApiPlatform\Metadata\Post', uriTemplate: '/books/{bookId}/borrow'),
        new 1(class: 'ApiPlatform\Metadata\Post', uriTemplate: '/books/{bookId}/return'),

How to reproduce
Declare the following configuration in the schema.yaml file:

types:
    BookOffer:
        guessFrom: "Book"
        attributes:
            - ApiResource:
                types: [ "https://schema.org/Book" ]
        operations:
            - class: "ApiPlatform\\Metadata\\GetCollection"
            - class: "ApiPlatform\\Metadata\\Get"

Then, run the Schema Generator: vendor/bin/schema generate src/ config/schema.yaml -vv

Possible Solution
Support both formats declared above.

Additional Context

alanpoulain commented 1 year ago

The code to modify is here: https://github.com/api-platform/schema-generator/blob/2f7098733d9d111d171730a1f412068db738d272/src/AttributeGenerator/ApiPlatformCoreAttributeGenerator.php#L83-L86