Skip to content

Typescript: how to handle arrays or toMany relations #129

Open
@luca-nardelli

Description

@luca-nardelli

Hey,

while I was working on an Angular generator I noticed that the current implementation of the typescript interface generator does not take into account toMany relations (or simple arrays as well).

I had a look at the fields property inside resource, however I can't seem to find any difference between single fields and array fields. I hoped that the maxCardinality property would help me, but it seems properly populated only for reference types.

Here is an example of what I mean:

Resource definition

    /**
     * @ORM\Column(type="text", nullable=true)
     * @Groups({"producerRead","userRead"})
     */
    private $name;

    /**
     * @ApiSubresource()
     * @ORM\OneToMany(targetEntity="App\Entity\Product", mappedBy="producer", orphanRemoval=true)
     * @Groups({"producerRead","userRead"})
     */
    private $products;

    /**
     * @ORM\OneToOne(targetEntity="App\Entity\Picture", cascade={"persist", "remove"})
     * @Groups({"producerRead","producerWrite"})
     */
    private $mainPicture;

    /**
     * @var array
     * @Groups({"producerRead","producerWrite"})
     */
    public $test;

Dump of resource.fields (part of id) inside the generate function

     Field {
       name: 'products',
       id: 'http://localhost:8080/api/docs.jsonld#Producer/products',
       range: 'http://localhost:8080/api/docs.jsonld#Product',
       reference: [Resource],
       required: false,
       description: '',
       maxCardinality: null,
       deprecated: false },
     Field {
       name: 'name',
       id: 'http://localhost:8080/api/docs.jsonld#Producer/name',
       range: 'http://www.w3.org/2001/XMLSchema#string',
       reference: null,
       required: false,
       description: '',
       maxCardinality: null,
       deprecated: false },
     Field {
       name: 'mainPicture',
       id: 'http://localhost:8080/api/docs.jsonld#Producer/mainPicture',
       range: 'http://localhost:8080/api/docs.jsonld#Picture',
       reference: [Resource],
       required: false,
       description: '',
       maxCardinality: 1,
       deprecated: false },
     Field {
       name: 'test',
       id: 'http://localhost:8080/api/docs.jsonld#Producer/test',
       range: null,
       reference: null,
       required: false,
       description: '',
       maxCardinality: null,
       deprecated: false },

In the swagger docs the property test is correctly picked up as a string array, however I can't seem to replicate the same thing for the typescript interfaces.

If we look at maxCardinality, it seems it is correct (if we assume null means 'collection') for reference fields (mainPicture is a OnetoOne and products is a ToMany), however this is not the case for name (string) and test (string array).

I'm asking here because there might be another field that helps me find out if I'm dealing with a collection or not and I'm missing it, but this issue might be related to doc-parser as well.

I'll gladly create a PR for the typescript generator if we find out how to fix this. Thanks!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions