Skip to content
This repository has been archived by the owner on Dec 11, 2023. It is now read-only.

Commit

Permalink
Merge parameter definitions. Fixes #16 (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
DerManoMann authored Feb 23, 2018
1 parent 6810019 commit a98e045
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 5 deletions.
3 changes: 3 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,6 @@ For a more complete example have a look at the included Symfony Console command.

### v3.0.3
* Allow to specify multiple source locations on command line [#14]

### v3.0.4
* Finally merge auto-generated and annotated parameter definitions [#16]
14 changes: 9 additions & 5 deletions src/Swagger/S2SConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,18 +248,22 @@ protected function migrateRequest(SLX\Request $request, Context $context, array
}

if (is_array($swgOperation->$key) && is_array($value) && 'parameters' == $key) {
// TODO: check for matching values; ie matching property names and then merge on value level
foreach ($value as $parameter) {
// does that parameter already exist?
$merged = false;
foreach ($swgOperation->$key as $cp) {
if ($cp->name == $parameter->name) {
$merged = true;
// todo: merge!
// does that parameter already exist?
if ($cp->name == $parameter->name && $cp->in == $parameter->in) {
foreach (array_keys(SWG\Parameter::$_types) as $pkey) {
if ($parameter->$pkey) {
$cp->$pkey = $parameter->$pkey;
$merged = true;
}
}
break;
}
}
if (!$merged) {
// not merged, so lets add it
$swgOperation->{$key}[] = $parameter;
}
}
Expand Down
33 changes: 33 additions & 0 deletions tests/Controller/ParameterMergeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Radebatz\Silex2Swagger\Tests\Controller;

use DDesrosiers\SilexAnnotations\Annotations as SLX;
use Swagger\Annotations as SWG;
use Radebatz\Silex2Swagger\Swagger\Annotations as S2S;

/**
* @SLX\Controller(prefix="/request")
*/
class CustomRequestController
{
/**
* @SLX\Route(
* @S2S\Request(method="GET", uri="get/{userId}",
* @S2S\SwaggerProperty(name="consumes", value={"application/x-www-form-urlencoded"})
* ),
*
* @SWG\Parameter(
* name="userId",
* in="path",
* description="User ID"
* ),
*
* @SWG\Response(response=200, description="GET")
* )
*/
public function testCustomRequest($userId)
{
return '';
}
}
30 changes: 30 additions & 0 deletions tests/Fixtures/auto.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,31 @@
}
}
},
"\/request\/get\/{userId}": {
"get": {
"summary": "GET:\/request\/get\/{userId}",
"description": "GET:\/request\/get\/{userId}",
"operationId": "testCustomRequest",
"consumes": [
"application\/x-www-form-urlencoded"
],
"parameters": [
{
"parameter": "userId",
"name": "userId",
"in": "path",
"description": "User ID",
"required": true,
"type": "string"
}
],
"responses": {
"200": {
"description": "GET"
}
}
}
},
"\/shared_all\/get": {
"get": {
"summary": "GET:\/shared_all\/get",
Expand Down Expand Up @@ -185,6 +210,11 @@
}
},
"parameters": {
"userId": {
"name": "userId",
"in": "path",
"description": "User ID"
},
"x-api-version": {
"name": "x-api-version",
"in": "header",
Expand Down
28 changes: 28 additions & 0 deletions tests/Fixtures/defaults.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,29 @@
}
}
},
"\/request\/get\/{userId}": {
"get": {
"operationId": "testCustomRequest",
"consumes": [
"application\/x-www-form-urlencoded"
],
"parameters": [
{
"parameter": "userId",
"name": "userId",
"in": "path",
"description": "User ID",
"required": true,
"type": "string"
}
],
"responses": {
"200": {
"description": "GET"
}
}
}
},
"\/shared_all\/get": {
"get": {
"operationId": "testGetRequest",
Expand Down Expand Up @@ -157,6 +180,11 @@
}
},
"parameters": {
"userId": {
"name": "userId",
"in": "path",
"description": "User ID"
},
"x-api-version": {
"name": "x-api-version",
"in": "header",
Expand Down
28 changes: 28 additions & 0 deletions tests/Fixtures/multi-path.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,29 @@
}
}
},
"\/request\/get\/{userId}": {
"get": {
"operationId": "testCustomRequest",
"consumes": [
"application\/x-www-form-urlencoded"
],
"parameters": [
{
"parameter": "userId",
"name": "userId",
"in": "path",
"description": "User ID",
"required": true,
"type": "string"
}
],
"responses": {
"200": {
"description": "GET"
}
}
}
},
"\/shared_all\/get": {
"get": {
"operationId": "testGetRequest",
Expand Down Expand Up @@ -192,6 +215,11 @@
}
},
"parameters": {
"userId": {
"name": "userId",
"in": "path",
"description": "User ID"
},
"x-api-version": {
"name": "x-api-version",
"in": "header",
Expand Down

0 comments on commit a98e045

Please sign in to comment.