-
Notifications
You must be signed in to change notification settings - Fork 96
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
nullable oneOf/anyOf value rejected #146
Comments
Here's a 3.1.0 example (which I think is not supported by this library yet) that has the same result: <?php
declare(strict_types=1);
namespace League\OpenAPIValidation\Tests\FromCommunity;
use GuzzleHttp\Psr7\Response;
use League\OpenAPIValidation\PSR7\OperationAddress;
use League\OpenAPIValidation\PSR7\ValidatorBuilder;
use League\OpenAPIValidation\Tests\PSR7\BaseValidatorTest;
final class IssueWithNullableMergeTest extends BaseValidatorTest
{
public function testNullableMergeOneOf(): void
{
$yaml = /** @lang yaml */
<<<'YAML'
openapi: 3.1.0
paths:
/api/nullable-merge:
get:
description: 'Test'
responses:
'200':
description: 'ok'
content:
application/json:
schema:
$ref: "#/components/schemas/Thing"
components:
schemas:
FooResult:
type: object
properties:
id:
type: integer
foo:
type: string
Thing:
type: object
properties:
result:
oneOf:
- $ref: "#/components/schemas/FooResult"
- type: null
YAML;
$validator = (new ValidatorBuilder())->fromYaml($yaml)->getResponseValidator();
$operation = new OperationAddress('/api/nullable-merge', 'get');
$responseContent = /** @lang JSON */
'
{
"result": null
}
';
$response = new Response(200, ['Content-Type' => 'application/json'], $responseContent);
$validator->validate($operation, $response);
$this->addToAssertionCount(1);
}
} |
This succeeds but I'm not sure if it's allowed according to spec: (
|
3.1 is not supported yet. I will check the given issue about nullables |
As far as I understand the problem is that at the moment validator validates In OAS 3.0.3 and 3.1 they had clarifyied this behaviour it still says it's proposal but it's actually in the spec and in the release notes.
I think we can adopt these changes and consider everything nullable by default (this follows the common principle of OAS which is every definition can only make constraints more strict, but not loose) @cebe WDYT? Can we have |
Currently this is fixable with this kind of patch. But serializing schema is not really plays well with performance, so we need somehow check if property Index: src/Schema/SchemaValidator.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/Schema/SchemaValidator.php b/src/Schema/SchemaValidator.php
--- a/src/Schema/SchemaValidator.php (revision a87c402b6c9af07e4638ab542031c606483f75bd)
+++ b/src/Schema/SchemaValidator.php (date 1635374751548)
@@ -56,8 +56,19 @@
$breadCrumb = $breadCrumb ?? new BreadCrumb();
try {
+
+ $nullable = true;
+ $rawSchema = $schema->getSerializableData();
+ if (!isset($rawSchema->nullable)) {
+ if ($schema->type !== null) {
+ $nullable = false;
+ }
+ } else {
+ $nullable = $rawSchema->nullable;
+ }
+
// These keywords are not part of the JSON Schema at all (new to OAS)
- (new Nullable($schema))->validate($data, $schema->nullable);
+ (new Nullable($schema))->validate($data, $nullable);
// We don't want to validate any more if the value is a valid Null
if ($data === null) { |
according to what you cited from the spec it should be
can't you just do |
|
Looks like the default value of nullable should depend on whether
|
So this can be adjusted in php-openapi, but you may also check whether a type is explicitly defined in the current schema, if there is no type, ignore the nullable value. |
When the type IS set - it's the easy one, I agree. But when it's NOT set - I see three cases:
components:
schemas:
FooResult: {}
components:
schemas:
FooResult:
nullable: false
components:
schemas:
FooResult:
nullable: true If For other cases - yes, we can do more smart checks. checking type first if it's present. |
If the type is not set you must not check nullable at all.
So in all cases there is no type sepecified, i.e. no type check is performed, therefore also no check for null. I think this is part of the reason why nullable is removed in openapi 3.1 and instead null is part of the type array. |
@scaytrase would be great to have your opinion on this change: cebe/php-openapi#132 |
FooResult
is not nullable.BarResult
is nullable.Thing.result
can be one ofFooResult
orBarResult
, so my expectation is thatnull
should be an acceptable value. Validation fails with:This same issue happens if
oneOf:
in the above snippet is replaced withanyOf:
.Swagger UI shows the
nullable: true
constraint as expected:The text was updated successfully, but these errors were encountered: