Skip to content

Explicitly disable validation for default and const values, fixes #119 #120

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
May 27, 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
2 changes: 1 addition & 1 deletion src/JsonSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public static function setUpProperties($properties, JsonBasicSchema $ownerSchema
$ownerSchema->addPropertyMapping('$schema', self::names()->schema);
$properties->title = JsonBasicSchema::string();
$properties->description = JsonBasicSchema::string();
$properties->default = new JsonBasicSchema();
$properties->default = (object)array();
$properties->multipleOf = JsonBasicSchema::number();
$properties->multipleOf->minimum = 0;
$properties->multipleOf->exclusiveMinimum = true;
Expand Down
3 changes: 3 additions & 0 deletions src/Wrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ public function getProperties()
*/
public function getProperty($name)
{
if ($name === Schema::CONST_PROPERTY || $name === Schema::DEFAULT_PROPERTY) {
return null;
}
return isset($this->schema->properties[$name]) ? $this->schema->properties[$name] : null;
}

Expand Down
25 changes: 25 additions & 0 deletions tests/src/Helper/WithConst.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Swaggest\JsonSchema\Tests\Helper;

use Swaggest\JsonSchema\Constraint\Properties;
use Swaggest\JsonSchema\Schema;
use Swaggest\JsonSchema\Structure\ClassStructure;

class WithConst extends ClassStructure
{
/** @var string */
public $foo;

/**
* @param Properties|static $properties
* @param Schema $ownerSchema
*/
public static function setUpProperties($properties, Schema $ownerSchema)
{
$properties->foo = Schema::string();
$properties->foo->const = "abc";

$ownerSchema->type = Schema::OBJECT;
}
}
7 changes: 7 additions & 0 deletions tests/src/PHPUnit/ClassStructure/ClassStructureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Swaggest\JsonSchema\Tests\Helper\SampleProperties;
use Swaggest\JsonSchema\Tests\Helper\SampleStructure;
use Swaggest\JsonSchema\Tests\Helper\StructureWithItems;
use Swaggest\JsonSchema\Tests\Helper\WithConst;

class ClassStructureTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -142,4 +143,10 @@ public function testPatternPropertiesMismatch()
$properties->setXValue('xfoo', 'bar');
}

function testGeneratedValid()
{
$v = WithConst::import((object)array('foo' => 'abc'));
$this->assertSame('{"foo":"abc"}', json_encode($v));
}

}