3131 * Class Schema
3232 * @package Swaggest\JsonSchema
3333 */
34- class Schema extends JsonSchema implements MetaHolder, SchemaContract
34+ class Schema extends JsonSchema implements MetaHolder, SchemaContract, HasDefault
3535{
3636 const ENUM_NAMES_PROPERTY = 'x-enum-names ' ;
3737 const CONST_PROPERTY = 'const ' ;
38+ const DEFAULT_PROPERTY = 'default ' ;
3839
3940 const DEFAULT_MAPPING = 'default ' ;
4041
@@ -423,7 +424,7 @@ private function processOneOf($data, Context $options, $path)
423424 try {
424425 $ result = self ::unboolSchema ($ item )->process ($ data , $ options , $ path . '->oneOf[ ' . $ index . '] ' );
425426 $ successes ++;
426- if ($ successes > 1 || $ options ->skipValidation ) {
427+ if ($ successes > 1 || $ options ->skipValidation ) { // @phpstan-ignore-line
427428 break ;
428429 }
429430 } catch (InvalidValue $ exception ) {
@@ -795,9 +796,23 @@ private function processObject($data, Context $options, $path, $result = null)
795796 && $ properties !== null
796797 ) {
797798 foreach ($ properties as $ key => $ property ) {
799+ $ allowNull = false ;
800+ if ($ property instanceof HasDefault) {
801+ if (!$ property ->hasDefault ()) {
802+ continue ;
803+ }
804+
805+ $ allowNull = true ;
806+ }
807+
798808 // todo check when property is \stdClass `{}` here (RefTest)
799- if ($ property instanceof SchemaContract && null !== $ default = $ property -> getDefault () ) {
809+ if ($ property instanceof SchemaContract) {
800810 if (!array_key_exists ($ key , $ array )) {
811+ $ default = $ property ->getDefault ();
812+ if (null === $ default && !$ allowNull ) { // @phpstan-ignore-line
813+ continue ;
814+ }
815+
801816 $ defaultApplied [$ key ] = true ;
802817 $ array [$ key ] = $ default ;
803818 }
@@ -961,6 +976,9 @@ private function processArray($data, Context $options, $path, $result)
961976 $ this ->items = self ::unboolSchema ($ this ->items );
962977 if ($ this ->items instanceof SchemaContract) {
963978 $ items = array ();
979+ /**
980+ * @var null|bool|Schema $additionalItems
981+ */
964982 $ additionalItems = $ this ->items ;
965983 } elseif ($ this ->items === null ) { // items defaults to empty schema so everything is valid
966984 $ items = array ();
@@ -973,7 +991,6 @@ private function processArray($data, Context $options, $path, $result)
973991
974992 /**
975993 * @var Schema|Schema[] $items
976- * @var null|bool|Schema $additionalItems
977994 */
978995 $ itemsLen = is_array ($ items ) ? count ($ items ) : 0 ;
979996 $ index = 0 ;
@@ -1407,6 +1424,14 @@ public function getDefault()
14071424 return $ this ->default ;
14081425 }
14091426
1427+ /**
1428+ * @return bool
1429+ */
1430+ public function hasDefault ()
1431+ {
1432+ return array_key_exists (self ::DEFAULT_PROPERTY , $ this ->__arrayOfData );
1433+ }
1434+
14101435 /**
14111436 * @nolint
14121437 */
0 commit comments