-
-
Notifications
You must be signed in to change notification settings - Fork 900
Make "hydra:mapping" item's property nullable #3877
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
Conversation
Turns out |
@@ -121,7 +121,10 @@ public function buildSchema(string $className, string $format = 'jsonld', string | |||
'properties' => [ | |||
'@type' => ['type' => 'string'], | |||
'variable' => ['type' => 'string'], | |||
'property' => ['type' => 'string'], | |||
'property' => [ | |||
'nullable' => true, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'nullable' => true
is only for OpenAPI < 3.1.
I don't think we should add it here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok. the change from string to 'type' => ['string', 'null'],
broke Behat pipelien, I'll update the tests as well if you're OK with the change ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes I think this change is necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems that 'null'
is valid type starting on Open-API 3.1 and the schema generated by the api:openapi:export
command is at "3.0.2", so it wont pass swagger-cli validation (which is normal). I could check the version to add nullable
or ['string', 'null']
combo, but I don't think you have control over the generated minor version. Any idea on how to solve this ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's more or less what it has been done here:
core/src/JsonSchema/TypeFactory.php
Lines 155 to 184 in f53bb64
private function addNullabilityToTypeDefinition(array $jsonSchema, Type $type, ?Schema $schema): array | |
{ | |
if ($schema && Schema::VERSION_SWAGGER === $schema->getVersion()) { | |
return $jsonSchema; | |
} | |
if (!$type->isNullable()) { | |
return $jsonSchema; | |
} | |
if (\array_key_exists('$ref', $jsonSchema)) { | |
return [ | |
'nullable' => true, | |
'anyOf' => [$jsonSchema], | |
]; | |
} | |
if ($schema && Schema::VERSION_JSON_SCHEMA === $schema->getVersion()) { | |
return array_merge( | |
$jsonSchema, | |
[ | |
'type' => \is_array($jsonSchema['type']) | |
? array_merge($jsonSchema['type'], ['null']) | |
: [$jsonSchema['type'], 'null'], | |
] | |
); | |
} | |
return array_merge($jsonSchema, ['nullable' => true]); | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed ! I've kept nullable for openapi
version, but if it's not necessary I'll move it to a ternary.
$mappingPropDefinition = ['type' => 'string', 'nullable' => true]; | ||
break; | ||
default: | ||
$mappingPropDefinition = ['type' => 'string']; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you initialize the property before the switch and rename it $nullableStringDefinition
? In case we want to reuse it elsewhere.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed!
Thank you @GaryPEGEOT. |
Context
My entites have the following filter, to select properties. Parameter name have been overrode.
* @ApiFilter(PropertyFilter::class, arguments={"parameterName": "fields"})
And I got the following error in my tests, using
assertMatchesResourceCollectionJsonSchema
assertion