swagger-api / swagger-api/swagger-codegen

[PHP] [SymfonyServerCodegen] Bug: ignore all default values for Swagger\Server\Model

Open
#8,940 1 comment 0 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

Default values for input models dont filled.

Swagger-codegen version
java -jar swagger-codegen-cli.jar version
2.3.1
Swagger declaration file content or url
definitions:
  Description:
    type: object
    properties:
      description:
        type: string
        description: "Description"
        default: "Default Description"
Command line used for generation

java -jar swagger-codegen-cli.jar generate --input-spec test.yml --lang io.swagger.codegen.languages.SymfonyServerCodegen --output app/demo

Steps to reproduce

This generate file SwaggerServerBundle\Model\Description.php:

<?php

namespace Swagger\Server\Model;

use Symfony\Component\Validator\Constraints as Assert;
use JMS\Serializer\Annotation\Type;
use JMS\Serializer\Annotation\SerializedName;

/**
 * Class representing the Description model.
 *
 * @package Swagger\Server\Model
 * @author  Swagger Codegen team
 */
class Description 
{
        /**
     * Description
     *
     * @var string|null
     * @SerializedName("description")
     * @Assert\Type("string")
     * @Type("string")
     */
    protected $description;

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


    /**
     * Gets description.
     *
     * @return string|null
     */
    public function getDescription()
    {
        return $this->description;
    }

    /**
     * Sets description.
     *
     * @param string|null $description  Description
     *
     * @return $this
     */
    public function setDescription($description = null)
    {
        $this->description = $description;

        return $this;
    }
}

But in Controller initiate by code:

$description = $this->deserialize($description, 'Swagger\Server\Model\Description', $inputFormat);

But this code create instance with reflection and newInstanceWithoutConstructor.
The class constructor is not called, in this case, and the default values are not filled!

Related issues/PRs

#8369

Suggest a fix/enhancement

Need to come up with another way to fill default values.

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 generated Swagger\Server\Model\Description.php and the controller deserialization call shown in the report. Compare the behavior with related issue #8369 and trace how deserialization bypasses the model constructor. Done means default values from the Swagger definition are populated when input models are deserialized.

Written by the indexing model from the issue text.

Assessment

Tech stack
php, symfony
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.