Hacktoberfest 2026: los issues que los mantenedores marcaron para octubre, abiertos y aptos para principiantes. Explorar issues de Hacktoberfest

Typescript: how to handle arrays or toMany relations

Abierto
#129 1 comentario 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
30/100
Tipo de issue
Nueva funcionalidad
Claridad
Necesita aclaración
Estado de actividad
Estancado
Stack tecnológico
typescript
Área
tooling

Línea de trabajo

Empieza en la función generate del generador de interfaces de TypeScript e inspecciona cómo resource.fields utiliza reference, range y maxCardinality. Compara esos metadatos con la representación de Swagger del array de prueba y de la relación to-many de products, y comprueba después si doc-parser proporciona información sobre colecciones. Se considera terminado cuando el generador pueda distinguir entre campos escalares, arrays y relaciones to-many, y producir las interfaces previstas.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

enhancement TypeScript

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!

Lenguaje dominante
TypeScript
Estrellas
376
Forks
132
Métricas de merge de PR
Sin PR fusionados en 30 d

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de api-platform/create-client

Todos los issues de api-platform/create-client

Issues similares

Más issues de TypeScript

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.