Skip to content

Commit 4a3d935

Browse files
authored
Merge pull request #3165 from abcsun/lumen
[PHP Lumen] sort the endpoints in ascending to avoid the route priority issure
2 parents 75bd838 + cff573f commit 4a3d935

File tree

5 files changed

+142
-131
lines changed

5 files changed

+142
-131
lines changed

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/LumenServerCodegen.java

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,16 @@
55

66
import java.util.*;
77
import java.io.File;
8+
import java.util.Comparator;
9+
import java.util.HashMap;
10+
import java.util.Map;
11+
import java.util.TreeMap;
812

913
public class LumenServerCodegen extends DefaultCodegen implements CodegenConfig {
1014

1115
// source folder where to write the files
1216
protected String sourceFolder = "";
13-
14-
public static final String SRC_BASE_PATH = "srcBasePath";
15-
public static final String COMPOSER_VENDOR_NAME = "composerVendorName";
16-
public static final String COMPOSER_PROJECT_NAME = "composerProjectName";
17-
protected String invokerPackage = "Swagger\\Client";
18-
protected String composerVendorName = null;
19-
protected String composerProjectName = null;
20-
protected String packagePath = "SwaggerClient-php";
21-
protected String artifactVersion = null;
22-
protected String srcBasePath = "lib";
2317
protected String apiVersion = "1.0.0";
24-
protected String apiDirName = "Api";
2518

2619
/**
2720
* Configures the type of generator.
@@ -95,12 +88,12 @@ public LumenServerCodegen() {
9588
/**
9689
* Api Package. Optional, if needed, this can be used in templates
9790
*/
98-
apiPackage = "io.swagger.client.api";
91+
apiPackage = "app.Http.Controllers";
9992

10093
/**
10194
* Model Package. Optional, if needed, this can be used in templates
10295
*/
103-
modelPackage = "io.swagger.client.model";
96+
modelPackage = "models";
10497

10598
/**
10699
* Reserved words. Override this with reserved words specific to your language
@@ -161,7 +154,7 @@ public String escapeReservedWord(String name) {
161154
* instantiated
162155
*/
163156
public String modelFileFolder() {
164-
return outputFolder + "/" + sourceFolder + "/" + modelPackage().replace('.', File.separatorChar);
157+
return outputFolder + "/" + modelPackage().replace('.', File.separatorChar);
165158
}
166159

167160
/**
@@ -170,10 +163,28 @@ public String modelFileFolder() {
170163
*/
171164
@Override
172165
public String apiFileFolder() {
173-
return outputFolder + "/app/Http/controllers";
174-
// return outputFolder + "/" + sourceFolder + "/" + apiPackage().replace('.', File.separatorChar);
166+
return outputFolder + "/" + apiPackage().replace('.', File.separatorChar);//"/app/Http/controllers";
175167
}
176168

169+
// override with any special post-processing
170+
@Override
171+
public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
172+
@SuppressWarnings("unchecked")
173+
Map<String, Object> objectMap = (Map<String, Object>) objs.get("operations");
174+
@SuppressWarnings("unchecked")
175+
List<CodegenOperation> operations = (List<CodegenOperation>) objectMap.get("operation");
176+
177+
// sort the endpoints in ascending to avoid the route priority issure.
178+
// https://github.com/swagger-api/swagger-codegen/issues/2643
179+
Collections.sort(operations, new Comparator<CodegenOperation>() {
180+
@Override
181+
public int compare(CodegenOperation lhs, CodegenOperation rhs) {
182+
return lhs.path.compareTo(rhs.path);
183+
}
184+
});
185+
186+
return objs;
187+
}
177188
/**
178189
* Optional - type declaration. This is a String which is used by the templates to instantiate your
179190
* types. There is typically special handling for different property types

samples/server/petstore/lumen/app/Http/controllers/PetApi.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -63,24 +63,28 @@ public function addPet()
6363
return response('How about implementing addPet as a POST method ?');
6464
}
6565
/**
66-
* Operation deletePet
66+
* Operation updatePet
6767
*
68-
* Deletes a pet.
68+
* Update an existing pet.
6969
*
70-
* @param Long $petId Pet id to delete (required)
7170
*
7271
* @return Http response
7372
*/
74-
public function deletePet($petId)
73+
public function updatePet()
7574
{
7675
$input = Request::all();
7776

7877
//path params validation
7978

8079

8180
//not path params validation
81+
if (!isset($input['body'])) {
82+
throw new \InvalidArgumentException('Missing the required parameter $body when calling updatePet');
83+
}
84+
$body = $input['body'];
8285

83-
return response('How about implementing deletePet as a DELETE method ?');
86+
87+
return response('How about implementing updatePet as a PUT method ?');
8488
}
8589
/**
8690
* Operation findPetsByStatus
@@ -131,15 +135,15 @@ public function findPetsByTags()
131135
return response('How about implementing findPetsByTags as a GET method ?');
132136
}
133137
/**
134-
* Operation getPetById
138+
* Operation deletePet
135139
*
136-
* Find pet by ID.
140+
* Deletes a pet.
137141
*
138-
* @param Long $petId ID of pet to return (required)
142+
* @param Long $petId Pet id to delete (required)
139143
*
140144
* @return Http response
141145
*/
142-
public function getPetById($petId)
146+
public function deletePet($petId)
143147
{
144148
$input = Request::all();
145149

@@ -148,31 +152,27 @@ public function getPetById($petId)
148152

149153
//not path params validation
150154

151-
return response('How about implementing getPetById as a GET method ?');
155+
return response('How about implementing deletePet as a DELETE method ?');
152156
}
153157
/**
154-
* Operation updatePet
158+
* Operation getPetById
155159
*
156-
* Update an existing pet.
160+
* Find pet by ID.
157161
*
162+
* @param Long $petId ID of pet to return (required)
158163
*
159164
* @return Http response
160165
*/
161-
public function updatePet()
166+
public function getPetById($petId)
162167
{
163168
$input = Request::all();
164169

165170
//path params validation
166171

167172

168173
//not path params validation
169-
if (!isset($input['body'])) {
170-
throw new \InvalidArgumentException('Missing the required parameter $body when calling updatePet');
171-
}
172-
$body = $input['body'];
173-
174174

175-
return response('How about implementing updatePet as a PUT method ?');
175+
return response('How about implementing getPetById as a GET method ?');
176176
}
177177
/**
178178
* Operation updatePetWithForm

samples/server/petstore/lumen/app/Http/controllers/StoreApi.php

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -39,95 +39,95 @@ public function __construct()
3939
}
4040

4141
/**
42-
* Operation deleteOrder
42+
* Operation getInventory
4343
*
44-
* Delete purchase order by ID.
44+
* Returns pet inventories by status.
4545
*
46-
* @param String $orderId ID of the order that needs to be deleted (required)
4746
*
4847
* @return Http response
4948
*/
50-
public function deleteOrder($orderId)
49+
public function getInventory()
5150
{
5251
$input = Request::all();
5352

5453
//path params validation
55-
if ($orderId] < 1.0) {
56-
throw new \InvalidArgumentException('invalid value for $orderId when calling StoreApi.deleteOrder, must be bigger than or equal to 1.0.');
57-
}
5854

5955

6056
//not path params validation
6157

62-
return response('How about implementing deleteOrder as a DELETE method ?');
58+
return response('How about implementing getInventory as a GET method ?');
6359
}
6460
/**
65-
* Operation getInventory
61+
* Operation placeOrder
6662
*
67-
* Returns pet inventories by status.
63+
* Place an order for a pet.
6864
*
6965
*
7066
* @return Http response
7167
*/
72-
public function getInventory()
68+
public function placeOrder()
7369
{
7470
$input = Request::all();
7571

7672
//path params validation
7773

7874

7975
//not path params validation
76+
if (!isset($input['body'])) {
77+
throw new \InvalidArgumentException('Missing the required parameter $body when calling placeOrder');
78+
}
79+
$body = $input['body'];
8080

81-
return response('How about implementing getInventory as a GET method ?');
81+
82+
return response('How about implementing placeOrder as a POST method ?');
8283
}
8384
/**
84-
* Operation getOrderById
85+
* Operation deleteOrder
8586
*
86-
* Find purchase order by ID.
87+
* Delete purchase order by ID.
8788
*
88-
* @param Long $orderId ID of pet that needs to be fetched (required)
89+
* @param String $orderId ID of the order that needs to be deleted (required)
8990
*
9091
* @return Http response
9192
*/
92-
public function getOrderById($orderId)
93+
public function deleteOrder($orderId)
9394
{
9495
$input = Request::all();
9596

9697
//path params validation
97-
if ($orderId] > 5.0) {
98-
throw new \InvalidArgumentException('invalid value for $orderId when calling StoreApi.getOrderById, must be smaller than or equal to 5.0.');
99-
}
10098
if ($orderId] < 1.0) {
101-
throw new \InvalidArgumentException('invalid value for $orderId when calling StoreApi.getOrderById, must be bigger than or equal to 1.0.');
99+
throw new \InvalidArgumentException('invalid value for $orderId when calling StoreApi.deleteOrder, must be bigger than or equal to 1.0.');
102100
}
103101

104102

105103
//not path params validation
106104

107-
return response('How about implementing getOrderById as a GET method ?');
105+
return response('How about implementing deleteOrder as a DELETE method ?');
108106
}
109107
/**
110-
* Operation placeOrder
108+
* Operation getOrderById
111109
*
112-
* Place an order for a pet.
110+
* Find purchase order by ID.
113111
*
112+
* @param Long $orderId ID of pet that needs to be fetched (required)
114113
*
115114
* @return Http response
116115
*/
117-
public function placeOrder()
116+
public function getOrderById($orderId)
118117
{
119118
$input = Request::all();
120119

121120
//path params validation
121+
if ($orderId] > 5.0) {
122+
throw new \InvalidArgumentException('invalid value for $orderId when calling StoreApi.getOrderById, must be smaller than or equal to 5.0.');
123+
}
124+
if ($orderId] < 1.0) {
125+
throw new \InvalidArgumentException('invalid value for $orderId when calling StoreApi.getOrderById, must be bigger than or equal to 1.0.');
126+
}
122127

123128

124129
//not path params validation
125-
if (!isset($input['body'])) {
126-
throw new \InvalidArgumentException('Missing the required parameter $body when calling placeOrder');
127-
}
128-
$body = $input['body'];
129130

130-
131-
return response('How about implementing placeOrder as a POST method ?');
131+
return response('How about implementing getOrderById as a GET method ?');
132132
}
133133
}

0 commit comments

Comments
 (0)