Skip to content

Unit tests are broken in master since PR #132 #142

Closed
@victorstanciu

Description

@victorstanciu

The failing tests from PR #132 were merged into master, so now all the checks are failing for new PRs:

There was 1 failure:

1) SchemaTest::testNullable
Failed asserting that false is null.

/home/runner/work/php-openapi/php-openapi/tests/spec/SchemaTest.php:66

The issue is caused by this ternary operator in src/spec/Schema.php, which will always evaluate to true (thereby always setting nullable to false, and never actually null):

'nullable' => $this->hasProperty('type') ? false : null,

This always evaluates to the true side of the operator because hasProperty() also returns true if an attribute is defined, whether or not it has a value:

protected function hasProperty(string $name): bool
{
return isset($this->_properties[$name]) || isset($this->attributes()[$name]);
}

And of course, type is a defined attribute of the Schema object:

'type' => Type::STRING,

I see two quick solutions to this problem, but I wanted to discuss them with you guys before I open a PR:

  1. As far as I see, Schema::attributeDefaults() is the only place where the SpecBaseObject::hasProperty() method is called, so a simple fix (which passes all the tests) would be to simplify the method like this:

       protected function hasProperty(string $name): bool
       {
           return isset($this->_properties[$name]);
       }

    Is there a reason why hasProperty() also checks if an attribute is defined? The method kinda feels misnamed.

  2. The ternary conditional in attributeDefaults() could be rewritten to use isset:

      'nullable' => isset($this->type) ? false : null,

    But isset of course calls __isset(), which itself calls attributeDefaults(), so this might cause a recursion error.

Thoughts?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions