Skip to content

Commit

Permalink
nullable type allows null value v2
Browse files Browse the repository at this point in the history
  • Loading branch information
Zrnik committed May 31, 2024
1 parent 5044462 commit c1eb175
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/PHPUnit/OpenApiSchemaCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,20 @@ public function assertSchemaReturnsCorrectJson(object $schemaObject, ?string $me

/** @var string|class-string $forcedType */
foreach ($forcedTypes as $forcedType) {
/** @noinspection NotOptimalIfConditionsInspection */
/** @noinspection PhpParamsInspection */
if ($this->objectExists($forcedType) && ($value instanceof $forcedType) && $this->isSchemaClass($value)) {
/** @noinspection PhpParamsInspection */
$this->assertSchemaReturnsCorrectJson($value);
continue 2;
}

$types[] = $forcedType;
}

if($schemaAttribute->nullable === true) {
$types[] = 'null';
}
}

if ($forcedType === null) {
Expand All @@ -92,15 +99,15 @@ public function assertSchemaReturnsCorrectJson(object $schemaObject, ?string $me
*/
foreach ($reflectionTypeContainer->getTypes() as $reflectionNamedType) {
$types[] = $reflectionNamedType->getName();
if($reflectionNamedType->allowsNull()) {
if ($reflectionNamedType->allowsNull()) {
$types[] = 'null';
}
}
}

if ($reflectionTypeContainer instanceof ReflectionNamedType) {
$types[] = $reflectionTypeContainer->getName();
if($reflectionTypeContainer->allowsNull()) {
if ($reflectionTypeContainer->allowsNull()) {
$types[] = 'null';
}
}
Expand All @@ -109,7 +116,7 @@ public function assertSchemaReturnsCorrectJson(object $schemaObject, ?string $me
/** @var ReflectionNamedType $reflectionNamedType */
foreach ($reflectionTypeContainer->getTypes() as $reflectionNamedType) {
$types[] = $reflectionNamedType->getName();
if($reflectionNamedType->allowsNull()) {
if ($reflectionNamedType->allowsNull()) {
$types[] = 'null';
}
}
Expand Down
3 changes: 3 additions & 0 deletions tests/ExampleSchema/SchemaClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public function __construct(
public readonly ?string $nullableText = 'nullable-text',
#[Property(nullable: true)]
public readonly ?string $nullableTextIsNull = null,
#[Property(type: 'string', nullable: true)]
public readonly ?string $nullableWithForcedTypeTextIsNull = null,
#[Property]
public readonly RuntimeException|LogicException|MisconfiguredOpenApiGeneratorException $unionExceptionProperty = new RuntimeException(),
#[Property(type: UnrelatedSchemaClass::class)]
Expand All @@ -48,6 +50,7 @@ public function jsonSerialize(): array
'text' => $this->text,
'nullableText' => $this->nullableText,
'nullableTextIsNull' => $this->nullableTextIsNull,
'nullableWithForcedTypeTextIsNull' => $this->nullableWithForcedTypeTextIsNull,
'unionExceptionProperty' => $this->unionExceptionProperty,
'unrelatedSchema' => new UnrelatedSchemaClass(),
'unrelatedNonSchemaClass' => new NotASchemaClass(),
Expand Down

0 comments on commit c1eb175

Please sign in to comment.