Skip to content

Commit

Permalink
Preserve properties when $ref is nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
DerManoMann committed Oct 17, 2023
1 parent df8de8e commit c84d201
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/Annotations/AbstractAnnotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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};
}
}
Expand All @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions tests/Fixtures/Scratch/NullRef.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -27,6 +27,7 @@ paths:
oneOf:
- { $ref: '#/components/schemas/repository', description: 'The repository' }
- { type: 'null' }
description: 'The repository'
components:
schemas:
repository: { }

0 comments on commit c84d201

Please sign in to comment.