From a9bb8654d33aeac1db8f51b157bf0c12ad8ac6c5 Mon Sep 17 00:00:00 2001 From: wuchen90 Date: Wed, 8 Jan 2025 11:42:38 +0100 Subject: [PATCH] fix(metadata): make the schema attribute to fallback to null for parameters in YamlResourceExtractor --- src/Metadata/Extractor/YamlResourceExtractor.php | 2 +- .../Tests/Extractor/Adapter/XmlResourceAdapter.php | 10 ++++++++-- src/Metadata/Tests/Extractor/Adapter/resources.xml | 2 +- src/Metadata/Tests/Extractor/Adapter/resources.yaml | 3 +++ .../Extractor/ResourceMetadataCompatibilityTest.php | 5 ++++- 5 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/Metadata/Extractor/YamlResourceExtractor.php b/src/Metadata/Extractor/YamlResourceExtractor.php index 8bbb03bed69..30838186580 100644 --- a/src/Metadata/Extractor/YamlResourceExtractor.php +++ b/src/Metadata/Extractor/YamlResourceExtractor.php @@ -471,7 +471,7 @@ private function buildParameters(array $resource): ?array $parameters[$key] = new $cl( key: $key, required: $this->phpize($parameter, 'required', 'bool'), - schema: $parameter['schema'], + schema: $parameter['schema'] ?? null, openApi: ($parameter['openapi'] ?? null) ? new Parameter( name: $parameter['openapi']['name'], in: $parameter['in'] ?? 'query', diff --git a/src/Metadata/Tests/Extractor/Adapter/XmlResourceAdapter.php b/src/Metadata/Tests/Extractor/Adapter/XmlResourceAdapter.php index 4a2acbbb069..0b302606fa5 100644 --- a/src/Metadata/Tests/Extractor/Adapter/XmlResourceAdapter.php +++ b/src/Metadata/Tests/Extractor/Adapter/XmlResourceAdapter.php @@ -533,8 +533,14 @@ private function buildParameters(\SimpleXMLElement $resource, ?array $values = n $childNode = $node->addChild('parameter'); $childNode->addAttribute('in', 'query'); $childNode->addAttribute('key', $key); - $childNode->addAttribute('required', $this->parse($value['required'])); - $this->buildValues($childNode->addChild('schema'), $value['schema']); + + if (\array_key_exists('required', $value)) { + $childNode->addAttribute('required', $this->parse($value['required'])); + } + + if (\array_key_exists('schema', $value)) { + $this->buildValues($childNode->addChild('schema'), $value['schema']); + } } } diff --git a/src/Metadata/Tests/Extractor/Adapter/resources.xml b/src/Metadata/Tests/Extractor/Adapter/resources.xml index 45b012242b2..65eebd8c971 100644 --- a/src/Metadata/Tests/Extractor/Adapter/resources.xml +++ b/src/Metadata/Tests/Extractor/Adapter/resources.xml @@ -1,3 +1,3 @@ -someirischemaanotheririschemaCommentapplication/vnd.openxmlformats-officedocument.spreadsheetml.sheetapplication/merge-patch+json+ldapplication/merge-patch+json+ld_foo\d+bazhttps
60120AuthorizationAccept-LanguageAcceptcomment:read_collectioncomment:writebazbazbazbarcomment.another_custom_filteruserIdLorem ipsum dolor sit ametDolor sit ametbarstringapplication/vnd.ms-excelapplication/merge-patch+jsonapplication/merge-patch+jsonpouet\d+barhttphttps60120AuthorizationAccept-Languagecomment:readcomment:writecomment:custombazbazbazbarcomment.custom_filterfoobarcustombazcustomquxcomment:read_collectioncomment:writebarcomment.another_custom_filteruserIdLorem ipsum dolor sit ametDolor sit ametbar/v1/v1Lorem ipsum dolor sit ametDolor sit amet/v1Lorem ipsum dolor sit ametDolor sit amet/v1Lorem ipsum dolor sit ametDolor sit ametLorem ipsum dolor sit ametDolor sit amet +someirischemaanotheririschemaCommentapplication/vnd.openxmlformats-officedocument.spreadsheetml.sheetapplication/merge-patch+json+ldapplication/merge-patch+json+ld_foo\d+bazhttps
60120AuthorizationAccept-LanguageAcceptcomment:read_collectioncomment:writebazbazbazbarcomment.another_custom_filteruserIdLorem ipsum dolor sit ametDolor sit ametbarstringapplication/vnd.ms-excelapplication/merge-patch+jsonapplication/merge-patch+jsonpouet\d+barhttphttps60120AuthorizationAccept-Languagecomment:readcomment:writecomment:custombazbazbazbarcomment.custom_filterfoobarcustombazcustomquxcomment:read_collectioncomment:writebarcomment.another_custom_filteruserIdLorem ipsum dolor sit ametDolor sit ametbar/v1/v1Lorem ipsum dolor sit ametDolor sit amet/v1Lorem ipsum dolor sit ametDolor sit amet/v1Lorem ipsum dolor sit ametDolor sit ametLorem ipsum dolor sit ametDolor sit amet diff --git a/src/Metadata/Tests/Extractor/Adapter/resources.yaml b/src/Metadata/Tests/Extractor/Adapter/resources.yaml index 2c9e21b1c5f..08dbaa80827 100644 --- a/src/Metadata/Tests/Extractor/Adapter/resources.yaml +++ b/src/Metadata/Tests/Extractor/Adapter/resources.yaml @@ -139,6 +139,9 @@ resources: - rel: 'http://www.w3.org/ns/json-ld#error' href: 'http://www.w3.org/ns/hydra/error' + parameters: + date: + key: date formats: json: null jsonld: null diff --git a/src/Metadata/Tests/Extractor/ResourceMetadataCompatibilityTest.php b/src/Metadata/Tests/Extractor/ResourceMetadataCompatibilityTest.php index 030f754e90c..fd16dac7b8e 100644 --- a/src/Metadata/Tests/Extractor/ResourceMetadataCompatibilityTest.php +++ b/src/Metadata/Tests/Extractor/ResourceMetadataCompatibilityTest.php @@ -442,6 +442,9 @@ final class ResourceMetadataCompatibilityTest extends TestCase 'links' => [ ['rel' => 'http://www.w3.org/ns/json-ld#error', 'href' => 'http://www.w3.org/ns/hydra/error'], ], + 'parameters' => [ + 'date' => ['key' => 'date'], + ], ], ], ], @@ -761,7 +764,7 @@ private function withParameters(array $values): ?array $parameters = []; foreach ($values as $k => $value) { - $parameters[$k] = new QueryParameter(key: $value['key'], required: $value['required'], schema: $value['schema']); + $parameters[$k] = new QueryParameter(key: $value['key'] ?? null, required: $value['required'] ?? null, schema: $value['schema'] ?? null); } return $parameters;