Hateoas icon indicating copy to clipboard operation
Hateoas copied to clipboard

No link in my result... :((

Open keyermann opened this issue 10 years ago • 9 comments

Hi.

Fist of all, sorry for my bad english, I'm trying to improve it. I don't know what I am doing wrong but I can't have any link in my results. Here are some peace of my work. Maybe you coult tell me the problem.

config.yml

#...

sensio_framework_extra:
    view: { annotations: false }

jms_serializer:
    metadata:
        auto_detection: true
        directories:
            BdgsModel:
                namespace_prefix: "BDGS\\DatabaseModelBundle"
                path: "@BdgsRestApiBundle/Resources/config/serializer/model/"
            Bdgs:
                namespace_prefix: "BDGS\\DatabaseBundle"
                path: "@BdgsRestApiBundle/Resources/config/serializer"

fos_rest:
    serializer:
        serialize_null: false
    param_fetcher_listener: true
    view:
        view_response_listener: 'force'
        formats:
            xml:  true
            json: true
        templating_formats:
            html: true
    format_listener:
        rules:
            - { path: ^/, priorities: [ json, html, xml ], fallback_format: ~, prefer_extension: true }
    exception:
        codes:
            'Symfony\Component\Routing\Exception\ResourceNotFoundException': 404
            'Doctrine\ORM\OptimisticLockException': HTTP_CONFLICT
        messages:
            'Symfony\Component\Routing\Exception\ResourceNotFoundException': true
    allowed_methods_listener: true
    access_denied_listener:
        json: true
    body_listener: true
    disable_csrf_role: ROLE_API

bazinga_hateoas: ~

bazinga_rest_extra: ~

StationModel.php

use BDGS\DatabaseModelBundle\Model\ModelTools;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

/**
 * Station
 *
 * @ORM\Table("station" , uniqueConstraints={@ORM\UniqueConstraint(name="uniq_acro_tech", columns={"acronyme", "techniquestation_id"})})
 * @ORM\Entity(repositoryClass="BDGS\DatabaseModelBundle\Model\StationModelRepository")
 * @UniqueEntity(fields={"acronyme","TechniqueStation"} )
 */
class StationModel extends ModelTools implements StationInterface
{
    /**
     * Identifiant
     *
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", options={"comment" = "Identifiant de la station."})
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * Acronyme
     *
     * @var string
     *
     * @Assert\NotBlank()
     * @Assert\Length(min = 4, max = 8)
     * @ORM\Column(name= "acronyme" , type="string", length=8, unique=false, nullable=false, options={"comment" = "Acronyme de la station."})
     */
    protected $acronyme;

    /** ............. */

Station.php

use BDGS\DatabaseModelBundle\Model\StationModel;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

/**
 * Station
 *
 * @ORM\Table("station", uniqueConstraints={@ORM\UniqueConstraint(name="uniq_acro_tech", columns={"acronyme", "techniquestation_id"})})
 * @ORM\Entity(repositoryClass="BDGS\DatabaseBundle\Entity\StationRepository")
 * @UniqueEntity(fields={"acronyme","TechniqueStation"} )
 */
class Station extends StationModel
{
    /**
     * Identifiant
     *
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", options={"comment" = "Identifiant de la station."})
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * truc temporaire
     *
     * @var integer
     *
     * @ORM\Column( name="ptg_temp" , type="integer", unique=false, nullable=true)
     */
    private $ptgTemp;

    /** ............. */

serializer configuration

BDGS\DatabaseModelBundle\Model\StationModel:
    exclusion_policy: ALL
    properties:
        id:
            read_only: true
            expose: true
            groups: [stations, station, technique_station]
        acronyme:
            expose: true    
            groups: [stations, station, technique_station]
        cdp:
            expose: true    
            groups: [stations, station]
        code2:
            expose: true    
            groups: [stations, station]

    # ...

    relations:
        -
            rel: self
            href: 
                route: api_1_get_station
                parameters:
                    id: expr(object.getId())
                absolute: true

Result of a request :

{
    id: 68,
    acronyme: "VACS",
    cdp: "GNSS01",
    code2: "GNSS01",
    date_debut: "2008-04-07",
    ptg_temp: 726
}

Any Idea ? Something I didn't understand ?

keyermann avatar Jul 02 '14 10:07 keyermann

@willdurand @adrienbrault I've come up with this issue too, and haven't found a solution in any of the open / closed issues.

I think this is the same problem that the user had in #140 but he never replied.

The main problem is that when using serializer library to display only group attributes, _links attribute is not shown. Not sure if this is a bug, or some documentation is missing.

Any thoughts?

Cheers!

pmartelletti avatar Jul 04 '14 05:07 pmartelletti

You need to add the groups to those relations (they are excluded the same way the properties are excluded).

On Thursday, July 3, 2014, Pablo Martelletti [email protected] wrote:

@willdurand https://github.com/willdurand @adrienbrault https://github.com/adrienbrault I've come up with this issue too, and haven't found a solution in any of the open / closed issues.

I think this is the same problem that the user had in #140 https://github.com/willdurand/Hateoas/issues/140 but he never replied.

The main problem is that when using serializer library to display only group attributes, _links attribute is not show. Not sure if this is a bug, or some documentation is missing.

Any thoughts?

Cheers!

— Reply to this email directly or view it on GitHub https://github.com/willdurand/Hateoas/issues/159#issuecomment-48010495.

adrienbrault avatar Jul 04 '14 05:07 adrienbrault

@adrienbrault what do yo mean by add the groups to the relation?

Talking about annotations, I would add a property to the group, by doing something like:

/**
 * @var DateTime $startDate
 * @ORM\Column(name="start_date", type="date")
 * @Groups({"list", "details"})
 */
protected $startDate;

However, I define a relation for an Entity the following way:

 * Class PlanAssignment
 *
 * @ORM\Entity
 * @ORM\Table(name="aaa_plan_assignment")
 * @ORM\Entity()
 * @Hateoas\Relation(
 *      "self",
 *      href = @Hateoas\Route("aaa_asuser_plan_assignment_detail", parameters = {"id" = "expr(object.getId())"})
 * )
 */
class PlanAssignment

So, with that example, what do you mean to add the group to the relation? the only "group" option in the documentation is for "exclusion" property, and in fact, I do no want to exclude but to show it (one would think that by default, all _links would be shown and I would need to exclude them for groups, according to the "exclusion" property in Relation).

pmartelletti avatar Jul 04 '14 05:07 pmartelletti

@adrienbrault ah! solved it. The key "exclusion" is confusing. I thought that those groups would be "excluded" from serialization, not the other way round. Sorry for that!

pmartelletti avatar Jul 04 '14 05:07 pmartelletti

Aw ! Shame on me... I haven't thought a second about exclusion groups...

Thank you both @pmartelletti and @adrienbrault !!

keyermann avatar Jul 04 '14 06:07 keyermann

Maybe we should rename exclusion to inclusion, I see it is really confusing.

On Thursday, July 3, 2014, Kévin EYERMANN [email protected] wrote:

Aw ! Shame on me... I haven't thought a second about exclusion groups...

Thank you both @pmartelletti https://github.com/pmartelletti and @adrienbrault https://github.com/adrienbrault !!

— Reply to this email directly or view it on GitHub https://github.com/willdurand/Hateoas/issues/159#issuecomment-48012795.

adrienbrault avatar Jul 04 '14 07:07 adrienbrault

@adrienbrault not really, because the "exlude if" attribute "excludes", it does not includes.

Maybe a better idea would be to include _links by default on all groups, and that the groups attribute on exclusion would list the groups on which the relation won't be shown. At least, that was the behavior I was expecting from the name of the attributes.

pmartelletti avatar Jul 04 '14 07:07 pmartelletti

Actually you have to specify "Default" and "list" (or "details") in your context for the feature to work as expected, or you will experience strange behaviors.

mrjoops avatar Jan 30 '15 11:01 mrjoops

If you use the FoSRestBundle , add annotations in the serialization controller Default group to see _links and _embedded

@Rest\View(serializerGroups={"Default"})

skineur avatar Dec 09 '15 14:12 skineur