Skip to content

Commit 117be2c

Browse files
authored
1385 Generate constants for path in spring boot @RequestMapping (#19782)
1 parent 1b352c2 commit 117be2c

File tree

194 files changed

+2377
-1189
lines changed

Some content is hidden

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

194 files changed

+2377
-1189
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
@@ -22,6 +22,7 @@
2222
import com.fasterxml.jackson.databind.node.ArrayNode;
2323
import com.fasterxml.jackson.databind.node.ObjectNode;
2424
import com.google.common.base.Strings;
25+
import com.google.common.collect.ImmutableMap;
2526
import com.google.common.collect.Sets;
2627
import com.samskivert.mustache.Mustache;
2728
import io.swagger.v3.oas.models.OpenAPI;
@@ -50,6 +51,7 @@
5051
import org.openapitools.codegen.model.ModelsMap;
5152
import org.openapitools.codegen.model.OperationMap;
5253
import org.openapitools.codegen.model.OperationsMap;
54+
import org.openapitools.codegen.templating.mustache.ReplaceAllLambda;
5355
import org.openapitools.codegen.utils.CamelizeOption;
5456
import org.openapitools.codegen.utils.ModelUtils;
5557
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
@@ -129,6 +129,7 @@ public interface {{classname}} {
129129
{{/jdk8-default-interface}}
130130
{{#operation}}
131131

132+
public static final String PATH_{{#lambda.uppercase}}{{#lambda.snakecase}}{{{operationId}}}{{/lambda.snakecase}}{{/lambda.uppercase}} = "{{{path}}}";
132133
/**
133134
* {{httpMethod}} {{{path}}}{{#summary}} : {{.}}{{/summary}}
134135
{{#notes}}
@@ -246,7 +247,7 @@ public interface {{classname}} {
246247
{{/implicitHeadersParams.0}}
247248
@RequestMapping(
248249
method = RequestMethod.{{httpMethod}},
249-
value = "{{{path}}}"{{#singleContentTypes}}{{#hasProduces}},
250+
value = {{classname}}.PATH_{{#lambda.uppercase}}{{#lambda.snakecase}}{{{operationId}}}{{/lambda.snakecase}}{{/lambda.uppercase}}{{#singleContentTypes}}{{#hasProduces}},
250251
produces = { {{#vendorExtensions.x-accepts}}"{{{.}}}"{{^-last}}, {{/-last}}{{/vendorExtensions.x-accepts}} }{{/hasProduces}}{{#hasConsumes}},
251252
consumes = "{{{vendorExtensions.x-content-type}}}"{{/hasConsumes}}{{/singleContentTypes}}{{^singleContentTypes}}{{#hasProduces}},
252253
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
@@ -73,7 +73,7 @@ private void assertFilesFromMergedSpec(String mergedSpec) throws IOException {
7373
.assertMethod("spec1OperationComplex")
7474
.hasReturnType("ResponseEntity<Spec1Model>")
7575
.assertMethodAnnotations()
76-
.containsWithNameAndAttributes("RequestMapping", ImmutableMap.of("value", "\"/spec1/complex/{param1}/path\""))
76+
.containsWithNameAndAttributes("RequestMapping", ImmutableMap.of("value", "Spec1Api.PATH_SPEC1_OPERATION_COMPLEX"))
7777
.toMethod()
7878
.assertParameter("param1")
7979
.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
@@ -25,14 +25,15 @@
2525
@Validated
2626
public interface SomeApi {
2727

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

3839
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
@@ -30,6 +30,7 @@
3030
@Api(value = "Default", description = "the Default API")
3131
public interface DefaultApi {
3232

33+
public static final String PATH_GET = "/thingy/{date}";
3334
/**
3435
* GET /thingy/{date}
3536
*
@@ -49,7 +50,7 @@ public interface DefaultApi {
4950
})
5051
@RequestMapping(
5152
method = RequestMethod.GET,
52-
value = "/thingy/{date}"
53+
value = DefaultApi.PATH_GET
5354
)
5455

5556
ResponseEntity<Void> get(
@@ -60,6 +61,7 @@ ResponseEntity<Void> get(
6061
);
6162

6263

64+
public static final String PATH_UPDATE_PET_WITH_FORM = "/thingy/{date}";
6365
/**
6466
* POST /thingy/{date}
6567
* update with form data
@@ -78,7 +80,7 @@ ResponseEntity<Void> get(
7880
})
7981
@RequestMapping(
8082
method = RequestMethod.POST,
81-
value = "/thingy/{date}",
83+
value = DefaultApi.PATH_UPDATE_PET_WITH_FORM,
8284
consumes = "application/x-www-form-urlencoded"
8385
)
8486

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
@@ -40,6 +40,7 @@
4040
@Tag(name = "pet", description = "Everything about your Pets")
4141
public interface PetApi {
4242

43+
public static final String PATH_ADD_PET = "/pet";
4344
/**
4445
* POST /pet : Add a new pet to the store
4546
*
@@ -61,7 +62,7 @@ public interface PetApi {
6162
)
6263
@RequestMapping(
6364
method = RequestMethod.POST,
64-
value = "/pet",
65+
value = PetApi.PATH_ADD_PET,
6566
consumes = "application/json"
6667
)
6768

@@ -70,6 +71,7 @@ ResponseEntity<Void> addPet(
7071
);
7172

7273

74+
public static final String PATH_DELETE_PET = "/pet/{petId}";
7375
/**
7476
* DELETE /pet/{petId} : Deletes a pet
7577
*
@@ -92,7 +94,7 @@ ResponseEntity<Void> addPet(
9294
)
9395
@RequestMapping(
9496
method = RequestMethod.DELETE,
95-
value = "/pet/{petId}"
97+
value = PetApi.PATH_DELETE_PET
9698
)
9799

98100
ResponseEntity<Void> deletePet(
@@ -101,6 +103,7 @@ ResponseEntity<Void> deletePet(
101103
);
102104

103105

106+
public static final String PATH_FIND_PETS_BY_STATUS = "/pet/findByStatus";
104107
/**
105108
* GET /pet/findByStatus : Finds Pets by status
106109
* Multiple status values can be provided with comma separated strings
@@ -127,7 +130,7 @@ ResponseEntity<Void> deletePet(
127130
)
128131
@RequestMapping(
129132
method = RequestMethod.GET,
130-
value = "/pet/findByStatus",
133+
value = PetApi.PATH_FIND_PETS_BY_STATUS,
131134
produces = { "application/json", "application/xml" }
132135
)
133136

@@ -136,6 +139,7 @@ ResponseEntity<List<Pet>> findPetsByStatus(
136139
);
137140

138141

142+
public static final String PATH_FIND_PETS_BY_TAGS = "/pet/findByTags";
139143
/**
140144
* GET /pet/findByTags : Finds Pets by tags
141145
* Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
@@ -165,7 +169,7 @@ ResponseEntity<List<Pet>> findPetsByStatus(
165169
)
166170
@RequestMapping(
167171
method = RequestMethod.GET,
168-
value = "/pet/findByTags",
172+
value = PetApi.PATH_FIND_PETS_BY_TAGS,
169173
produces = { "application/json", "application/xml" }
170174
)
171175

@@ -174,6 +178,7 @@ ResponseEntity<List<Pet>> findPetsByTags(
174178
);
175179

176180

181+
public static final String PATH_GET_PET_BY_ID = "/pet/{petId}";
177182
/**
178183
* GET /pet/{petId} : Find pet by ID
179184
* Returns a single pet
@@ -202,7 +207,7 @@ ResponseEntity<List<Pet>> findPetsByTags(
202207
)
203208
@RequestMapping(
204209
method = RequestMethod.GET,
205-
value = "/pet/{petId}",
210+
value = PetApi.PATH_GET_PET_BY_ID,
206211
produces = { "application/json", "application/xml" }
207212
)
208213

@@ -211,6 +216,7 @@ ResponseEntity<Pet> getPetById(
211216
);
212217

213218

219+
public static final String PATH_UPDATE_PET = "/pet";
214220
/**
215221
* PUT /pet : Update an existing pet
216222
*
@@ -236,7 +242,7 @@ ResponseEntity<Pet> getPetById(
236242
)
237243
@RequestMapping(
238244
method = RequestMethod.PUT,
239-
value = "/pet",
245+
value = PetApi.PATH_UPDATE_PET,
240246
consumes = "application/json"
241247
)
242248

@@ -245,6 +251,7 @@ ResponseEntity<Void> updatePet(
245251
);
246252

247253

254+
public static final String PATH_UPDATE_PET_WITH_FORM = "/pet/{petId}";
248255
/**
249256
* POST /pet/{petId} : Updates a pet in the store with form data
250257
*
@@ -268,7 +275,7 @@ ResponseEntity<Void> updatePet(
268275
)
269276
@RequestMapping(
270277
method = RequestMethod.POST,
271-
value = "/pet/{petId}",
278+
value = PetApi.PATH_UPDATE_PET_WITH_FORM,
272279
consumes = "application/x-www-form-urlencoded"
273280
)
274281

@@ -279,6 +286,7 @@ ResponseEntity<Void> updatePetWithForm(
279286
);
280287

281288

289+
public static final String PATH_UPLOAD_FILE = "/pet/{petId}/uploadImage";
282290
/**
283291
* POST /pet/{petId}/uploadImage : uploads an image
284292
*
@@ -304,7 +312,7 @@ ResponseEntity<Void> updatePetWithForm(
304312
)
305313
@RequestMapping(
306314
method = RequestMethod.POST,
307-
value = "/pet/{petId}/uploadImage",
315+
value = PetApi.PATH_UPLOAD_FILE,
308316
produces = { "application/json" },
309317
consumes = "multipart/form-data"
310318
)

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
@@ -40,6 +40,7 @@
4040
@Tag(name = "store", description = "Access to Petstore orders")
4141
public interface StoreApi {
4242

43+
public static final String PATH_DELETE_ORDER = "/store/order/{orderId}";
4344
/**
4445
* DELETE /store/order/{orderId} : Delete purchase order by ID
4546
* For valid response try integer IDs with value &lt; 1000. Anything above 1000 or nonintegers will generate API errors
@@ -60,14 +61,15 @@ public interface StoreApi {
6061
)
6162
@RequestMapping(
6263
method = RequestMethod.DELETE,
63-
value = "/store/order/{orderId}"
64+
value = StoreApi.PATH_DELETE_ORDER
6465
)
6566

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

7071

72+
public static final String PATH_GET_INVENTORY = "/store/inventory";
7173
/**
7274
* GET /store/inventory : Returns pet inventories by status
7375
* Returns a map of status codes to quantities
@@ -90,7 +92,7 @@ ResponseEntity<Void> deleteOrder(
9092
)
9193
@RequestMapping(
9294
method = RequestMethod.GET,
93-
value = "/store/inventory",
95+
value = StoreApi.PATH_GET_INVENTORY,
9496
produces = { "application/json" }
9597
)
9698

@@ -99,6 +101,7 @@ ResponseEntity<Map<String, Integer>> getInventory(
99101
);
100102

101103

104+
public static final String PATH_GET_ORDER_BY_ID = "/store/order/{orderId}";
102105
/**
103106
* GET /store/order/{orderId} : Find purchase order by ID
104107
* For valid response try integer IDs with value &lt;&#x3D; 5 or &gt; 10. Other values will generate exceptions
@@ -124,7 +127,7 @@ ResponseEntity<Map<String, Integer>> getInventory(
124127
)
125128
@RequestMapping(
126129
method = RequestMethod.GET,
127-
value = "/store/order/{orderId}",
130+
value = StoreApi.PATH_GET_ORDER_BY_ID,
128131
produces = { "application/json", "application/xml" }
129132
)
130133

@@ -133,6 +136,7 @@ ResponseEntity<Order> getOrderById(
133136
);
134137

135138

139+
public static final String PATH_PLACE_ORDER = "/store/order";
136140
/**
137141
* POST /store/order : Place an order for a pet
138142
*
@@ -156,7 +160,7 @@ ResponseEntity<Order> getOrderById(
156160
)
157161
@RequestMapping(
158162
method = RequestMethod.POST,
159-
value = "/store/order",
163+
value = StoreApi.PATH_PLACE_ORDER,
160164
produces = { "application/json", "application/xml" },
161165
consumes = "application/json"
162166
)

0 commit comments

Comments
 (0)