swagger-api / swagger-api/swagger-codegen

PHP 7 : the way to transform an array of objects to an object which contains multiple objects

Open
#10,170 3 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

Hi,

I've an array FooList of object Foo :

FooList:
    type: array
    items:
      $ref: "#/definitions/Foo"

And the object

Foo:
    type: object
    properties:
      id:
        type: integer

The swagger-codegen generate the PHP code :
For class FooList

class FooList {
    //@param mixed[] $data Associated array of property values initializing the model
    public function __construct(array $data = null){}
}

And class Foo

class Foo {
    protected $id;

    // @param mixed[] $data Associated array of property values initializing the model
    public function __construct(array $data = null){
        $this->id = isset($data['id']) ? $data['id'] : null;
    }

    // getId + setId
}

In PHP I can map to have this result:

array:2 [
  0 => Swagger\Server\Model\Foo {
    #id: 1
  }
  1 => Swagger\Server\Model\Foo {
    #id: 2
  }
]

But how to map to have an object Foolist which contains Foo object like this ? :

object (FooList):2 [
  0 => Swagger\Server\Model\Foo {
    #id: 1
  }
  1 => Swagger\Server\Model\Foo {
    #id: 2
  }
]
Swagger-codegen version

2.4.12

Swagger declaration file content or url
swagger: "2.0"
info:
  description: "The description"
  version: "1.0.0"
  title: "FooList"
host: "127.0.0.1:8000"
basePath: "/"

paths:
  /foo:
    get:
      responses:
        200:
          description: "success"
          schema:
            $ref: "#/definitions/FooList"

definitions:
  Foo:
    type: object
    properties:
      id:
        type: integer
        
  FooList:
    type: array
    items:
      $ref: "#/definitions/Foo"
Command line used for generation

java -jar swagger-codegen-cli.jar generate -i C:\swagger.yaml -l php-symfony -o C:\generationDirectory

Steps to reproduce
  1. Symfony 5
  2. PHP 7.4
  3. Copy Only Models class generated in the Symfony project or if you don't know symfony just create a PHP file and write this:
$foo_1 = new Foo()
$foo_1->setId(1);
$foo_2 = new Foo()
$foo_2->setId(2);

$arrayFoo[]=$foo_1;
$arrayFoo[]=$foo_2;

$fooList = new FooList($arrayFoo);
var_dump($fooList);

... Impossible to find the way to have an object Foolist which contains an array....

Related issues/PRs
Suggest a fix/enhancement

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the php-symfony generation command and the FooList and Foo model output described in the issue. Compare the Swagger FooList array definition with the generated FooList constructor and determine the intended representation for an array of Foo objects. Done means the generated PHP models support the requested FooList behavior and the reproduction steps work as described.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
devtools
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.