Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Non required properties could be null #47

Open
bartje321 opened this issue Mar 15, 2022 · 0 comments
Open

Non required properties could be null #47

bartje321 opened this issue Mar 15, 2022 · 0 comments

Comments

@bartje321
Copy link

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 */
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant