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:
I see two quick solutions to this problem, but I wanted to discuss them with you guys before I open a PR:
-
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.
-
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?
The failing tests from PR #132 were merged into master, so now all the checks are failing for new PRs:
The issue is caused by this ternary operator in
src/spec/Schema.php, which will always evaluate totrue(thereby always settingnullabletofalse, and never actuallynull):php-openapi/src/spec/Schema.php
Line 130 in 336bd8d
This always evaluates to the
trueside of the operator becausehasProperty()also returnstrueif an attribute is defined, whether or not it has a value:php-openapi/src/SpecBaseObject.php
Lines 300 to 303 in 336bd8d
And of course,
typeis a defined attribute of theSchemaobject:php-openapi/src/spec/Schema.php
Line 92 in 336bd8d
I see two quick solutions to this problem, but I wanted to discuss them with you guys before I open a PR:
As far as I see,
Schema::attributeDefaults()is the only place where theSpecBaseObject::hasProperty()method is called, so a simple fix (which passes all the tests) would be to simplify the method like this:Is there a reason why
hasProperty()also checks if an attribute is defined? The method kinda feels misnamed.The ternary conditional in
attributeDefaults()could be rewritten to useisset:But
issetof course calls__isset(), which itself callsattributeDefaults(), so this might cause a recursion error.Thoughts?