willdurand / Hateoas

A PHP library to support implementing representations for HATEOAS REST web services.
https://williamdurand.fr/Hateoas/
Other
1.04k stars 118 forks source link

No link in my result... :(( #159

Open keyermann opened 10 years ago

keyermann commented 10 years ago

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 ?

pmartelletti commented 10 years ago

@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!

adrienbrault commented 10 years ago

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 notifications@github.com 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.

pmartelletti commented 10 years ago

@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 commented 10 years ago

@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!

keyermann commented 10 years ago

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

Thank you both @pmartelletti and @adrienbrault !!

adrienbrault commented 10 years ago

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

On Thursday, July 3, 2014, Kévin EYERMANN notifications@github.com 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.

pmartelletti commented 10 years ago

@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.

mrjoops commented 9 years ago

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.

skineur commented 8 years ago

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

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