Skip to content

Commit

Permalink
Added the ability to specify a ref to a schema in the Property attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
zaneevat committed Mar 7, 2022
1 parent 1434c87 commit 03cdbe5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 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
3 changes: 2 additions & 1 deletion tests/Examples/Dummy/DummyRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
Property(Type::STRING, "Property 1"),
Property(Type::INT, "Property 2"),
Property(Type::ARRAY, "Property 3"),
PropertyItems(Type::STRING)
PropertyItems(Type::STRING),
Property(Type::REF, "Property 4", ref: DummyRefComponent::class),
]
class DummyRequest extends Request
{
Expand Down

0 comments on commit 03cdbe5

Please sign in to comment.