Skip to content

Commit 60bc089

Browse files
committed
feat: add sf requests to components requestBodies
1 parent 150fac7 commit 60bc089

11 files changed

Lines changed: 69 additions & 56 deletions

src/Attributes/RequestBody.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,6 @@ public function jsonSerialize(): array
4848
}
4949

5050
// TODO: deal with media content or any other Types (cf. $this->type)
51-
return ['content' => $this->schema];
51+
return $this->schema->jsonSerialize();
5252
}
5353
}

src/Attributes/Response.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use Attribute;
88
use Countable;
99
use JsonSerializable;
10-
use OpenApiGenerator\Type;
10+
use OpenApiGenerator\RefProperty;
1111
use OpenApiGenerator\Types\ItemsType;
1212
use OpenApiGenerator\Types\SchemaType;
1313

@@ -57,7 +57,7 @@ public function jsonSerialize(): array
5757
];
5858

5959
if ($this->schema) {
60-
$array[$this->code]['content'] = $this->schema;
60+
$array[$this->code] = array_merge($array[$this->code], $this->schema->jsonSerialize());
6161
}
6262

6363
if ($this->extra) {

src/Attributes/Schema.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Attribute;
88
use Countable;
99
use JsonSerializable;
10+
use OpenApiGenerator\RefProperty;
1011
use OpenApiGenerator\Types\SchemaType;
1112

1213
/**
@@ -84,8 +85,10 @@ public function jsonSerialize(): array
8485
}
8586

8687
return [
87-
$this->getMediaType() => [
88-
'schema' => $schema
88+
"content" => [
89+
$this->getMediaType() => [
90+
'schema' => $schema
91+
]
8992
]
9093
];
9194
}

src/ComponentFactory.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,16 @@ public static function build(ReflectionClass $reflectionClass): Schema
4646

4747
return $builder->getComponent();
4848
}
49+
50+
public static function buildRequest(string $refComponentName): Schema
51+
{
52+
$builder = new SchemaBuilder(false);
53+
54+
$schema = new Schema();
55+
$schema->addProperty(new RefProperty($refComponentName));
56+
57+
$builder->addSchema($schema, $refComponentName);
58+
59+
return $builder->getComponent();
60+
}
4961
}

src/Generator.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use ReflectionAttribute;
1414
use ReflectionClass;
1515
use ReflectionException;
16+
use Symfony\Component\HttpFoundation\Request;
1617

1718
class Generator
1819
{
@@ -105,6 +106,11 @@ private function loadSchema(ReflectionClass $reflectionClass): void
105106
if (count($reflectionClass->getAttributes(Schema::class))) {
106107
$component = ComponentFactory::build($reflectionClass);
107108
$this->description['components']['schemas'][$component->getName()] = $component;
109+
110+
if ($reflectionClass->isSubclassOf(Request::class)) {
111+
$component = ComponentFactory::buildRequest($component->getName());
112+
$this->description['components']['requestBodies'][$component->getName()] = $component;
113+
}
108114
}
109115
}
110116

src/GeneratorHttp.php

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use OpenApiGenerator\Attributes\PropertyInterface;
1717
use OpenApiGenerator\Attributes\PropertyItems;
1818
use OpenApiGenerator\Attributes\PUT;
19-
use OpenApiGenerator\Attributes\RefProperty;
2019
use OpenApiGenerator\Attributes\RequestBody;
2120
use OpenApiGenerator\Attributes\Response;
2221
use OpenApiGenerator\Attributes\Route;
@@ -113,8 +112,7 @@ public function append(ReflectionClass $reflectionClass): void
113112

114113
/**
115114
* OPAG supports Symfony Request class. This method will build the RequestBody from the Symfony Request class
116-
*
117-
* @throws ReflectionException|IllegalFieldException
115+
* The request object will be added as a reference to the RequestBody
118116
*/
119117
private function buildRequestBodyFromSymfonyRequest(ReflectionMethod $method): ?RequestBody
120118
{
@@ -133,22 +131,12 @@ private function buildRequestBodyFromSymfonyRequest(ReflectionMethod $method): ?
133131
return null;
134132
}
135133

136-
// Get the Schema attribute from the Symfony Request class
137-
$requestReflection = new ReflectionClass($requestClass->getType()->getName());
138-
$schemaAttributes = $requestReflection->getAttributes(Schema::class);
139-
$schemaAttribute = reset($schemaAttributes);
140-
if (!$schemaAttribute) {
141-
return null;
142-
}
143-
144-
// Build the schema
145-
$schema = $schemaAttribute->newInstance();
146-
$builder = new SchemaBuilder(false);
147-
$builder->addSchema($schema, $requestClass->getType()->getName());
148-
$builder->addProperty(new RefProperty($schema->getName()));
149-
150-
// Set the schema to the RequestBody and return it
151-
$requestBody->setSchema($builder->getComponent());
134+
$schema = new Schema();
135+
$schema->setNoMedia(true);
136+
$requestBody->setSchema($schema);
137+
$property = new RefProperty($requestClass->getType()->getName());
138+
$property->setComponentRoutePrefix("#/components/requestBodies/");
139+
$requestBody->addProperty($property);
152140

153141
return $requestBody;
154142
}
Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,32 @@
22

33
declare(strict_types=1);
44

5-
namespace OpenApiGenerator\Attributes;
5+
namespace OpenApiGenerator;
66

77
use Attribute;
88
use JsonSerializable;
9+
use OpenApiGenerator\Attributes\PropertyInterface;
910
use OpenApiGenerator\Types\PropertyType;
1011

11-
/**
12-
* This represents an open api property.
13-
*/
14-
#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_ALL)]
1512
class RefProperty implements PropertyInterface, JsonSerializable
1613
{
14+
private string $route = "#/components/schemas/";
15+
1716
public function __construct(
1817
private string $ref,
1918
) {
2019
$ref = explode('\\', $this->ref);
2120
$this->ref = end($ref);
2221
}
2322

23+
public function setComponentRoutePrefix(string $route): void
24+
{
25+
$this->route = $route;
26+
}
27+
2428
public function jsonSerialize(): array
2529
{
26-
return ['$ref' => "#/components/schemas/$this->ref"];
30+
return ['$ref' => "{$this->route}{$this->ref}"];
2731
}
2832

2933
public function getType(): string

tests/Unit/ParameterTest.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,11 @@
44

55
namespace Unit;
66

7-
use OpenApiGenerator\Attributes\MediaProperty;
87
use OpenApiGenerator\Attributes\Parameter;
9-
use OpenApiGenerator\Attributes\Property;
10-
use OpenApiGenerator\Attributes\PropertyInterface;
11-
use OpenApiGenerator\Attributes\PropertyItems;
12-
use OpenApiGenerator\Attributes\RefProperty;
13-
use OpenApiGenerator\Attributes\Schema;
148
use OpenApiGenerator\IllegalFieldException;
15-
use OpenApiGenerator\Type;
16-
use OpenApiGenerator\Types\MediaType;
17-
use OpenApiGenerator\Types\SchemaType;
189
use PHPUnit\Framework\Attributes\DataProvider;
1910
use PHPUnit\Framework\Attributes\Test;
2011
use PHPUnit\Framework\TestCase;
21-
use stdClass;
2212

2313
class ParameterTest extends TestCase
2414
{

tests/Unit/PropertyTest.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
namespace Unit;
66

7-
use OpenApiGenerator\Attributes\Info;
87
use OpenApiGenerator\Attributes\Property;
98
use OpenApiGenerator\Attributes\PropertyItems;
109
use OpenApiGenerator\IllegalFieldException;
10+
use OpenApiGenerator\RefProperty;
1111
use OpenApiGenerator\Tests\Examples\Dummy\DummyBackedEnum;
1212
use OpenApiGenerator\Tests\Examples\Dummy\DummyRefComponent;
1313
use PHPUnit\Framework\Attributes\Test;
@@ -62,7 +62,18 @@ public function json_must_return_the_property_with_a_ref(): void
6262
'$ref' => '#/components/schemas/DummyRefComponent'
6363
], $property->jsonSerialize());
6464
}
65-
65+
66+
#[Test]
67+
public function json_contains_a_ref_property_with_a_different_prefix(): void
68+
{
69+
$property = new RefProperty(StdClass::class);
70+
$property->setComponentRoutePrefix("#/components/requestBodies/");
71+
72+
$this->assertEqualsCanonicalizing([
73+
'$ref' => '#/components/requestBodies/StdClass'
74+
], $property->jsonSerialize());
75+
}
76+
6677
#[Test]
6778
public function json_must_return_the_property_with_a_nullable_ref(): void
6879
{

tests/Unit/SchemaBuilderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function schema_can_have_media_in_schema(): void
3434
$schemaBuilder->addProperty(new Property("string", "prop1"));
3535
$component = $schemaBuilder->getComponent();
3636

37-
$this->assertArrayHasKey("application/json", $component->jsonSerialize());
37+
$this->assertArrayHasKey("content", $component->jsonSerialize());
3838
}
3939

4040
#[Test]

0 commit comments

Comments
 (0)