Skip to content

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

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

Closed
victorstanciu opened this issue Dec 10, 2021 · 2 comments
Closed

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

victorstanciu opened this issue Dec 10, 2021 · 2 comments
Labels
bug Something isn't working
Milestone

Comments

@victorstanciu
Copy link

victorstanciu commented Dec 10, 2021

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?

@victorstanciu victorstanciu changed the title PR #132 broke the unit tests Unit tests are broken in master Dec 10, 2021
@victorstanciu victorstanciu changed the title Unit tests are broken in master Unit tests are broken in master since PR #132 Dec 10, 2021
@cebe cebe added bug Something isn't working under discussion feature/enhancement needs more input to decide if or how it should be implemented. labels Dec 21, 2021
@cebe cebe added this to the 1.5.3 milestone Dec 21, 2021
@cebe cebe modified the milestones: 1.5.3, 1.6.0 Feb 9, 2022
@cebe
Copy link
Owner

cebe commented Feb 9, 2022

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

yeah, no idea why this method even exists, it is only called in that one place, so I'm going to fix it this way.

@cebe cebe removed the under discussion feature/enhancement needs more input to decide if or how it should be implemented. label Feb 9, 2022
@cebe cebe closed this as completed in 0e0c69c Feb 9, 2022
@cebe
Copy link
Owner

cebe commented Feb 9, 2022

actually came up with an even better fix, added a new method and deprecated hasProperty() so this does not break any extension that might use it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants