Skip to content

Commit

Permalink
Add SwagEntityAttribute::example #208
Browse files Browse the repository at this point in the history
Changes SchemaProperty::example to `@var mixed`
  • Loading branch information
cnizzardini committed Oct 18, 2020
1 parent bade171 commit 7a0b786
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 7 deletions.
5 changes: 5 additions & 0 deletions src/Lib/Annotation/AbstractSchemaProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ abstract class AbstractSchemaProperty
*/
public $enum = [];

/**
* @var mixed
*/
public $example;

/**
* @param array $values Annotation attributes as key-value pair
*/
Expand Down
1 change: 1 addition & 0 deletions src/Lib/Annotation/SwagEntityAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
* @Attribute("maxProperties", type = "integer"),
* @Attribute("minProperties", type = "integer"),
* @Attribute("enum", type = "array"),
* @Attribute("example", type = "mixed"),
* })
*
* Read OpenAPI specification for exact usage of the attributes:
Expand Down
14 changes: 8 additions & 6 deletions src/Lib/OpenApi/SchemaProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ class SchemaProperty implements JsonSerializable
private $format = '';

/**
* @var string
* @var mixed
*/
private $example = '';
private $example;

/**
* @var string
Expand Down Expand Up @@ -232,20 +232,20 @@ public function setRequired(bool $required)
}

/**
* @return string
* @return mixed
*/
public function getExample(): string
public function getExample()
{
return $this->example;
}

/**
* @param string|int $example Example
* @param mixed $example An example value
* @return $this
*/
public function setExample($example)
{
$this->example = (string)$example;
$this->example = $example;

return $this;
}
Expand Down Expand Up @@ -371,4 +371,6 @@ public function setItems(array $items)

return $this;
}


}
1 change: 1 addition & 0 deletions src/Lib/Schema/SchemaPropertyFromAnnotationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public function create(AbstractSchemaProperty $attribute): SchemaProperty
'uniqueItems',
'maxProperties',
'minProperties',
'example',
];

foreach ($properties as $property) {
Expand Down
1 change: 1 addition & 0 deletions tests/TestCase/Lib/Annotations/SwagEntityAttributeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public function testEntityAttribute()
$employee = $arr['components']['schemas']['Employee'];

$this->assertNotEmpty($employee['properties']['gender']['enum']);
$this->assertEquals('female', $employee['properties']['gender']['example']);
$this->assertEquals(3, $employee['properties']['last_name']['minLength']);
$this->assertEquals(59, $employee['properties']['last_name']['maxLength']);
$this->assertEquals('/\W/', $employee['properties']['last_name']['pattern']);
Expand Down
2 changes: 1 addition & 1 deletion tests/test_app/src/Model/Entity/Employee.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/**
* Employee Entity
* @SwagAnnotation\SwagEntityAttribute(
* name="gender", type="string", required=false, enum={"male","female","other"},
* name="gender", type="string", required=false, enum={"male","female","other"}, example="female"
* )
* @SwagAnnotation\SwagEntityAttribute(
* name="last_name", type="string", minLength=3, maxLength=59, pattern="/\W/"
Expand Down

0 comments on commit 7a0b786

Please sign in to comment.