diff --git a/src/spec/Parameter.php b/src/spec/Parameter.php index 9cc81820..d133ff67 100644 --- a/src/spec/Parameter.php +++ b/src/spec/Parameter.php @@ -115,7 +115,20 @@ protected function performValidation() } } if (!empty($this->content) && !empty($this->schema)) { - $this->addError("A Parameter Object MUST contain either a schema property, or a content property, but not both. "); + $this->addError('A Parameter Object MUST contain either a schema property, or a content property, but not both.'); + } + if (!empty($this->content) && count($this->content) !== 1) { + $this->addError('A Parameter Object with Content property MUST have A SINGLE content type.'); + } + + $supportedSerializationStyles = [ + 'path' => ['simple', 'label', 'matrix'], + 'query' => ['form', 'spaceDelimited', 'pipeDelimited', 'deepObject'], + 'header' => ['simple'], + 'cookie' => ['form'], + ]; + if (isset($supportedSerializationStyles[$this->in]) && !in_array($this->style, $supportedSerializationStyles[$this->in])) { + $this->addError('A Parameter Object DOES NOT support this serialization style.'); } } } diff --git a/tests/spec/ParameterTest.php b/tests/spec/ParameterTest.php index a72ba46e..14557f8c 100644 --- a/tests/spec/ParameterTest.php +++ b/tests/spec/ParameterTest.php @@ -148,4 +148,91 @@ public function testDefaultValuesCookie() $this->assertEquals('form', $parameter->style); $this->assertTrue($parameter->explode); } + + public function testItValidatesSchemaAndContentCombination() + { + /** @var $parameter Parameter */ + $parameter = Reader::readFromYaml(<<<'YAML' +name: token +in: cookie +schema: + type: object +content: + application/json: + schema: + type: object +YAML + , Parameter::class); + + $result = $parameter->validate(); + $this->assertEquals(['A Parameter Object MUST contain either a schema property, or a content property, but not both.'], $parameter->getErrors()); + $this->assertFalse($result); + } + + public function testItValidatesContentCanHaveOnlySingleKey() + { + /** @var $parameter Parameter */ + $parameter = Reader::readFromYaml(<<<'YAML' +name: token +in: cookie +content: + application/json: + schema: + type: object + application/xml: + schema: + type: object +YAML + , Parameter::class); + + $result = $parameter->validate(); + $this->assertEquals(['A Parameter Object with Content property MUST have A SINGLE content type.'], $parameter->getErrors()); + $this->assertFalse($result); + } + + + public function testItValidatesSupportedSerializationStyles() + { + // 1. Prepare test inputs + $specTemplate = << ['simple', 'label', 'matrix'], + 'query' => ['form', 'spaceDelimited', 'pipeDelimited', 'deepObject'], + 'header' => ['simple'], + 'cookie' => ['form'], + ]; + $badCombinations = [ + 'path' => ['unknown', 'form', 'spaceDelimited', 'pipeDelimited', 'deepObject'], + 'query' => ['unknown', 'simple', 'label', 'matrix'], + 'header' => ['unknown', 'form', 'spaceDelimited', 'pipeDelimited', 'deepObject', 'matrix'], + 'cookie' => ['unknown', 'spaceDelimited', 'pipeDelimited', 'deepObject', 'matrix', 'label', 'matrix'], + ]; + + // 2. Run tests for valid input + foreach($goodCombinations as $in=>$styles) { + foreach($styles as $style) { + /** @var $parameter Parameter */ + $parameter = Reader::readFromYaml(sprintf($specTemplate, $in, $style) , Parameter::class); + $result = $parameter->validate(); + $this->assertEquals([], $parameter->getErrors()); + $this->assertTrue($result); + } + } + + // 2. Run tests for invalid input + foreach($badCombinations as $in=>$styles) { + foreach($styles as $style) { + /** @var $parameter Parameter */ + $parameter = Reader::readFromYaml(sprintf($specTemplate, $in, $style) , Parameter::class); + $result = $parameter->validate(); + $this->assertEquals(['A Parameter Object DOES NOT support this serialization style.'], $parameter->getErrors()); + $this->assertFalse($result); + } + } + } } \ No newline at end of file