10000 Add attributes for request and response by yuters · Pull Request #14 · dingo/blueprint · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add attributes for request and response #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 30, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Annotation/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,9 @@ class Request
* @var array
*/
public $headers = [];

/**
* @var array<Dingo\Blueprint\Annotation\Attribute>
*/
public $attributes;
}
5 changes: 5 additions & 0 deletions src/Annotation/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,9 @@ class Response
* @var array
*/
public $headers = [];

/**
* @var array<Dingo\Blueprint\Annotation\Attribute>
*/
public $attributes;
}
17 changes: 13 additions & 4 deletions src/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,16 +170,17 @@ protected function generateContentsFromResources(Collection $resources, $name)
*
* @param string $contents
* @param \Illuminate\Support\Collection $attributes
* @param int $indent
*
* @return void
*/
protected function appendAttributes(&$contents, Collection $attributes)
protected function appendAttributes(&$contents, Collection $attributes, $indent = 0)
{
$this->appendSection($contents, 'Attributes');
$this->appendSection($contents, 'Attributes', $indent);

$attributes->each(function ($attribute) use (&$contents) {
$attributes->each(function ($attribute) use (&$contents, $indent) {
$contents .= $this->line();
$contents .= $this->tab();
$contents .= $this->tab(1 + $indent);
$contents .= sprintf('+ %s', $attribute->identifier);

if ($attribute->sample) {
Expand Down Expand Up @@ -251,6 +252,10 @@ protected function appendResponse(&$contents, Annotation\Response $response)
$this->appendHeaders($contents, $request->headers);
}

if (isset($response->attributes)) {
$this->appendAttributes($contents, collect($response->attributes), 1);
}

if (isset($response->body)) {
$this->appendBody($contents, $this->prepareBody($response->body, $response->contentType));
}
Expand Down Expand Up @@ -278,6 +283,10 @@ protected function appendRequest(&$contents, $request)
$this->appendHeaders($contents, $request->headers);
}

if (isset($request->attributes)) {
$this->appendAttributes($contents, collect($request->attributes), 1);
}

if (isset($request->body)) {
$this->appendBody($contents, $this->prepareBody($request->body, $request->contentType));
}
Expand Down
20 changes: 20 additions & 0 deletions tests/BlueprintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ public function testGeneratingBlueprintForSingleResource()
Create a new user.

+ Request (application/json)

+ Attributes
+ name: jason (string, required) - The user name
+ email: jason@jason.com (string, required) - The user email
+ password: 1234567 (string, required) - The user password
+ Body

{
Expand Down Expand Up @@ -164,6 +169,11 @@ public function testGeneratingBlueprintForMultipleResourcesWithVersionOne()
Create a new user.

+ Request (application/json)

+ Attributes
+ name: jason (string, required) - The user name
+ email: jason@jason.com (string, required) - The user email
+ password: 1234567 (string, required) - The user password
+ Body

{
Expand Down Expand Up @@ -318,6 +328,11 @@ public function testGeneratingBlueprintForMultipleResourcesWithVersionTwo()
Create a new user.

+ Request (application/json)

+ Attributes
+ name: jason (string, required) - The user name
+ email: jason@jason.com (string, required) - The user email
+ password: 1234567 (string, required) - The user password
+ Body

{
Expand Down Expand Up @@ -383,6 +398,11 @@ public function testGeneratingBlueprintForMultipleResourcesWithVersionTwo()
+ photoId (integer, required) - ID of photo to show.

+ Response 200 (application/json)

+ Attributes
+ id: 1 (number, optional) - The photo id
+ name: photo (string, optional) - The photo name
+ src: path/to/cool/photo.jpg (string, optional) - The photo path
+ Body

{
Expand Down
4 changes: 4 additions & 0 deletions tests/Stubs/UserPhotosResourceStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ public function index($userId)
* "id": 1,
* "name": "photo",
* "src": "path/to/cool/photo.jpg"
* }, attributes={
* @Attribute("id", type="number", description="The photo id", sample="1"),
* @Attribute("name", type="string", description="The photo name", sample="photo"),
* @Attribute("src", type="string", description="The photo path", sample="path/to/cool/photo.jpg")
* }),
* @Response(404, body={"message": "Photo could not be found."})
* })
Expand Down
6 changes: 5 additions & 1 deletion tests/Stubs/UsersResourceStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,14 @@ public function show($id)
*
* @Post("/")
* @Transaction({
* @Request({
* @Request(body={
* "name": "jason",
* "email": "jason@jason.com",
* "password": "1234567"
* }, attributes={
* @Attribute("name", type="string", description="The user name", sample="jason", required=true),
* @Attribute("email", type="string", description="The user email", sample="jason@jason.com", required=true),
* @Attribute("password", type="string", description="The user password", sample="1234567", required=true)
* }),
* @Response(200, body={"id": 10, "name": "jason", "email": "jason@jason.com"}),
* @Response(422, body={
Expand Down
0