Skip to content

Commit 3cb178c

Browse files
committed
sort the endpoints in ascending to avoid the route priority issure
1 parent 0effbae commit 3cb178c

File tree

5 files changed

+138
-127
lines changed

5 files changed

+138
-127
lines changed

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

Lines changed: 23 additions & 12 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.
@@ -171,9 +164,27 @@ public String modelFileFolder() {
171164
@Override
172165
public String apiFileFolder() {
173166
return outputFolder + "/app/Http/controllers";
174-
// return outputFolder + "/" + sourceFolder + "/" + apiPackage().replace('.', File.separatorChar);
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
}

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

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -111,35 +111,43 @@ public function createUsersWithListInput()
111111
return response('How about implementing createUsersWithListInput as a POST method ?');
112112
}
113113
/**
114-
* Operation deleteUser
114+
* Operation loginUser
115115
*
116-
* Delete user.
116+
* Logs user into the system.
117117
*
118-
* @param String $username The name that needs to be deleted (required)
119118
*
120119
* @return Http response
121120
*/
122-
public function deleteUser($username)
121+
public function loginUser()
123122
{
124123
$input = Request::all();
125124

126125
//path params validation
127126

128127

129128
//not path params validation
129+
if (!isset($input['username'])) {
130+
throw new \InvalidArgumentException('Missing the required parameter $username when calling loginUser');
131+
}
132+
$username = $input['username'];
130133

131-
return response('How about implementing deleteUser as a DELETE method ?');
134+
if (!isset($input['password'])) {
135+
throw new \InvalidArgumentException('Missing the required parameter $password when calling loginUser');
136+
}
137+
$password = $input['password'];
138+
139+
140+
return response('How about implementing loginUser as a GET method ?');
132141
}
133142
/**
134-
* Operation getUserByName
143+
* Operation logoutUser
135144
*
136-
* Get user by user name.
145+
* Logs out current logged in user session.
137146
*
138-
* @param String $username The name that needs to be fetched. Use user1 for testing. (required)
139147
*
140148
* @return Http response
141149
*/
142-
public function getUserByName($username)
150+
public function logoutUser()
143151
{
144152
$input = Request::all();
145153

@@ -148,46 +156,38 @@ public function getUserByName($username)
148156

149157
//not path params validation
150158

151-
return response('How about implementing getUserByName as a GET method ?');
159+
return response('How about implementing logoutUser as a GET method ?');
152160
}
153161
/**
154-
* Operation loginUser
162+
* Operation deleteUser
155163
*
156-
* Logs user into the system.
164+
* Delete user.
157165
*
166+
* @param String $username The name that needs to be deleted (required)
158167
*
159168
* @return Http response
160169
*/
161-
public function loginUser()
170+
public function deleteUser($username)
162171
{
163172
$input = Request::all();
164173

165174
//path params validation
166175

167176

168177
//not path params validation
169-
if (!isset($input['username'])) {
170-
throw new \InvalidArgumentException('Missing the required parameter $username when calling loginUser');
171-
}
172-
$username = $input['username'];
173-
174-
if (!isset($input['password'])) {
175-
throw new \InvalidArgumentException('Missing the required parameter $password when calling loginUser');
176-
}
177-
$password = $input['password'];
178178

179-
180-
return response('How about implementing loginUser as a GET method ?');
179+
return response('How about implementing deleteUser as a DELETE method ?');
181180
}
182181
/**
183-
* Operation logoutUser
182+
* Operation getUserByName
184183
*
185-
* Logs out current logged in user session.
184+
* Get user by user name.
186185
*
186+
* @param String $username The name that needs to be fetched. Use user1 for testing. (required)
187187
*
188188
* @return Http response
189189
*/
190-
public function logoutUser()
190+
public function getUserByName($username)
191191
{
192192
$input = Request::all();
193193

@@ -196,7 +196,7 @@ public function logoutUser()
196196

197197
//not path params validation
198198

199-
return response('How about implementing logoutUser as a GET method ?');
199+
return response('How about implementing getUserByName as a GET method ?');
200200
}
201201
/**
202202
* Operation updateUser

0 commit comments

Comments
 (0)