php-code-builder icon indicating copy to clipboard operation
php-code-builder copied to clipboard

Non required properties could be null

Open bartje321 opened this issue 2 years ago • 0 comments

Non required properties could be missing and can return null. The generated classes do not reflect this is the phpdoc, For example, the schema:

{
    "type": "object",
    "properties": {
        "foo": {"type": "string"},
        "bar": {"type": "string"}
    },
    "required": ["foo"]
}

Creates a class with:

/** @var string */
public $bar;

...

/**
 * @return string
 * @codeCoverageIgnoreStart
 */
public function getBar()
{
    return $this->bar;
}

When validating

{"foo":""}

The schema will be validated as valid, but getBar() will return null, even though the phpdoc suggests it will always return a string


Test case

<?php

require_once __DIR__ . '/vendor/autoload.php';

$schemaData = json_decode(<<<'JSON'
{
    "type": "object",
    "properties": {
        "foo": {"type": "string"},
        "bar": {"type": "string"}
    },
    "required": ["foo"]
}
JSON
);

$swaggerSchema = \Swaggest\JsonSchema\Schema::import($schemaData);

$builder = new \Swaggest\PhpCodeBuilder\JsonSchema\PhpBuilder();
$builder->buildGetters = true;

$app = new \Swaggest\PhpCodeBuilder\App\PhpApp();
$app->setNamespaceRoot('foobar', '.');

$builder->classCreatedHook = new \Swaggest\PhpCodeBuilder\JsonSchema\ClassHookCallback(
    function (\Swaggest\PhpCodeBuilder\PhpClass $class) use ($app) {
        $class->setName('Test');
        $app->addClass($class);
    }
);

$builder->getType($swaggerSchema);
$app->clearOldFiles(__DIR__ . '/out');
$app->store(__DIR__ . '/out');

Actual

<?php
/**
 * @file ATTENTION!!! The code below was carefully crafted by a mean machine.
 * Please consider to NOT put any emotional human-generated modifications as the splendid AI will throw them away with no mercy.
 */

use Swaggest\JsonSchema\Constraint\Properties;
use Swaggest\JsonSchema\Schema;
use Swaggest\JsonSchema\Structure\ClassStructure;


class Test extends ClassStructure
{
    /** @var string */
    public $foo;

    /** @var string */
    public $bar;

    /**
     * @param Properties|static $properties
     * @param Schema $ownerSchema
     */
    public static function setUpProperties($properties, Schema $ownerSchema)
    {
        $properties->foo = Schema::string();
        $properties->bar = Schema::string();
        $ownerSchema->type = Schema::OBJECT;
        $ownerSchema->required = array(
            self::names()->foo,
        );
    }

    /**
     * @return string
     * @codeCoverageIgnoreStart
     */
    public function getFoo()
    {
        return $this->foo;
    }
    /** @codeCoverageIgnoreEnd */

    /**
     * @return string
     * @codeCoverageIgnoreStart
     */
    public function getBar()
    {
        return $this->bar;
    }
    /** @codeCoverageIgnoreEnd */
}

Expected

<?php
/**
 * @file ATTENTION!!! The code below was carefully crafted by a mean machine.
 * Please consider to NOT put any emotional human-generated modifications as the splendid AI will throw them away with no mercy.
 */

use Swaggest\JsonSchema\Constraint\Properties;
use Swaggest\JsonSchema\Schema;
use Swaggest\JsonSchema\Structure\ClassStructure;


class Test extends ClassStructure
{
    /** @var string */
    public $foo;

    /** @var string | null */
    public $bar;

    /**
     * @param Properties|static $properties
     * @param Schema $ownerSchema
     */
    public static function setUpProperties($properties, Schema $ownerSchema)
    {
        $properties->foo = Schema::string();
        $properties->bar = Schema::string();
        $ownerSchema->type = Schema::OBJECT;
        $ownerSchema->required = array(
            self::names()->foo,
        );
    }

    /**
     * @return string
     * @codeCoverageIgnoreStart
     */
    public function getFoo()
    {
        return $this->foo;
    }
    /** @codeCoverageIgnoreEnd */

    /**
     * @return string | null
     * @codeCoverageIgnoreStart
     */
    public function getBar()
    {
        return $this->bar;
    }
    /** @codeCoverageIgnoreEnd */
}

bartje321 avatar Mar 15 '22 11:03 bartje321