/** * @param SwaggerDocument $document * * @return void */ public function process(SwaggerDocument $document) { $definition = $document->getDefinition(); if (!isset($definition->responses)) { $definition->responses = []; } if (!isset($definition->responses['ServerError'])) { $definition->responses['ServerError'] = ['description' => 'Server Error', 'schema' => ['$ref' => '#/definitions/VndError']]; } if (!isset($definition->responses['InputError'])) { $definition->responses['InputError'] = ['description' => 'Input Error', 'schema' => ['$ref' => '#/definitions/VndError']]; } if (!isset($definition->definitions)) { $definition->definitions = []; } if (!isset($definition->definitions['VndError'])) { $definition->definitions['VndError'] = ['type' => 'object', 'required' => ['message', 'logref'], 'properties' => ['message' => ['type' => 'string'], 'logref' => ['type' => 'string']]]; } foreach ($definition->paths as &$operations) { foreach ($operations as &$operation) { if (!isset($operation['responses']['500'])) { $operation['responses']['500'] = ['headers' => ['Content-Type' => 'application/vnd.error+json'], '$ref' => '#/responses/ServerError']; } if (!isset($operation['responses']['400'])) { $operation['responses']['400'] = ['headers' => ['Content-Type' => 'application/vnd.error+json'], '$ref' => '#/responses/InputError']; } } } }
/** * @param BundleInterface $bundle * @param SwaggerDocument $document * @param string $relativeNamespace */ public function generate(BundleInterface $bundle, SwaggerDocument $document, $relativeNamespace = 'Model\\Resources') { $dir = $bundle->getPath(); $parameters = ['namespace' => $bundle->getNamespace(), 'bundle' => $bundle->getName(), 'resource_namespace' => $relativeNamespace]; foreach ($document->getResourceSchemas() as $typeName => $spec) { $resourceFile = "{$dir}/" . str_replace('\\', '/', $relativeNamespace) . "/{$typeName}.php"; $this->renderFile('resource.php.twig', $resourceFile, array_merge($parameters, $spec, ['resource' => $typeName, 'resource_class' => $typeName])); } }
/** * @param SwaggerDocument $document * * @return void */ public function process(SwaggerDocument $document) { $definition = $document->getDefinition(); if (!isset($definition->definitions)) { $definition->definitions = []; } if (!isset($definition->definitions['Pet'])) { $definition->definitions['Pet'] = ['type' => 'object', 'required' => ['message', 'logref'], 'properties' => ['message' => ['type' => 'string'], 'logref' => ['type' => 'string']]]; } }
/** * @param string $fullPath * @param string $method * * @return object * @throws ApiResponseErrorException */ private function getJsonForLastRequest($fullPath, $method) { $method = strtolower($method); $response = $this->client->getResponse(); $responseContent = $response->getContent(); if ($response->getStatusCode() === 204 && !$responseContent) { return null; } $basePath = isset(self::$document->getDefinition()->basePath) ? self::$document->getDefinition()->basePath : ''; $relativePath = !$basePath ? $fullPath : substr($fullPath, strlen($basePath)); $json = json_decode($responseContent); if ($response->getStatusCode() !== 200) { if (!isset($this->validateErrorResponse) || !$this->validateErrorResponse) { $this->assertResponseBodyMatch($json, self::$schemaManager, $fullPath, $method, $response->getStatusCode()); } throw new ApiResponseErrorException($json, $response->getStatusCode()); } if (self::$schemaManager->hasPath(['paths', $relativePath, $method, 'responses', '200'])) { $this->assertNotNull($json, "Not valid JSON: {$responseContent}"); $headers = []; foreach ($response->headers->all() as $key => $values) { $headers[str_replace(' ', '-', ucwords(str_replace('-', ' ', $key)))] = $values[0]; } $this->assertResponseHeadersMatch($headers, self::$schemaManager, $fullPath, $method, 200); $this->assertResponseBodyMatch($json, self::$schemaManager, $fullPath, $method, 200); return $json; } // If there is no response definition, the API should return 204 No Content. // With the current spec the behavior is undefined, this must be fixed. throw new \UnexpectedValueException("There is no 200 response definition for {$relativePath}:{$method}. For empty responses, use 204."); }
/** * @test */ public function canResolveParameterSchemaReferences() { $document = new SwaggerDocument('src/Tests/Functional/PetStore/app/instagram.yml'); $pathDefinitions = $document->getPathDefinitions(); $argumentPseudoSchema = $pathDefinitions['/users/{user-id}']['parameters'][0]; $this->assertArrayNotHasKey('$ref', $argumentPseudoSchema); $this->assertArrayHasKey('in', $argumentPseudoSchema); $this->assertSame('user-id', $argumentPseudoSchema['name']); }