Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1385 Generate constants for path in spring boot @RequestMapping #19782

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Sets;
import com.samskivert.mustache.Mustache;
import io.swagger.v3.oas.models.OpenAPI;
Expand All @@ -45,6 +46,7 @@
import org.openapitools.codegen.model.ModelsMap;
import org.openapitools.codegen.model.OperationMap;
import org.openapitools.codegen.model.OperationsMap;
import org.openapitools.codegen.templating.mustache.ReplaceAllLambda;
import org.openapitools.codegen.utils.CamelizeOption;
import org.openapitools.codegen.utils.ModelUtils;
import org.slf4j.Logger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ public interface {{classname}} {
{{/jdk8-default-interface}}
{{#operation}}

public static final String PATH_{{#lambda.uppercase}}{{#lambda.snakecase}}{{{operationId}}}{{/lambda.snakecase}}{{/lambda.uppercase}} = "{{{path}}}";
/**
* {{httpMethod}} {{{path}}}{{#summary}} : {{.}}{{/summary}}
{{#notes}}
Expand Down Expand Up @@ -232,7 +233,7 @@ public interface {{classname}} {
{{/implicitHeadersParams.0}}
@RequestMapping(
method = RequestMethod.{{httpMethod}},
value = "{{{path}}}"{{#singleContentTypes}}{{#hasProduces}},
value = {{classname}}.PATH_{{#lambda.uppercase}}{{#lambda.snakecase}}{{{operationId}}}{{/lambda.snakecase}}{{/lambda.uppercase}}{{#singleContentTypes}}{{#hasProduces}},
produces = { {{#vendorExtensions.x-accepts}}"{{{.}}}"{{^-last}}, {{/-last}}{{/vendorExtensions.x-accepts}} }{{/hasProduces}}{{#hasConsumes}},
consumes = "{{{vendorExtensions.x-content-type}}}"{{/hasConsumes}}{{/singleContentTypes}}{{^singleContentTypes}}{{#hasProduces}},
produces = { {{#produces}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/produces}} }{{/hasProduces}}{{#hasConsumes}},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private void assertFilesFromMergedSpec(String mergedSpec) throws IOException {
.assertMethod("spec1OperationComplex")
.hasReturnType("ResponseEntity<Spec1Model>")
.assertMethodAnnotations()
.containsWithNameAndAttributes("RequestMapping", ImmutableMap.of("value", "\"/spec1/complex/{param1}/path\""))
.containsWithNameAndAttributes("RequestMapping", ImmutableMap.of("value", "Spec1Api.PATH_SPEC1_OPERATION_COMPLEX"))
.toMethod()
.assertParameter("param1")
.hasType("String")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public void doAnnotateDatesOnModelParameters() throws IOException {
.containsWithNameAndAttributes("Operation", ImmutableMap.of("operationId", "\"getZebras\""))
.containsWithNameAndAttributes("RequestMapping", ImmutableMap.of(
"method", "RequestMethod.GET",
"value", "\"/zebras\""
"value", "ZebrasApi.PATH_GET_ZEBRAS"
))
.toMethod()
.assertParameter("limit").hasType("BigDecimal")
Expand Down Expand Up @@ -201,7 +201,7 @@ public void doAnnotateDatesOnModelParametersWithOptionalAndJsonNullable() throws
.containsWithNameAndAttributes("Operation", ImmutableMap.of("operationId", "\"getZebras\""))
.containsWithNameAndAttributes("RequestMapping", ImmutableMap.of(
"method", "RequestMethod.GET",
"value", "\"/zebras\""
"value", "ZebrasApi.PATH_GET_ZEBRAS"
))
.toMethod()
.assertParameter("limit").hasType("Optional<BigDecimal>")
Expand Down Expand Up @@ -1108,8 +1108,10 @@ public void testRequestMappingAnnotation() throws IOException {
final Map<String, File> files = generateFiles(codegen, "src/test/resources/2_0/petstore.yaml");

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

// Check that the @RequestMapping annotation is not generated in the Controller file
final File petApiControllerFile = files.get("PetApiController.java");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@
@Validated
public interface SomeApi {

public static final String PATH_SOME_ENDPOINT_GET = "/some/endpoint";
/**
* GET /some/endpoint
*
* @return OK (status code 200)
*/
@RequestMapping(
method = RequestMethod.GET,
value = "/some/endpoint"
value = SomeApi.PATH_SOME_ENDPOINT_GET
)

ResponseEntity<Void> someEndpointGet(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
@Api(value = "Default", description = "the Default API")
public interface DefaultApi {

public static final String PATH_GET = "/thingy/{date}";
/**
* GET /thingy/{date}
*
Expand All @@ -48,7 +49,7 @@ public interface DefaultApi {
})
@RequestMapping(
method = RequestMethod.GET,
value = "/thingy/{date}"
value = DefaultApi.PATH_GET
)

ResponseEntity<Void> get(
Expand All @@ -59,6 +60,7 @@ ResponseEntity<Void> get(
);


public static final String PATH_UPDATE_PET_WITH_FORM = "/thingy/{date}";
/**
* POST /thingy/{date}
* update with form data
Expand All @@ -77,7 +79,7 @@ ResponseEntity<Void> get(
})
@RequestMapping(
method = RequestMethod.POST,
value = "/thingy/{date}",
value = DefaultApi.PATH_UPDATE_PET_WITH_FORM,
consumes = "application/x-www-form-urlencoded"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
@Tag(name = "pet", description = "Everything about your Pets")
public interface PetApi {

public static final String PATH_ADD_PET = "/pet";
/**
* POST /pet : Add a new pet to the store
*
Expand All @@ -59,7 +60,7 @@ public interface PetApi {
)
@RequestMapping(
method = RequestMethod.POST,
value = "/pet",
value = PetApi.PATH_ADD_PET,
consumes = "application/json"
)

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


public static final String PATH_DELETE_PET = "/pet/{petId}";
/**
* DELETE /pet/{petId} : Deletes a pet
*
Expand All @@ -90,7 +92,7 @@ ResponseEntity<Void> addPet(
)
@RequestMapping(
method = RequestMethod.DELETE,
value = "/pet/{petId}"
value = PetApi.PATH_DELETE_PET
)

ResponseEntity<Void> deletePet(
Expand All @@ -99,6 +101,7 @@ ResponseEntity<Void> deletePet(
);


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

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


public static final String PATH_FIND_PETS_BY_TAGS = "/pet/findByTags";
/**
* GET /pet/findByTags : Finds Pets by tags
* Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
Expand Down Expand Up @@ -163,7 +167,7 @@ ResponseEntity<List<Pet>> findPetsByStatus(
)
@RequestMapping(
method = RequestMethod.GET,
value = "/pet/findByTags",
value = PetApi.PATH_FIND_PETS_BY_TAGS,
produces = { "application/json", "application/xml" }
)

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


public static final String PATH_GET_PET_BY_ID = "/pet/{petId}";
/**
* GET /pet/{petId} : Find pet by ID
* Returns a single pet
Expand Down Expand Up @@ -200,7 +205,7 @@ ResponseEntity<List<Pet>> findPetsByTags(
)
@RequestMapping(
method = RequestMethod.GET,
value = "/pet/{petId}",
value = PetApi.PATH_GET_PET_BY_ID,
produces = { "application/json", "application/xml" }
)

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


public static final String PATH_UPDATE_PET = "/pet";
/**
* PUT /pet : Update an existing pet
*
Expand All @@ -234,7 +240,7 @@ ResponseEntity<Pet> getPetById(
)
@RequestMapping(
method = RequestMethod.PUT,
value = "/pet",
value = PetApi.PATH_UPDATE_PET,
consumes = "application/json"
)

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


public static final String PATH_UPDATE_PET_WITH_FORM = "/pet/{petId}";
/**
* POST /pet/{petId} : Updates a pet in the store with form data
*
Expand All @@ -266,7 +273,7 @@ ResponseEntity<Void> updatePet(
)
@RequestMapping(
method = RequestMethod.POST,
value = "/pet/{petId}",
value = PetApi.PATH_UPDATE_PET_WITH_FORM,
consumes = "application/x-www-form-urlencoded"
)

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


public static final String PATH_UPLOAD_FILE = "/pet/{petId}/uploadImage";
/**
* POST /pet/{petId}/uploadImage : uploads an image
*
Expand All @@ -302,7 +310,7 @@ ResponseEntity<Void> updatePetWithForm(
)
@RequestMapping(
method = RequestMethod.POST,
value = "/pet/{petId}/uploadImage",
value = PetApi.PATH_UPLOAD_FILE,
produces = { "application/json" },
consumes = "multipart/form-data"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
@Tag(name = "store", description = "Access to Petstore orders")
public interface StoreApi {

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

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


public static final String PATH_GET_INVENTORY = "/store/inventory";
/**
* GET /store/inventory : Returns pet inventories by status
* Returns a map of status codes to quantities
Expand All @@ -88,7 +90,7 @@ ResponseEntity<Void> deleteOrder(
)
@RequestMapping(
method = RequestMethod.GET,
value = "/store/inventory",
value = StoreApi.PATH_GET_INVENTORY,
produces = { "application/json" }
)

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


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

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


public static final String PATH_PLACE_ORDER = "/store/order";
/**
* POST /store/order : Place an order for a pet
*
Expand All @@ -154,7 +158,7 @@ ResponseEntity<Order> getOrderById(
)
@RequestMapping(
method = RequestMethod.POST,
value = "/store/order",
value = StoreApi.PATH_PLACE_ORDER,
produces = { "application/json", "application/xml" },
consumes = "application/json"
)
Expand Down
Loading
Loading