diff --git a/src/Request/RequestTransformer.php b/src/Request/RequestTransformer.php index 10df918..cf55551 100644 --- a/src/Request/RequestTransformer.php +++ b/src/Request/RequestTransformer.php @@ -16,6 +16,7 @@ use KleijnWeb\SwaggerBundle\Exception\InvalidParametersException; use KleijnWeb\SwaggerBundle\Exception\MalformedContentException; use KleijnWeb\SwaggerBundle\Exception\UnsupportedContentTypeException; +use Symfony\Component\Serializer\Encoder\JsonDecode; /** * @author John Kleijn @@ -45,12 +46,20 @@ public function __construct(ContentDecoder $contentDecoder) */ public function coerceRequest(Request $request, array $operationDefinition) { + // TODO HACK for https://github.com/kleijnweb/swagger-bundle/issues/24 + $originalContent = $request->getContent(); + $stdClassContent = null; + if ($originalContent) { + $decoder = new JsonDecode(false); + $stdClassContent = (object)$decoder->decode($originalContent, 'json'); + } + $content = $this->contentDecoder->decodeContent($request, $operationDefinition); // This modifies the Request object (and adds the content to the 'attributes' ParameterBag $this->coerceRequestParameters($request, $operationDefinition, $content); - $this->validateRequest($request, $operationDefinition); + $this->validateRequest($request, $operationDefinition, $stdClassContent); // Needed to be able to set the decoded body $request->initialize( @@ -149,16 +158,17 @@ public function coerceRequestParameters(Request $request, array $operationDefini /** * TODO Move to new RequestValidator * - * @param Request $request - * @param array $operationDefinition + * @param Request $request + * @param array $operationDefinition + * @param \stdClass $content * * @throws InvalidParametersException * @throws UnsupportedException */ - private function validateRequest(Request $request, array $operationDefinition) + private function validateRequest(Request $request, array $operationDefinition, \stdClass $content = null) { // This retrieves the modified parameters - $parameters = $this->assembleParameterDataForValidation($request, $operationDefinition); + $parameters = $this->assembleParameterDataForValidation($request, $operationDefinition, $content); // Validate the parameters using a schema created from the operation definition $validator = new Validator(); @@ -177,14 +187,18 @@ private function validateRequest(Request $request, array $operationDefinition) /** * TODO Move to new RequestValidator * - * @param Request $request - * @param array $operationDefinition + * @param Request $request + * @param array $operationDefinition + * @param \stdClass $content * * @return \stdClass * @throws UnsupportedException */ - private function assembleParameterDataForValidation(Request $request, array $operationDefinition) - { + private function assembleParameterDataForValidation( + Request $request, + array $operationDefinition, + \stdClass $content = null + ) { if (!isset($operationDefinition['parameters'])) { return new \stdClass; } @@ -208,7 +222,21 @@ private function assembleParameterDataForValidation(Request $request, array $ope if (!$request->$paramBagName->has($paramName)) { continue; } + if ($paramDefinition['in'] === 'body' && $content) { + $parameters[$paramName] = $content; + continue; + } $parameters[$paramName] = $request->$paramBagName->get($paramName); + + // TODO HACK related to https://github.com/kleijnweb/swagger-bundle/issues/24 + if (isset($paramDefinition['format'])) { + if ($paramDefinition['format'] === 'date') { + $parameters[$paramName] = $parameters[$paramName]->format('Y-m-d'); + } + if ($paramDefinition['format'] === 'date-time') { + $parameters[$paramName] = $parameters[$paramName]->format(\DateTime::W3C); + } + } } // TODO Hack, probably not the best performing of solutions diff --git a/src/Tests/Request/RequestTransformerTest.php b/src/Tests/Request/RequestTransformerTest.php index b7bce44..69d20f4 100644 --- a/src/Tests/Request/RequestTransformerTest.php +++ b/src/Tests/Request/RequestTransformerTest.php @@ -10,6 +10,7 @@ use KleijnWeb\SwaggerBundle\Request\RequestTransformer; use KleijnWeb\SwaggerBundle\Request\Transformer\ContentDecoder; +use KleijnWeb\SwaggerBundle\Request\Transformer\ParameterCoercer; use Symfony\Component\HttpFoundation\Request; /** @@ -113,6 +114,8 @@ public function canOmitParameterWhenNotExplicitlyMarkedAsRequired() $transformer = new RequestTransformer($this->contentDecoderMock); $content = '[]'; + + // TODO: This will break: content (body) vs query $request = new Request([], [], [], [], [], [], $content); $operationDefinition = [ @@ -130,6 +133,40 @@ public function canOmitParameterWhenNotExplicitlyMarkedAsRequired() $this->assertSame([], $request->getContent()); } + /** + * @test + */ + public function willConstructDate() + { + $this->contentDecoderMock = $this + ->getMockBuilder('KleijnWeb\SwaggerBundle\Request\Transformer\ContentDecoder') + ->disableOriginalConstructor() + ->getMock(); + + $transformer = new RequestTransformer($this->contentDecoderMock); + $request = new Request(['foo' => "2015-01-01"], [], [], [], [], []); + + $operationDefinition = [ + 'parameters' => [ + [ + 'name' => 'foo', + 'in' => 'query', + 'type' => 'string', + 'format' => 'date' + ] + ] + ]; + + $transformer->coerceRequest($request, $operationDefinition); + + $expected = ParameterCoercer::coerceParameter($operationDefinition['parameters'][0], "2015-01-01"); + + // Sanity check + $this->assertInstanceOf('DateTime', $expected); + + $this->assertEquals($expected, $request->get('foo')); + } + /** * @test * @expectedException \KleijnWeb\SwaggerBundle\Exception\InvalidParametersException