Skip to content

Commit 4e3a3ba

Browse files
committed
1385 Generate constants for path in spring boot @RequestMapping
1 parent 008c1a4 commit 4e3a3ba

File tree

182 files changed

+2249
-1125
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

182 files changed

+2249
-1125
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.fasterxml.jackson.databind.JsonNode;
2121
import com.fasterxml.jackson.databind.node.ArrayNode;
2222
import com.google.common.base.Strings;
23+
import com.google.common.collect.ImmutableMap;
2324
import com.google.common.collect.Sets;
2425
import com.samskivert.mustache.Mustache;
2526
import io.swagger.v3.oas.models.OpenAPI;
@@ -45,6 +46,7 @@
4546
import org.openapitools.codegen.model.ModelsMap;
4647
import org.openapitools.codegen.model.OperationMap;
4748
import org.openapitools.codegen.model.OperationsMap;
49+
import org.openapitools.codegen.templating.mustache.ReplaceAllLambda;
4850
import org.openapitools.codegen.utils.CamelizeOption;
4951
import org.openapitools.codegen.utils.ModelUtils;
5052
import org.slf4j.Logger;

modules/openapi-generator/src/main/resources/JavaSpring/api.mustache

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ public interface {{classname}} {
127127
{{/jdk8-default-interface}}
128128
{{#operation}}
129129

130+
public static final String PATH_{{#lambda.uppercase}}{{#lambda.snakecase}}{{{operationId}}}{{/lambda.snakecase}}{{/lambda.uppercase}} = "{{{path}}}";
130131
/**
131132
* {{httpMethod}} {{{path}}}{{#summary}} : {{.}}{{/summary}}
132133
{{#notes}}
@@ -232,7 +233,7 @@ public interface {{classname}} {
232233
{{/implicitHeadersParams.0}}
233234
@RequestMapping(
234235
method = RequestMethod.{{httpMethod}},
235-
value = "{{{path}}}"{{#singleContentTypes}}{{#hasProduces}},
236+
value = {{classname}}.PATH_{{#lambda.uppercase}}{{#lambda.snakecase}}{{{operationId}}}{{/lambda.snakecase}}{{/lambda.uppercase}}{{#singleContentTypes}}{{#hasProduces}},
236237
produces = { {{#vendorExtensions.x-accepts}}"{{{.}}}"{{^-last}}, {{/-last}}{{/vendorExtensions.x-accepts}} }{{/hasProduces}}{{#hasConsumes}},
237238
consumes = "{{{vendorExtensions.x-content-type}}}"{{/hasConsumes}}{{/singleContentTypes}}{{^singleContentTypes}}{{#hasProduces}},
238239
produces = { {{#produces}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/produces}} }{{/hasProduces}}{{#hasConsumes}},

modules/openapi-generator/src/test/java/org/openapitools/codegen/config/MergedSpecBuilderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ private void assertFilesFromMergedSpec(String mergedSpec) throws IOException {
7575
.assertMethod("spec1OperationComplex")
7676
.hasReturnType("ResponseEntity<Spec1Model>")
7777
.assertMethodAnnotations()
78-
.containsWithNameAndAttributes("RequestMapping", ImmutableMap.of("value", "\"/spec1/complex/{param1}/path\""))
78+
.containsWithNameAndAttributes("RequestMapping", ImmutableMap.of("value", "Spec1Api.PATH_SPEC1_OPERATION_COMPLEX"))
7979
.toMethod()
8080
.assertParameter("param1")
8181
.hasType("String")

modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public void doAnnotateDatesOnModelParameters() throws IOException {
123123
.containsWithNameAndAttributes("Operation", ImmutableMap.of("operationId", "\"getZebras\""))
124124
.containsWithNameAndAttributes("RequestMapping", ImmutableMap.of(
125125
"method", "RequestMethod.GET",
126-
"value", "\"/zebras\""
126+
"value", "ZebrasApi.PATH_GET_ZEBRAS"
127127
))
128128
.toMethod()
129129
.assertParameter("limit").hasType("BigDecimal")
@@ -201,7 +201,7 @@ public void doAnnotateDatesOnModelParametersWithOptionalAndJsonNullable() throws
201201
.containsWithNameAndAttributes("Operation", ImmutableMap.of("operationId", "\"getZebras\""))
202202
.containsWithNameAndAttributes("RequestMapping", ImmutableMap.of(
203203
"method", "RequestMethod.GET",
204-
"value", "\"/zebras\""
204+
"value", "ZebrasApi.PATH_GET_ZEBRAS"
205205
))
206206
.toMethod()
207207
.assertParameter("limit").hasType("Optional<BigDecimal>")
@@ -1108,8 +1108,10 @@ public void testRequestMappingAnnotation() throws IOException {
11081108
final Map<String, File> files = generateFiles(codegen, "src/test/resources/2_0/petstore.yaml");
11091109

11101110
// Check that the @RequestMapping annotation is generated in the Api file
1111-
final File petApiFile = files.get("PetApi.java");
1112-
assertFileContains(petApiFile.toPath(), "@RequestMapping(\"${openapi.openAPIPetstore.base-path:/v2}\")");
1111+
JavaFileAssert.assertThat(files.get("PetApi.java"))
1112+
.fileContains("@RequestMapping(\"${openapi.openAPIPetstore.base-path:/v2}\")",
1113+
"public static final String PATH_ADD_PET = \"/pet\";",
1114+
"value = PetApi.PATH_ADD_PET");
11131115

11141116
// Check that the @RequestMapping annotation is not generated in the Controller file
11151117
final File petApiControllerFile = files.get("PetApiController.java");

samples/client/petstore/spring-cloud-auth/src/main/java/org/openapitools/api/SomeApi.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@
2424
@Validated
2525
public interface SomeApi {
2626

27+
public static final String PATH_SOME_ENDPOINT_GET = "/some/endpoint";
2728
/**
2829
* GET /some/endpoint
2930
*
3031
* @return OK (status code 200)
3132
*/
3233
@RequestMapping(
3334
method = RequestMethod.GET,
34-
value = "/some/endpoint"
35+
value = SomeApi.PATH_SOME_ENDPOINT_GET
3536
)
3637

3738
ResponseEntity<Void> someEndpointGet(

samples/client/petstore/spring-cloud-date-time/src/main/java/org/openapitools/api/DefaultApi.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
@Api(value = "Default", description = "the Default API")
3030
public interface DefaultApi {
3131

32+
public static final String PATH_GET = "/thingy/{date}";
3233
/**
3334
* GET /thingy/{date}
3435
*
@@ -48,7 +49,7 @@ public interface DefaultApi {
4849
})
4950
@RequestMapping(
5051
method = RequestMethod.GET,
51-
value = "/thingy/{date}"
52+
value = DefaultApi.PATH_GET
5253
)
5354

5455
ResponseEntity<Void> get(
@@ -59,6 +60,7 @@ ResponseEntity<Void> get(
5960
);
6061

6162

63+
public static final String PATH_UPDATE_PET_WITH_FORM = "/thingy/{date}";
6264
/**
6365
* POST /thingy/{date}
6466
* update with form data
@@ -77,7 +79,7 @@ ResponseEntity<Void> get(
7779
})
7880
@RequestMapping(
7981
method = RequestMethod.POST,
80-
value = "/thingy/{date}",
82+
value = DefaultApi.PATH_UPDATE_PET_WITH_FORM,
8183
consumes = "application/x-www-form-urlencoded"
8284
)
8385

samples/client/petstore/spring-cloud-deprecated/src/main/java/org/openapitools/api/PetApi.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
@Tag(name = "pet", description = "Everything about your Pets")
3939
public interface PetApi {
4040

41+
public static final String PATH_ADD_PET = "/pet";
4142
/**
4243
* POST /pet : Add a new pet to the store
4344
*
@@ -59,7 +60,7 @@ public interface PetApi {
5960
)
6061
@RequestMapping(
6162
method = RequestMethod.POST,
62-
value = "/pet",
63+
value = PetApi.PATH_ADD_PET,
6364
consumes = "application/json"
6465
)
6566

@@ -68,6 +69,7 @@ ResponseEntity<Void> addPet(
6869
);
6970

7071

72+
public static final String PATH_DELETE_PET = "/pet/{petId}";
7173
/**
7274
* DELETE /pet/{petId} : Deletes a pet
7375
*
@@ -90,7 +92,7 @@ ResponseEntity<Void> addPet(
9092
)
9193
@RequestMapping(
9294
method = RequestMethod.DELETE,
93-
value = "/pet/{petId}"
95+
value = PetApi.PATH_DELETE_PET
9496
)
9597

9698
ResponseEntity<Void> deletePet(
@@ -99,6 +101,7 @@ ResponseEntity<Void> deletePet(
99101
);
100102

101103

104+
public static final String PATH_FIND_PETS_BY_STATUS = "/pet/findByStatus";
102105
/**
103106
* GET /pet/findByStatus : Finds Pets by status
104107
* Multiple status values can be provided with comma separated strings
@@ -125,7 +128,7 @@ ResponseEntity<Void> deletePet(
125128
)
126129
@RequestMapping(
127130
method = RequestMethod.GET,
128-
value = "/pet/findByStatus",
131+
value = PetApi.PATH_FIND_PETS_BY_STATUS,
129132
produces = { "application/json", "application/xml" }
130133
)
131134

@@ -134,6 +137,7 @@ ResponseEntity<List<Pet>> findPetsByStatus(
134137
);
135138

136139

140+
public static final String PATH_FIND_PETS_BY_TAGS = "/pet/findByTags";
137141
/**
138142
* GET /pet/findByTags : Finds Pets by tags
139143
* Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
@@ -163,7 +167,7 @@ ResponseEntity<List<Pet>> findPetsByStatus(
163167
)
164168
@RequestMapping(
165169
method = RequestMethod.GET,
166-
value = "/pet/findByTags",
170+
value = PetApi.PATH_FIND_PETS_BY_TAGS,
167171
produces = { "application/json", "application/xml" }
168172
)
169173

@@ -172,6 +176,7 @@ ResponseEntity<List<Pet>> findPetsByTags(
172176
);
173177

174178

179+
public static final String PATH_GET_PET_BY_ID = "/pet/{petId}";
175180
/**
176181
* GET /pet/{petId} : Find pet by ID
177182
* Returns a single pet
@@ -200,7 +205,7 @@ ResponseEntity<List<Pet>> findPetsByTags(
200205
)
201206
@RequestMapping(
202207
method = RequestMethod.GET,
203-
value = "/pet/{petId}",
208+
value = PetApi.PATH_GET_PET_BY_ID,
204209
produces = { "application/json", "application/xml" }
205210
)
206211

@@ -209,6 +214,7 @@ ResponseEntity<Pet> getPetById(
209214
);
210215

211216

217+
public static final String PATH_UPDATE_PET = "/pet";
212218
/**
213219
* PUT /pet : Update an existing pet
214220
*
@@ -234,7 +240,7 @@ ResponseEntity<Pet> getPetById(
234240
)
235241
@RequestMapping(
236242
method = RequestMethod.PUT,
237-
value = "/pet",
243+
value = PetApi.PATH_UPDATE_PET,
238244
consumes = "application/json"
239245
)
240246

@@ -243,6 +249,7 @@ ResponseEntity<Void> updatePet(
243249
);
244250

245251

252+
public static final String PATH_UPDATE_PET_WITH_FORM = "/pet/{petId}";
246253
/**
247254
* POST /pet/{petId} : Updates a pet in the store with form data
248255
*
@@ -266,7 +273,7 @@ ResponseEntity<Void> updatePet(
266273
)
267274
@RequestMapping(
268275
method = RequestMethod.POST,
269-
value = "/pet/{petId}",
276+
value = PetApi.PATH_UPDATE_PET_WITH_FORM,
270277
consumes = "application/x-www-form-urlencoded"
271278
)
272279

@@ -277,6 +284,7 @@ ResponseEntity<Void> updatePetWithForm(
277284
);
278285

279286

287+
public static final String PATH_UPLOAD_FILE = "/pet/{petId}/uploadImage";
280288
/**
281289
* POST /pet/{petId}/uploadImage : uploads an image
282290
*
@@ -302,7 +310,7 @@ ResponseEntity<Void> updatePetWithForm(
302310
)
303311
@RequestMapping(
304312
method = RequestMethod.POST,
305-
value = "/pet/{petId}/uploadImage",
313+
value = PetApi.PATH_UPLOAD_FILE,
306314
produces = { "application/json" },
307315
consumes = "multipart/form-data"
308316
)

samples/client/petstore/spring-cloud-deprecated/src/main/java/org/openapitools/api/StoreApi.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
@Tag(name = "store", description = "Access to Petstore orders")
3939
public interface StoreApi {
4040

41+
public static final String PATH_DELETE_ORDER = "/store/order/{orderId}";
4142
/**
4243
* DELETE /store/order/{orderId} : Delete purchase order by ID
4344
* For valid response try integer IDs with value &lt; 1000. Anything above 1000 or nonintegers will generate API errors
@@ -58,14 +59,15 @@ public interface StoreApi {
5859
)
5960
@RequestMapping(
6061
method = RequestMethod.DELETE,
61-
value = "/store/order/{orderId}"
62+
value = StoreApi.PATH_DELETE_ORDER
6263
)
6364

6465
ResponseEntity<Void> deleteOrder(
6566
@Parameter(name = "orderId", description = "ID of the order that needs to be deleted", required = true, in = ParameterIn.PATH) @PathVariable("orderId") String orderId
6667
);
6768

6869

70+
public static final String PATH_GET_INVENTORY = "/store/inventory";
6971
/**
7072
* GET /store/inventory : Returns pet inventories by status
7173
* Returns a map of status codes to quantities
@@ -88,7 +90,7 @@ ResponseEntity<Void> deleteOrder(
8890
)
8991
@RequestMapping(
9092
method = RequestMethod.GET,
91-
value = "/store/inventory",
93+
value = StoreApi.PATH_GET_INVENTORY,
9294
produces = { "application/json" }
9395
)
9496

@@ -97,6 +99,7 @@ ResponseEntity<Map<String, Integer>> getInventory(
9799
);
98100

99101

102+
public static final String PATH_GET_ORDER_BY_ID = "/store/order/{orderId}";
100103
/**
101104
* GET /store/order/{orderId} : Find purchase order by ID
102105
* For valid response try integer IDs with value &lt;&#x3D; 5 or &gt; 10. Other values will generate exceptions
@@ -122,7 +125,7 @@ ResponseEntity<Map<String, Integer>> getInventory(
122125
)
123126
@RequestMapping(
124127
method = RequestMethod.GET,
125-
value = "/store/order/{orderId}",
128+
value = StoreApi.PATH_GET_ORDER_BY_ID,
126129
produces = { "application/json", "application/xml" }
127130
)
128131

@@ -131,6 +134,7 @@ ResponseEntity<Order> getOrderById(
131134
);
132135

133136

137+
public static final String PATH_PLACE_ORDER = "/store/order";
134138
/**
135139
* POST /store/order : Place an order for a pet
136140
*
@@ -154,7 +158,7 @@ ResponseEntity<Order> getOrderById(
154158
)
155159
@RequestMapping(
156160
method = RequestMethod.POST,
157-
value = "/store/order",
161+
value = StoreApi.PATH_PLACE_ORDER,
158162
produces = { "application/json", "application/xml" },
159163
consumes = "application/json"
160164
)

0 commit comments

Comments
 (0)