From 551c62ad8372a0e03d3ccfdd1d7f3f1c142de8f2 Mon Sep 17 00:00:00 2001 From: Martin Rademacher Date: Wed, 18 Oct 2023 09:51:31 +1300 Subject: [PATCH] Preserve properties when `$ref` is nullable (#1487) --- src/Annotations/AbstractAnnotation.php | 24 +++++++++++++++++------- tests/Fixtures/Scratch/NullRef.yaml | 1 + tests/Fixtures/Scratch/NullRef31.yaml | 1 + 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/Annotations/AbstractAnnotation.php b/src/Annotations/AbstractAnnotation.php index a6e27785..1c8bac50 100644 --- a/src/Annotations/AbstractAnnotation.php +++ b/src/Annotations/AbstractAnnotation.php @@ -221,13 +221,13 @@ 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; } @@ -235,9 +235,10 @@ public function mergeProperties($object): void $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; } diff --git a/tests/Fixtures/Scratch/NullRef.yaml b/tests/Fixtures/Scratch/NullRef.yaml index 540a68f6..8395936b 100644 --- a/tests/Fixtures/Scratch/NullRef.yaml +++ b/tests/Fixtures/Scratch/NullRef.yaml @@ -27,6 +27,7 @@ paths: oneOf: - { $ref: '#/components/schemas/repository' } nullable: true + description: 'The repository' components: schemas: repository: { } diff --git a/tests/Fixtures/Scratch/NullRef31.yaml b/tests/Fixtures/Scratch/NullRef31.yaml index 203737c7..4e62d4bb 100644 --- a/tests/Fixtures/Scratch/NullRef31.yaml +++ b/tests/Fixtures/Scratch/NullRef31.yaml @@ -27,6 +27,7 @@ paths: oneOf: - { $ref: '#/components/schemas/repository', description: 'The repository' } - { type: 'null' } + description: 'The repository' components: schemas: repository: { }