diff --git a/README.md b/README.md index 5a5f94d..80c6dcb 100644 --- a/README.md +++ b/README.md @@ -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 @@ -75,6 +87,9 @@ Will generate "prop3": { "type": "boolean", "description": "" + }, + "prop4": { + "$ref": "#/components/schemas/RefSchema" } } } diff --git a/src/Attributes/Property.php b/src/Attributes/Property.php index 39080db..583b96a 100644 --- a/src/Attributes/Property.php +++ b/src/Attributes/Property.php @@ -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 @@ -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; diff --git a/tests/Examples/Dummy/DummyRequest.php b/tests/Examples/Dummy/DummyRequest.php index 82ad1b7..8035737 100644 --- a/tests/Examples/Dummy/DummyRequest.php +++ b/tests/Examples/Dummy/DummyRequest.php @@ -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 {