SerializerTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php declare(strict_types=1);
  2. namespace OpenApiTests;
  3. use OpenApi\Annotations;
  4. use OpenApi\Serializer;
  5. use const OpenApi\UNDEFINED;
  6. class SerializerTest extends OpenApiTestCase
  7. {
  8. private function getExpected()
  9. {
  10. $path = new Annotations\PathItem([]);
  11. $path->path = '/products';
  12. $path->post = new Annotations\Post([]);
  13. $path->post->tags = ['products'];
  14. $path->post->summary = 's1';
  15. $path->post->description = 'd1';
  16. $path->post->requestBody = new Annotations\RequestBody([]);
  17. $mediaType = new Annotations\MediaType([]);
  18. $mediaType->mediaType = 'application/json';
  19. $mediaType->schema = new Annotations\Schema([]);
  20. $mediaType->schema->type = 'object';
  21. $mediaType->schema->additionalProperties = true;
  22. $path->post->requestBody->content = [$mediaType];
  23. $path->post->requestBody->description = 'data in body';
  24. $path->post->requestBody->x = [];
  25. $path->post->requestBody->x['repository'] = 'def';
  26. $resp = new Annotations\Response([]);
  27. $resp->response = '200';
  28. $resp->description = 'Success';
  29. $content = new Annotations\MediaType([]);
  30. $content->mediaType = 'application/json';
  31. $content->schema = new Annotations\Schema([]);
  32. $content->schema->ref = '#/components/schemas/Pet';
  33. $resp->content = [$content];
  34. $resp->x = [];
  35. $resp->x['repository'] = 'def';
  36. $respRange = new Annotations\Response([]);
  37. $respRange->response = '4XX';
  38. $respRange->description = 'Client error response';
  39. $path->post->responses = [$resp, $respRange];
  40. $expected = new Annotations\OpenApi([]);
  41. $expected->openapi = '3.0.0';
  42. $expected->paths = [
  43. $path,
  44. ];
  45. $info = new Annotations\Info([]);
  46. $info->title = 'Pet store';
  47. $info->version = '1.0';
  48. $expected->info = $info;
  49. $schema = new Annotations\Schema([]);
  50. $schema->schema = 'Pet';
  51. $schema->required = ['name', 'photoUrls'];
  52. $expected->components = new Annotations\Components([]);
  53. $expected->components->schemas = [$schema];
  54. return $expected;
  55. }
  56. public function testDeserializeAnnotation()
  57. {
  58. $serializer = new Serializer();
  59. $json = <<<JSON
  60. {
  61. "openapi": "3.0.0",
  62. "info": {
  63. "title": "Pet store",
  64. "version": "1.0"
  65. },
  66. "paths": {
  67. "/products": {
  68. "post": {
  69. "tags": [
  70. "products"
  71. ],
  72. "summary": "s1",
  73. "description": "d1",
  74. "requestBody": {
  75. "description": "data in body",
  76. "content": {
  77. "application/json": {
  78. "schema": {
  79. "type": "object",
  80. "additionalProperties": true
  81. }
  82. }
  83. },
  84. "x-repository": "def"
  85. },
  86. "responses": {
  87. "200": {
  88. "description": "Success",
  89. "content": {
  90. "application/json": {
  91. "schema": {
  92. "\$ref": "#/components/schemas/Pet"
  93. }
  94. }
  95. },
  96. "x-repository": "def"
  97. },
  98. "4XX": {
  99. "description": "Client error response"
  100. }
  101. }
  102. }
  103. }
  104. },
  105. "components": {
  106. "schemas": {
  107. "Pet": {
  108. "required": [
  109. "name",
  110. "photoUrls"
  111. ]
  112. }
  113. }
  114. }
  115. }
  116. JSON;
  117. /** @var Annotations\OpenApi $annotation */
  118. $annotation = $serializer->deserialize($json, 'OpenApi\Annotations\OpenApi');
  119. $this->assertInstanceOf('OpenApi\Annotations\OpenApi', $annotation);
  120. $this->assertJsonStringEqualsJsonString(
  121. $annotation->toJson(),
  122. $this->getExpected()->toJson()
  123. );
  124. $schema = $annotation->paths['/products']->post->requestBody->content['application/json']->schema;
  125. $this->assertTrue($schema->additionalProperties);
  126. }
  127. public function testPetstoreExample()
  128. {
  129. $serializer = new Serializer();
  130. $openapi = $serializer->deserializeFile(__DIR__.'/ExamplesOutput/petstore.swagger.io.json');
  131. $this->assertInstanceOf('OpenApi\Annotations\OpenApi', $openapi);
  132. $this->assertOpenApiEqualsFile(__DIR__.'/ExamplesOutput/petstore.swagger.io.json', $openapi);
  133. }
  134. /**
  135. * Test for correct deserialize schemas 'allOf' property.
  136. * @throws \Exception
  137. */
  138. public function testDeserializeAllOfProperty()
  139. {
  140. $serializer = new Serializer();
  141. $json = <<<JSON
  142. {
  143. "openapi": "3.0.0",
  144. "info": {
  145. "title": "Pet store",
  146. "version": "1.0"
  147. },
  148. "components": {
  149. "schemas": {
  150. "Dog": {
  151. "allOf": [{
  152. "\$ref": "#/components/schemas/SomeSchema"
  153. }]
  154. },
  155. "Cat": {
  156. "allOf": [{
  157. "\$ref": "#/components/schemas/SomeSchema"
  158. }]
  159. }
  160. }
  161. }
  162. }
  163. JSON;
  164. /* @var $annotation Annotations\OpenApi */
  165. $annotation = $serializer->deserialize($json, Annotations\OpenApi::class);
  166. foreach ($annotation->components->schemas as $schemaObject) {
  167. $this->assertObjectHasAttribute('allOf', $schemaObject);
  168. $this->assertNotSame($schemaObject->allOf, UNDEFINED);
  169. $this->assertIsArray($schemaObject->allOf);
  170. $allOfItem = current($schemaObject->allOf);
  171. $this->assertIsObject($allOfItem);
  172. $this->assertInstanceOf(Annotations\Schema::class, $allOfItem);
  173. $this->assertObjectHasAttribute('ref', $allOfItem);
  174. $this->assertNotSame($allOfItem->ref, UNDEFINED);
  175. $this->assertSame('#/components/schemas/SomeSchema', $allOfItem->ref);
  176. }
  177. }
  178. }