diff --git a/src/Serializer.php b/src/Serializer.php index ea401b456..329c11dcd 100644 --- a/src/Serializer.php +++ b/src/Serializer.php @@ -74,13 +74,13 @@ protected static function isValidAnnotationClass(string $className): bool * * @param class-string $className */ - public function deserialize(string $jsonString, string $className): OA\AbstractAnnotation + public function deserialize(string $jsonString, string $className, ?Context $context = null): OA\AbstractAnnotation { if (!static::isValidAnnotationClass($className)) { throw new OpenApiException($className . ' is not defined in OpenApi PHP Annotations'); } - return $this->doDeserialize(json_decode($jsonString), $className, new Context(['generated' => true])); + return $this->doDeserialize(json_decode($jsonString), $className, $context ?? new Context(['generated' => true])); } /** @@ -88,7 +88,7 @@ public function deserialize(string $jsonString, string $className): OA\AbstractA * * @param class-string $className */ - public function deserializeFile(string $filename, string $format = 'json', string $className = OA\OpenApi::class): OA\AbstractAnnotation + public function deserializeFile(string $filename, string $format = 'json', string $className = OA\OpenApi::class, ?Context $context = null): OA\AbstractAnnotation { if (!static::isValidAnnotationClass($className)) { throw new OpenApiException($className . ' is not a valid OpenApi PHP Annotations'); @@ -101,7 +101,7 @@ public function deserializeFile(string $filename, string $format = 'json', strin $contents = json_encode(Yaml::parse($contents)); } - return $this->doDeserialize(json_decode($contents), $className, new Context(['generated' => true])); + return $this->doDeserialize(json_decode($contents), $className, $context ?? new Context(['generated' => true])); } /** diff --git a/tests/SerializerTest.php b/tests/SerializerTest.php index 641a7c7e2..e2b0210f3 100644 --- a/tests/SerializerTest.php +++ b/tests/SerializerTest.php @@ -8,6 +8,7 @@ use OpenApi\Annotations as OA; use OpenApi\Annotations\OpenApi; +use OpenApi\Context; use OpenApi\Generator; use OpenApi\Serializer; use OpenApi\Tests\Concerns\UsesExamples; @@ -17,7 +18,7 @@ final class SerializerTest extends OpenApiTestCase { use UsesExamples; - private function getExpected(): OpenApi + private function getPetstoreExpected(): OpenApi { $path = new OA\PathItem(['_context' => $this->getContext()]); $path->path = '/products'; @@ -54,7 +55,7 @@ private function getExpected(): OpenApi $path->post->responses = [$resp, $respRange]; $expected = new OpenApi(['_context' => $this->getContext()]); - $expected->openapi = OpenApi::VERSION_3_0_0; + $expected->openapi = OpenApi::VERSION_3_1_0; $expected->paths = [ $path, ]; @@ -74,76 +75,76 @@ private function getExpected(): OpenApi return $expected; } - public function testDeserializeAnnotation(): void + public function testDeserializePetstore(): void { - $serializer = new Serializer(); - $json = <<deserialize($json, OpenApi::class); $this->assertInstanceOf(OpenApi::class, $annotation); $this->assertJsonStringEqualsJsonString( $annotation->toJson(), - $this->getExpected()->toJson() + $this->getPetstoreExpected()->toJson() ); $schema = $annotation->paths['/products']->post->requestBody->content['application/json']->schema; @@ -153,7 +154,7 @@ public function testDeserializeAnnotation(): void public function testPetstoreExample(): void { $serializer = new Serializer(); - $spec = self::examplePath('petstore/petstore-3.0.0.yaml'); + $spec = self::examplePath('petstore/petstore-3.1.0.yaml'); $openapi = $serializer->deserializeFile($spec, 'yaml'); $this->assertInstanceOf(OpenApi::class, $openapi); $this->assertSpecEquals(file_get_contents($spec), $openapi->toYaml()); @@ -166,30 +167,32 @@ public function testPetstoreExample(): void */ public function testDeserializeAllOfProperty(): void { - $serializer = new Serializer(); $json = <<deserialize($json, OpenApi::class); @@ -212,4 +215,28 @@ public function testValidAnnotationsListComplete(string $annotation): void $staticProperties = (new \ReflectionClass((Serializer::class)))->getStaticProperties(); $this->assertArrayHasKey($annotation, array_flip($staticProperties['VALID_ANNOTATIONS'])); } + + public function testSerializeWithContext(): void + { + $json = <<deserialize($json, OpenApi::class, new Context(['version' => '3.2.0', 'generated' => true])); + + $this->assertJsonStringEqualsJsonString( + str_replace('"tags"', '"openapi":"3.2.0", "tags"', $json), + $annotation->toJson(), + ); + } }