nelmio / NelmioApiDocBundle

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

Serialization groups are not passed on to nested Entities #1383

Open chriskaya opened 6 years ago

chriskaya commented 6 years ago

Hi there,

On current master I have an issue were the Default serialization group is not passed on to nested Entities. It seems to be caused by PR #1367 (more precisely by commit #5e5c650cf8b209da1b46d83c2c451fcd25d741c0).

Here's an example:

App\Entity\Transaction:
    exclusion_policy: ALL
    xml_root_name: transaction
    properties:
        id:
            expose: true
        name:
            expose: true
        category:
            expose: true
        buyer:
            expose: true
            groups: [ 'transaction_full' ]
App\Entity\Category:
    exclusion_policy: ALL
    xml_root_name: category
    properties:
        id:
            expose: true
        name:
            expose: true
        transactions:
            expose: true
            groups: [ 'category_full' ]
/**
     * Creates a transaction
     *
     * @SWG\Response(
     *     response=201,
     *     description="Transaction created",
     *     @Model(type=Transaction::class, groups={"Default"})
     * )
     * @SWG\Tag(name="Transaction")
     */

Result before #1367:

{
  "id": 0,
  "name": "string",
  "category": {
    "id": 5,
    "name": "Vente d'objets",
  }
}

Result after #1367:

{
  "id": 0,
  "name": "string",
  "category": {
      "id": 5,
      "name": "Vente d'objets",
      "transactions": [
            {
              "id": 0,
              "name": "string",
              "category": null,
              "buyer": { ... }
            }
      ]
  }
}

As you can see, the "Default" group is lost in nested Entities, and everything is rendered. BTW, with stable (3.2.1), I must specify "Default" otherwise everything is shown (not only in nested Entities).

So for me, $groups = null (#5e5c650cf8b209da1b46d83c2c451fcd25d741c0) is not the same as $groups = [self::DEFAULT_GROUP] (https://github.com/schmittjoh/serializer/blob/942b566c801b2c567193f006ebf95b027e5bd439/src/JMS/Serializer/Exclusion/GroupsExclusionStrategy.php#L85).

Or maybe I'm missing something?

Thx for your time :)

goetas commented 6 years ago

You sure about this? you are referring to #1367 (that is a nemio api doc PR) while your error is related to jms/serializer...

Are you passing the group to the serializer? (this depends how you are using jms... if via FOSRest or something else... can you share a bit ore about your controller?

chriskaya commented 6 years ago

Yes I'm starting to think there is something wrong in my conf... I posted that here because switching to master caused this change in my documentation, and if I comment $groups = null it works again. (The "results" I shared in my first post are documentation results, not responses from my controller)

I'm using SF 4.1, with FOSRest 2.3 and JMS 2.3 Here's my JMS conf:

jms_serializer:
    visitors:
        xml:
            format_output: '%kernel.debug%'
    metadata:
        auto_detection: true
        directories:
            Entity:
                namespace_prefix: "App\\Entity"
                path: "%kernel.root_dir%/../config/serializer"

And my FOSRest conf:

fos_rest: 
    param_fetcher_listener:  true
    allowed_methods_listener:  true
    routing_loader: true
    view:
        view_response_listener:  true
        formats:
            json : true
        templating_formats:
            html: true
    exception:
        codes:
            Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException: 403
        messages:
            Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException: true
    format_listener:
        rules:
            - { path: ^/api, prefer_extension: true, fallback_format: json, priorities: [ json ] }
            - { path: ^/oauth, prefer_extension: true, fallback_format: json, priorities: [ json ] }

Also, nelmio conf:

nelmio_api_doc:
    documentation:
        host: 'platform.kayanet.fr'
        schemes: [ 'https' ]
        consumes: [ 'application/json' ]
        produces: [ 'application/json' ]
        info:
            title: 'Tripartie backend'
            description: 'Everything is awesome!'
            version: 1.0.0
    areas:
        path_patterns:
            - ^/api(?!/doc$)
goetas commented 6 years ago

did you put in your action the annotation @View(serializerGroups={"group1", "group2"}) ?

chriskaya commented 6 years ago

No, here's the full annotation:

 /**
     * Creates a transaction
     * 
     * @SWG\Parameter(
     *     name="transaction",
     *     in="body",
     *     description="All fields are mandatory.",
     *     @Model(type=TransactionCreateType::class)
     * )
     * 
     * @SWG\Parameter(
     *     name="scopes",
     *     in="query",
     *     type="string",
     *     description="Use scopes=transaction_full to get more info on returned transaction.",
     * )
     *
     * @SWG\Response(
     *     response=201,
     *     description="Transaction created",
     *     @Model(type=Transaction::class, groups={"Default"})
     * )
     * @SWG\Tag(name="Transaction")
     */
    public function create(Request $request)
goetas commented 6 years ago

then you should

chriskaya commented 6 years ago

I tried this way (on another action which is simpler):

    /**
     * Fetch a transaction
     * 
     * @Security(name="Bearer")
     * 
     * @SWG\Parameter(
     *     name="id",
     *     in="path",
     *     description="The transaction's ID.",
     *     type="integer"
     * )
     * 
     * @SWG\Parameter(
     *     name="scopes",
     *     in="query",
     *     description="Use scopes=transaction_full to get more info on returned transaction.",
     *     type="string"
     * )
     *
     * @SWG\Response(
     *     response=200,
     *     description="Success",
     *     @Model(type=Transaction::class, groups={"Default"})
     * )
     *
     * @SWG\Response(
     *     response=404,
     *     description="Could not find transaction",
     *     @SWG\Schema(
     *         type="object",
     *         @SWG\Property(property="code", type="integer", example=404),
     *         @SWG\Property(property="errors", type="string", example=TransactionError::TRANSACTION_NOT_FOUND)
     *     )
     * )
     * 
     * @SWG\Tag(name="Transaction")
     *
     * @Rest\View(serializerGroups={"Default"})
     */
    public function show($id)

But I get the same behavior. I have the feeling I'm missing something :/ Sorry

chriskaya commented 6 years ago

The weird thing is that the main entity is ok, it's only the nested ones that are not respecting the specified group.

pdugas commented 3 years ago

Has this been investigated? I just discovered I'm getting the same results.