Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
uderline committed Mar 8, 2022
2 parents adea982 + e7da94d commit e6cc51d
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 13 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,24 @@ class Controller {
Property(PropertyType::STRING, "prop1", description: "Property description", enum: ["val1", "val2"]),
Property(PropertyType::INT, "prop2", example: 1),
Property(PropertyType::BOOLEAN, "prop3"),
Property(PropertyType::REF, "prop4", ref: RefSchema::class)
Response(ref: SchemaName::class, description: "Response description")
]
public function get(#[Parameter("Parameter description")] int $id): JsonResponse {
// ...
}
}

#[
Schema,
Property(Type::STRING, "Property 1"),
Property(Type::INT, "Property 2"),
]
class RefSchema
{
public string $property1;
public int $property2;
}
```

Will generate
Expand Down Expand Up @@ -75,6 +87,9 @@ Will generate
"prop3": {
"type": "boolean",
"description": ""
},
"prop4": {
"$ref": "#/components/schemas/RefSchema"
}
}
}
Expand Down
11 changes: 10 additions & 1 deletion src/Attributes/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,13 @@ public function __construct(
private string $description = '',
private mixed $example = null,
private ?string $format = null,
private ?array $enum = null
private ?array $enum = null,
private ?string $ref = null
) {
if ($this->ref) {
$ref = explode('\\', $this->ref);
$this->ref = end($ref);
}
}

public function setPropertyItems(PropertyItems $propertyItems): void
Expand Down Expand Up @@ -55,6 +60,10 @@ public function jsonSerialize(): array
}
}

if ($this->type === PropertyType::REF) {
return ['$ref' => "#/components/schemas/$this->ref"];
}

if ($this->type === PropertyType::ID) {
$type = 'integer';
$minimum = 1;
Expand Down
2 changes: 1 addition & 1 deletion src/Attributes/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function jsonSerialize(): array
$firstProperty = reset($this->properties);

if ($firstProperty instanceof RefProperty || $firstProperty instanceof MediaProperty) {
$schema = $firstProperty;
$schema = $firstProperty->jsonSerialize();
} else {
$array = [];

Expand Down
13 changes: 8 additions & 5 deletions src/ComponentBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@

namespace OpenApiGenerator;

use OpenApiGenerator\Attributes\Property;
use OpenApiGenerator\Attributes\PropertyInterface;
use OpenApiGenerator\Attributes\PropertyItems;
use OpenApiGenerator\Attributes\Schema;

class ComponentBuilder
{
private ?Schema $currentSchema = null;
private ?Property $currentProperty = null;
private ?PropertyInterface $currentProperty = null;

public function addSchema(Schema $schema, string $className): bool
public function __construct(private $noMedia = true)
{
$schema->setNoMedia(true);
}

public function addSchema(Schema $schema, string $className): bool
{
if (!$schema->getName()) {
$explodedNamespace = explode('\\', $className);
$className = end($explodedNamespace);
Expand All @@ -28,7 +30,7 @@ public function addSchema(Schema $schema, string $className): bool
return true;
}

public function addProperty(Property $property): bool
public function addProperty(PropertyInterface $property): bool
{
$this->saveProperty();
$this->currentProperty = $property;
Expand Down Expand Up @@ -57,6 +59,7 @@ public function addPropertyItems(PropertyItems $items): bool
public function getComponent(): ?Schema
{
$this->saveProperty();
$this->currentSchema->setNoMedia($this->noMedia);

return $this->currentSchema;
}
Expand Down
41 changes: 40 additions & 1 deletion src/GeneratorHttp.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@
use OpenApiGenerator\Attributes\Property;
use OpenApiGenerator\Attributes\PropertyItems;
use OpenApiGenerator\Attributes\PUT;
use OpenApiGenerator\Attributes\RefProperty;
use OpenApiGenerator\Attributes\RequestBody;
use OpenApiGenerator\Attributes\Response;
use OpenApiGenerator\Attributes\Route;
use OpenApiGenerator\Attributes\Schema;
use OpenApiGenerator\Tests\Examples\Dummy\DummyRequest;
use ReflectionAttribute;
use ReflectionClass;
use ReflectionMethod;
use ReflectionParameter;
use Symfony\Component\HttpFoundation\Request;

Expand Down Expand Up @@ -50,8 +54,10 @@ public function append(ReflectionClass $reflectionClass)
}
}

$requestBody = $this->getRequestBody($method);

$pathBuilder = new PathMethodBuilder();
$pathBuilder->setRequestBody(new RequestBody());
$pathBuilder->setRequestBody($requestBody);

// Add method Attributes to the builder
foreach ($methodAttributes as $attribute) {
Expand Down Expand Up @@ -82,6 +88,39 @@ public function append(ReflectionClass $reflectionClass)
}
}

private function getRequestBody(ReflectionMethod $method): RequestBody
{
$requestBody = new RequestBody();

$requestClass = array_filter(
$method->getParameters(),
static fn(ReflectionParameter $parameter): bool => is_subclass_of(
$parameter->getType()->getName(),
Request::class
)
);

if (count($requestClass) > 0) {
$requestReflection = new ReflectionClass(DummyRequest::class);
$schemaAttributes = $requestReflection->getAttributes(Schema::class);
/** @var ReflectionAttribute|false $schema */
$schema = reset($schemaAttributes);

if ($schema) {
/** @var Schema $requestSchema */
$requestSchema = $schema->newInstance();

$builder = new ComponentBuilder(false);
$builder->addSchema($requestSchema, DummyRequest::class);
$builder->addProperty(new RefProperty($requestSchema->getName()));

$requestBody->setSchema($builder->getComponent());
}
}

return $requestBody;
}

/**
* @param ReflectionParameter[] $methodParameters
* @return Parameter[]
Expand Down
9 changes: 4 additions & 5 deletions tests/Examples/Dummy/DummyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ class DummyController
GET("/path", ["Dummy"], "Dummy get path"),
Property(Type::STRING, "prop1", "Prop 1", "val1"),
Property(Type::STRING, "prop2", "Prop 2", "val2"),
Response
Response,
Property(Type::STRING, "Response prop 1", "Prop response 1", "1"),
Property(Type::INT, "Response prop 2", "Prop response 2", 4)
]
public function get(): void
{
Expand Down Expand Up @@ -76,12 +78,9 @@ public function post(): void

#[
PUT("/path/{id}", ["Dummy"], "Dummy put"),
Property(Type::STRING, "prop1"),
Property(Type::ID, "prop2"),
Property(Type::BOOLEAN, "prop3"),
Response(204)
]
public function put(#[IDParam] int $id): void
public function put(#[IDParam] int $id, DummyRequest $dummyRequest): void
{
//
}
Expand Down
22 changes: 22 additions & 0 deletions tests/Examples/Dummy/DummyRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace OpenApiGenerator\Tests\Examples\Dummy;

use OpenApiGenerator\Attributes\Property;
use OpenApiGenerator\Attributes\PropertyItems;
use OpenApiGenerator\Attributes\Schema;
use OpenApiGenerator\Type;
use Symfony\Component\HttpFoundation\Request;

#[
Schema,
Property(Type::STRING, "Property 1"),
Property(Type::INT, "Property 2"),
Property(Type::ARRAY, "Property 3"),
PropertyItems(Type::STRING),
Property(Type::REF, "Property 4", ref: DummyRefComponent::class),
]
class DummyRequest extends Request
{

}

0 comments on commit e6cc51d

Please sign in to comment.