Skip to content
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

Preserve properties when $ref is nullable #1487

Merged
Merged
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
24 changes: 17 additions & 7 deletions src/Annotations/AbstractAnnotation.php
Original file line number Diff line number Diff line change
@@ -221,23 +221,24 @@ public function merge(array $annotations, bool $ignore = false): array
*/
public function mergeProperties($object): void
{
$defaultValues = get_class_vars(get_class($this));
$currentValues = get_object_vars($this);
foreach ($object as $property => $value) {
if ($property === '_context') {
continue;
}
if ($currentValues[$property] === $defaultValues[$property]) { // Overwrite default values
if (Generator::isDefault($currentValues[$property])) {
// Overwrite default values
$this->{$property} = $value;
continue;
}
if ($property === '_unmerged') {
$this->_unmerged = array_merge($this->_unmerged, $value);
continue;
}
if ($currentValues[$property] !== $value) { // New value is not the same?
if ($defaultValues[$property] === $value) { // but is the same as the default?
continue; // Keep current, no notice
if ($currentValues[$property] !== $value) {
// New value is not the same?
if (Generator::isDefault($value)) {
continue;
}
$identity = method_exists($object, 'identity') ? $object->identity() : get_class($object);
$context1 = $this->_context;
@@ -355,11 +356,10 @@ public function jsonSerialize()
if (isset($data->ref)) {
// Only specific https://github.com/OAI/OpenAPI-Specification/blob/3.1.0/versions/3.1.0.md#reference-object
$ref = ['$ref' => $data->ref];
$defaultValues = get_class_vars(get_class($this));
if ($this->_context->version === OpenApi::VERSION_3_1_0) {
foreach (['summary', 'description'] as $prop) {
if (property_exists($this, $prop)) {
if ($this->{$prop} !== $defaultValues[$prop]) {
if (!Generator::isDefault($this->{$prop})) {
$ref[$prop] = $data->{$prop};
}
}
@@ -373,6 +373,16 @@ public function jsonSerialize()
$ref['nullable'] = $data->nullable;
}
unset($data->nullable);

// preserve other properties
foreach (get_object_vars($this) as $property => $value) {
if ('_' == $property[0] || in_array($property, ['ref', 'nullable'])) {
continue;
}
if (!Generator::isDefault($value)) {
$ref[$property] = $value;
}
}
}
$data = (object) $ref;
}
1 change: 1 addition & 0 deletions tests/Fixtures/Scratch/NullRef.yaml
Original file line number Diff line number Diff line change
@@ -27,6 +27,7 @@ paths:
oneOf:
- { $ref: '#/components/schemas/repository' }
nullable: true
description: 'The repository'
components:
schemas:
repository: { }
1 change: 1 addition & 0 deletions tests/Fixtures/Scratch/NullRef31.yaml
Original file line number Diff line number Diff line change
@@ -27,6 +27,7 @@ paths:
oneOf:
- { $ref: '#/components/schemas/repository', description: 'The repository' }
- { type: 'null' }
description: 'The repository'
components:
schemas:
repository: { }