Skip to content

Commit

Permalink
allow define docs request model by endpoint mapping (#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
troytft committed Apr 21, 2022
1 parent 8024b5f commit fbe7016
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Mapping/OpenApi/Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,24 @@ class Endpoint
public ?string $description;
/** @var string[]|string */
public $tags;
public ?string $requestModel;

/**
* @param array|string $options
* @param string[]|string $tags
*/
public function __construct($options = [], string $title = '', ?string $description = null, $tags = [])
public function __construct($options = [], string $title = '', ?string $description = null, $tags = [], ?string $requestModel = null)
{
if (is_string($options)) {
$this->title = $options;
$this->description = $description;
$this->tags = $tags;
$this->requestModel = $requestModel;
} elseif (is_array($options)) {
$this->title = $options['title'] ?? $options['value'] ?? $title;
$this->description = $options['description'] ?? $description;
$this->tags = $options['tags'] ?? $tags;
$this->requestModel = $options['requestModel'] ?? $requestModel;
} else {
throw new \InvalidArgumentException();
}
Expand Down
6 changes: 6 additions & 0 deletions src/Services/OpenApi/SchemaGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,12 @@ private function createOperation(RestApiBundle\Model\OpenApi\EndpointData $endpo
}
}

if ($requestModelType && $endpointData->endpointMapping->requestModel) {
throw new RestApiBundle\Exception\ContextAware\ReflectionMethodAwareException('RequestModel already defined by action arguments', $endpointData->reflectionMethod);
} elseif ($endpointData->endpointMapping->requestModel) {
$requestModelType = new PropertyInfo\Type(PropertyInfo\Type::BUILTIN_TYPE_OBJECT, false, $endpointData->endpointMapping->requestModel);
}

if ($requestModelType && $httpMethod === HttpFoundation\Request::METHOD_GET) {
$operationParameters = array_merge($operationParameters, $this->createQueryParametersFromRequestModel($requestModelType->getClassName()));
} elseif ($requestModelType) {
Expand Down

0 comments on commit fbe7016

Please sign in to comment.