Skip to content

Commit

Permalink
added body param request support (#702)
Browse files Browse the repository at this point in the history
  • Loading branch information
romalytvynenko authored Feb 2, 2025
1 parent ce5abfb commit 23f0fd0
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 18 deletions.
24 changes: 24 additions & 0 deletions src/Attributes/BodyParameter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Dedoc\Scramble\Attributes;

use Attribute;

#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_ALL)]
class BodyParameter extends Parameter
{
public function __construct(
string $name,
?string $description = null,
?bool $required = null,
$deprecated = false,
?string $type = null,
?string $format = null,
bool $infer = true,
mixed $default = new MissingValue,
mixed $example = new MissingValue,
array $examples = [],
) {
parent::__construct('body', $name, $description, $required, $deprecated, $type, $format, $infer, $default, $example, $examples);
}
}
5 changes: 2 additions & 3 deletions src/Attributes/CookieParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,18 @@
#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_ALL)]
class CookieParameter extends Parameter
{
public readonly bool $required;

public function __construct(
string $name,
?string $description = null,
?bool $required = null,
$deprecated = false,
?string $type = null,
?string $format = null,
bool $infer = true,
mixed $default = new MissingValue,
mixed $example = new MissingValue,
array $examples = [],
) {
parent::__construct('cookie', $name, $description, $required, $deprecated, $type, $infer, $default, $example, $examples);
parent::__construct('cookie', $name, $description, $required, $deprecated, $type, $format, $infer, $default, $example, $examples);
}
}
5 changes: 2 additions & 3 deletions src/Attributes/HeaderParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,18 @@
#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_ALL)]
class HeaderParameter extends Parameter
{
public readonly bool $required;

public function __construct(
string $name,
?string $description = null,
?bool $required = null,
$deprecated = false,
?string $type = null,
?string $format = null,
bool $infer = true,
mixed $default = new MissingValue,
mixed $example = new MissingValue,
array $examples = [],
) {
parent::__construct('header', $name, $description, $required, $deprecated, $type, $infer, $default, $example, $examples);
parent::__construct('header', $name, $description, $required, $deprecated, $type, $format, $infer, $default, $example, $examples);
}
}
3 changes: 2 additions & 1 deletion src/Attributes/Parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Parameter
public readonly bool $required;

/**
* @param 'query'|'path'|'header'|'cookie' $in
* @param 'query'|'path'|'header'|'cookie'|'body' $in
* @param scalar|array|object|MissingValue $example
* @param array<string, Example> $examples The key is a distinct name and the value is an example object.
*/
Expand All @@ -21,6 +21,7 @@ public function __construct(
?bool $required = null,
public bool $deprecated = false,
public ?string $type = null,
public ?string $format = null,
public bool $infer = true,
public mixed $default = new MissingValue,
public mixed $example = new MissingValue,
Expand Down
5 changes: 2 additions & 3 deletions src/Attributes/PathParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,18 @@
#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_ALL)]
class PathParameter extends Parameter
{
public readonly bool $required;

public function __construct(
string $name,
?string $description = null,
?bool $required = null,
$deprecated = false,
?string $type = null,
?string $format = null,
bool $infer = true,
mixed $default = new MissingValue,
mixed $example = new MissingValue,
array $examples = [],
) {
parent::__construct('path', $name, $description, $required, $deprecated, $type, $infer, $default, $example, $examples);
parent::__construct('path', $name, $description, $required, $deprecated, $type, $format, $infer, $default, $example, $examples);
}
}
5 changes: 2 additions & 3 deletions src/Attributes/QueryParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,18 @@
#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_ALL)]
class QueryParameter extends Parameter
{
public readonly bool $required;

public function __construct(
string $name,
?string $description = null,
?bool $required = null,
$deprecated = false,
?string $type = null,
?string $format = null,
bool $infer = true,
mixed $default = new MissingValue,
mixed $example = new MissingValue,
array $examples = [],
) {
parent::__construct('query', $name, $description, $required, $deprecated, $type, $infer, $default, $example, $examples);
parent::__construct('query', $name, $description, $required, $deprecated, $type, $format, $infer, $default, $example, $examples);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ private function createParameter(array $extractedParameters, ParameterAttribute
$parameter->schema->type = $attributeParameter->schema->type;
}

if ($name === 'format') {
$parameter->schema->type->format = $attributeParameter->schema->type->format;
}

if ($name === 'deprecated') {
$parameter->deprecated = $attributeParameter->deprecated;
}
Expand All @@ -98,6 +102,8 @@ private function createParameter(array $extractedParameters, ParameterAttribute
if ($name === 'examples') {
$parameter->examples = $attributeParameter->examples;
}

$parameter->setAttribute('nonBody', $attributeParameter->getAttribute('nonBody'));
}

return $parameter;
Expand All @@ -119,6 +125,8 @@ private function createParameterFromAttribute(ParameterAttribute $attribute): Pa
))
->required($attribute->required);

$parameter->setAttribute('nonBody', $attribute->in !== 'body');

$parameter->deprecated = $attribute->deprecated;

if (! $attribute->example instanceof MissingValue) {
Expand All @@ -132,6 +140,10 @@ private function createParameterFromAttribute(ParameterAttribute $attribute): Pa
);
}

if ($attribute->format) {
$type->format = $attribute->format;
}

return $parameter;
}

Expand Down
10 changes: 5 additions & 5 deletions src/Support/OperationExtensions/RequestBodyExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ public function handle(Operation $operation, RouteInfo $routeInfo)
return;
}

[$queryParams, $bodyParams] = collect($allParams)
->partition(fn (Parameter $p) => $p->getAttribute('isInQuery'))
[$nonBodyParams, $bodyParams] = collect($allParams)
->partition(fn (Parameter $p) => $p->getAttribute('isInQuery') || $p->getAttribute('nonBody'))
->map->toArray();

$operation->addParameters($this->convertDotNamedParamsToComplexStructures($queryParams));
$operation->addParameters($this->convertDotNamedParamsToComplexStructures($nonBodyParams));

if (! $bodyParams) {
return;
Expand All @@ -82,8 +82,8 @@ public function handle(Operation $operation, RouteInfo $routeInfo)

$schemas = $schemaResults->merge($schemalessResults)
->filter(fn (ParametersExtractionResult $r) => count($r->parameters) || $r->schemaName)
->map(function (ParametersExtractionResult $r) use ($queryParams) {
$qpNames = collect($queryParams)->keyBy('name');
->map(function (ParametersExtractionResult $r) use ($nonBodyParams) {
$qpNames = collect($nonBodyParams)->keyBy('name');

$r->parameters = collect($r->parameters)->filter(fn ($p) => ! $qpNames->has($p->name))->values()->all();

Expand Down

0 comments on commit 23f0fd0

Please sign in to comment.