Skip to content

Adjust default value for Schema:: when no type is specified #132

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

Merged
merged 1 commit into from
Nov 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/spec/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ protected function attributeDefaults(): array
'allOf' => null,
'oneOf' => null,
'anyOf' => null,
// nullable is only relevant, when a type is specified
// return null as default when there is no type
// return false as default when there is a type
'nullable' => $this->hasProperty('type') ? false : null,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At what point this is calculated? Is schema->type mutable? Is this possible?

        // nullable is undefined if no type is given
        $schema = Reader::readFromJson('{"oneOf": [{"type": "string"}, {"type": "integer"}]}', Schema::class);
        $this->assertNull($schema->type);
        $this->assertNull($schema->nullable);
        $schema->type = Type::STRING;
        $this->assertFalse($schema->nullable);

If it's not mutable - looks good

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is executed on demand, e.g. in case the property is accessed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, then I think we need to fix this as a testcase

];
}

Expand Down
23 changes: 23 additions & 0 deletions tests/spec/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,29 @@ public function testRead()
$this->assertFalse($schema->deprecated);
}

public function testNullable()
{
/** @var $schema Schema */
$schema = Reader::readFromJson('{"type": "string"}', Schema::class);
$this->assertEquals(Type::STRING, $schema->type);
$this->assertFalse($schema->nullable);

/** @var $schema Schema */
$schema = Reader::readFromJson('{"type": "string", "nullable": false}', Schema::class);
$this->assertEquals(Type::STRING, $schema->type);
$this->assertFalse($schema->nullable);

/** @var $schema Schema */
$schema = Reader::readFromJson('{"type": "string", "nullable": true}', Schema::class);
$this->assertEquals(Type::STRING, $schema->type);
$this->assertTrue($schema->nullable);

// nullable is undefined if no type is given
$schema = Reader::readFromJson('{"oneOf": [{"type": "string"}, {"type": "integer"}]}', Schema::class);
$this->assertNull($schema->type);
$this->assertNull($schema->nullable);
}

public function testReadObject()
{
/** @var $schema Schema */
Expand Down