title: Serializing nested fields in Symfony
body: <p>Is it possible to achieve this using either Symfony serializer or JMS serializer?</p>
  <p>My example schema:</p>
  <pre class="lang-php prettyprint-override"><code>    class User
      {
          /**
           * @ORM\Column(type=&quot;string&quot;)
           * @Serializer\Groups({
           *     &quot;name&quot;
           * })
           * @Serializer\Expose()
          */
          private $name;       
  
          /**
           * @ORM\Column(type=&quot;string&quot;)
           * @Serializer\Groups({
           *     &quot;age&quot;
           * })
           * @Serializer\Expose()
          */
          private $age;   
  
          /**
           * @ORM\ManyToOne(targetEntity=&quot;Product&quot;)
           * @Serializer\Groups({
           *     &quot;product&quot;
           * })
           * @Serializer\Expose()
          */
          private $product;   
      }
  
      class Product
      {
          /**
           * @ORM\Column(type=&quot;string&quot;)
           * @Serializer\Groups({
           *     &quot;name&quot;
           * })
           * @Serializer\Expose()
          */
          private $name;
      }
  </code></pre>
  <p>Now I would like to pass following serialization groups to let the serializer return exactly that schema:</p>
  <pre class="lang-php prettyprint-override"><code>$groups = [
      'age',
      'product' =&gt; ['name']
  ];
  $serializer-&gt;serialize($user, 'json', (new SerializationContext())-&gt;setGroups($groups));
  </code></pre>
  <p>and this should return to me only</p>
  <pre class="lang-json prettyprint-override"><code>{
     &quot;age&quot;: 15,
     &quot;product&quot;: {
        &quot;name&quot;: &quot;Product 1&quot;
     }
  }
  </code></pre>
  <p>However, <strong>this does not work</strong> because it returns only:</p>
  <pre class="lang-json prettyprint-override"><code>{
     &quot;age&quot;: 15
  }
  </code></pre>
  <p>After little tweeking:</p>
  <pre class="lang-php prettyprint-override"><code>$groups = [
      0 =&gt; 'age',
      1 =&gt; 'product',
      'product' =&gt; ['name']
  ];
  </code></pre>
  <p>it returns:</p>
  <pre class="lang-json prettyprint-override"><code>{
     &quot;age&quot;: 15,
     &quot;product&quot;: {}
  }
  </code></pre>
  <p>but &quot;product&quot; does not have name yet unfortunately.</p>
  <p>It starting to work when I add &quot;name&quot; to the root of the array, like this:</p>
  <pre class="lang-php prettyprint-override"><code>$groups = [
      0 =&gt; 'age',
      1 =&gt; 'name',
      2 =&gt; 'product'
  ];
  </code></pre>
  <p>returns:</p>
  <pre class="lang-json prettyprint-override"><code>{
     &quot;name&quot;: &quot;John&quot;,
     &quot;age&quot;: 15,
     &quot;product&quot;: {
         &quot;name&quot;: &quot;Product 1&quot;
     }
  }
  </code></pre>
  <p>However, <strong>this is not what</strong> I want because user also returns &quot;name&quot;.</p>
  <p>I want to return exactly those properties I have specified, kinda like GraphQL supports fields.</p>