Skip to content

Commit 8d51f71

Browse files
committed
store PATHs as accesible strings in companion objects.
1 parent 875b064 commit 8d51f71

80 files changed

Lines changed: 1272 additions & 515 deletions

File tree

Some content is hidden

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

modules/openapi-generator/src/main/resources/kotlin-spring/api.mustache

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ import org.springframework.validation.annotation.Validated
3030
{{/useBeanValidation}}
3131
import org.springframework.web.context.request.NativeWebRequest
3232
import org.springframework.beans.factory.annotation.Autowired
33+
{{#useRequestMappingOnController}}
34+
import {{#apiPackage}}{{.}}.{{/apiPackage}}{{classname}}.Companion.BASE_PATH
35+
{{/useRequestMappingOnController}}
3336

3437
{{#useBeanValidation}}
3538
import {{javaxPackage}}.validation.Valid
@@ -58,13 +61,13 @@ import kotlin.collections.Map
5861
{{/swagger1AnnotationLibrary}}
5962
{{#useRequestMappingOnController}}
6063
{{=<% %>=}}
61-
@RequestMapping("\${api.base-path:<%contextPath%>}")
64+
@RequestMapping("\${api.<%title%>.base-path:api.base-path:$BASE_PATH}")
6265
<%={{ }}=%>
6366
{{/useRequestMappingOnController}}
6467
{{#operations}}
6568
class {{classname}}Controller({{#serviceInterface}}@Autowired(required = true) val service: {{classname}}Service{{/serviceInterface}}) {
66-
{{#operation}}
6769
70+
{{#operation}}
6871
{{^useResponseEntity}}
6972
@ResponseStatus({{#springHttpStatus}}{{#responses.0}}{{{code}}}{{/responses.0}}{{/springHttpStatus}}){{!
7073
}}{{/useResponseEntity}}{{!
@@ -88,7 +91,7 @@ class {{classname}}Controller({{#serviceInterface}}@Autowired(required = true) v
8891
value = [{{#responses}}ApiResponse(code = {{{code}}}, message = "{{{message}}}"{{#baseType}}, response = {{{.}}}::class{{/baseType}}{{#containerType}}, responseContainer = "{{{.}}}"{{/containerType}}){{^-last}},{{/-last}}{{/responses}}]){{/swagger1AnnotationLibrary}}
8992
@RequestMapping(
9093
method = [RequestMethod.{{httpMethod}}],
91-
value = ["{{#lambdaEscapeInNormalString}}{{{path}}}{{/lambdaEscapeInNormalString}}"]{{#singleContentTypes}}{{#hasProduces}},
94+
value = [PATH_{{#lambda.uppercase}}{{#lambda.snakecase}}{{{operationId}}}{{/lambda.snakecase}}{{/lambda.uppercase}} /* "{{#lambdaEscapeInNormalString}}{{{path}}}{{/lambdaEscapeInNormalString}}" */]{{#singleContentTypes}}{{#hasProduces}},
9295
produces = [{{#vendorExtensions.x-accepts}}"{{{.}}}"{{^-last}}, {{/-last}}{{/vendorExtensions.x-accepts}}]{{/hasProduces}}{{#hasConsumes}},
9396
consumes = "{{{vendorExtensions.x-content-type}}}"{{/hasConsumes}}{{/singleContentTypes}}{{^singleContentTypes}}{{#hasProduces}},
9497
produces = [{{#produces}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/produces}}]{{/hasProduces}}{{#hasConsumes}},
@@ -100,6 +103,16 @@ class {{classname}}Controller({{#serviceInterface}}@Autowired(required = true) v
100103
{{/hasParams}}): {{#useResponseEntity}}ResponseEntity<{{/useResponseEntity}}{{>returnTypes}}{{#useResponseEntity}}>{{/useResponseEntity}} {
101104
return {{>returnValue}}
102105
}
106+
103107
{{/operation}}
108+
companion object {
109+
//for your own safety never directly reuse these path definitions in tests
110+
{{#useRequestMappingOnController}}
111+
const val BASE_PATH: String = "{{=<% %>=}}<%contextPath%><%={{ }}=%>"
112+
{{/useRequestMappingOnController}}
113+
{{#operation}}
114+
const val PATH_{{#lambda.uppercase}}{{#lambda.snakecase}}{{{operationId}}}{{/lambda.snakecase}}{{/lambda.uppercase}}: String = "{{{path}}}"
115+
{{/operation}}
116+
}
104117
}
105118
{{/operations}}

modules/openapi-generator/src/main/resources/kotlin-spring/apiInterface.mustache

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ import org.springframework.validation.annotation.Validated
3535
{{/useBeanValidation}}
3636
import org.springframework.web.context.request.NativeWebRequest
3737
import org.springframework.beans.factory.annotation.Autowired
38+
{{#useRequestMappingOnInterface}}
39+
import {{#apiPackage}}{{.}}.{{/apiPackage}}{{classname}}.Companion.BASE_PATH
40+
{{/useRequestMappingOnInterface}}
3841

3942
{{#useBeanValidation}}
4043
import {{javaxPackage}}.validation.constraints.DecimalMax
@@ -65,7 +68,7 @@ import kotlin.collections.Map
6568
{{/swagger1AnnotationLibrary}}
6669
{{#useRequestMappingOnInterface}}
6770
{{=<% %>=}}
68-
@RequestMapping("\${api.base-path:<%contextPath%>}")
71+
@RequestMapping("\${api.<%title%>.base-path:api.base-path:$BASE_PATH}")
6972
<%={{ }}=%>
7073
{{/useRequestMappingOnInterface}}
7174
{{#operations}}
@@ -74,8 +77,8 @@ interface {{classname}} {
7477

7578
fun getDelegate(): {{classname}}Delegate{{^skipDefaultDelegateInterface}} = object: {{classname}}Delegate {}{{/skipDefaultDelegateInterface}}
7679
{{/isDelegate}}
77-
{{#operation}}
7880

81+
{{#operation}}
7982
{{^useResponseEntity}}
8083
@ResponseStatus({{#springHttpStatus}}{{#responses.0}}{{{code}}}{{/responses.0}}{{/springHttpStatus}}){{!
8184
}}{{/useResponseEntity}}{{!
@@ -103,7 +106,7 @@ interface {{classname}} {
103106
){{/swagger1AnnotationLibrary}}
104107
@RequestMapping(
105108
method = [RequestMethod.{{httpMethod}}],
106-
value = ["{{#lambdaEscapeInNormalString}}{{{path}}}{{/lambdaEscapeInNormalString}}"]{{#singleContentTypes}}{{#hasProduces}},
109+
value = [PATH_{{#lambda.uppercase}}{{#lambda.snakecase}}{{{operationId}}}{{/lambda.snakecase}}{{/lambda.uppercase}} /* "{{#lambdaEscapeInNormalString}}{{{path}}}{{/lambdaEscapeInNormalString}}" */]{{#singleContentTypes}}{{#hasProduces}},
107110
produces = [{{#vendorExtensions.x-accepts}}"{{{.}}}"{{^-last}}, {{/-last}}{{/vendorExtensions.x-accepts}}]{{/hasProduces}}{{#hasConsumes}},
108111
consumes = "{{{vendorExtensions.x-content-type}}}"{{/hasConsumes}}{{/singleContentTypes}}{{^singleContentTypes}}{{#hasProduces}},
109112
produces = [{{#produces}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/produces}}]{{/hasProduces}}{{#hasConsumes}},
@@ -120,6 +123,16 @@ interface {{classname}} {
120123
return getDelegate().{{operationId}}({{#allParams}}{{{paramName}}}{{^-last}}, {{/-last}}{{/allParams}}{{#includeHttpRequestContext}}{{#hasParams}}, {{/hasParams}}{{#reactive}}exchange{{/reactive}}{{^reactive}}request{{/reactive}}{{/includeHttpRequestContext}})
121124
{{/isDelegate}}
122125
}{{/skipDefaultApiInterface}}
126+
123127
{{/operation}}
128+
companion object {
129+
//for your own safety never directly reuse these path definitions in tests
130+
{{#useRequestMappingOnInterface}}
131+
const val BASE_PATH: String = "{{=<% %>=}}<%contextPath%><%={{ }}=%>"
132+
{{/useRequestMappingOnInterface}}
133+
{{#operation}}
134+
const val PATH_{{#lambda.uppercase}}{{#lambda.snakecase}}{{{operationId}}}{{/lambda.snakecase}}{{/lambda.uppercase}}: String = "{{{path}}}"
135+
{{/operation}}
136+
}
124137
}
125138
{{/operations}}

modules/openapi-generator/src/main/resources/kotlin-spring/libraries/spring-declarative-http-interface/apiInterface.mustache

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,19 @@ import kotlin.collections.List
4848
import kotlin.collections.Map
4949

5050
{{#useRequestMappingOnInterface}}@HttpExchange(
51-
"{{=<% %>=}}\${api.base-path:$BASE_PATH}<%={{ }}=%>"
51+
"{{=<% %>=}}\${api.<%title%>.base-path:api.base-path:$BASE_PATH}<%={{ }}=%>"
5252
){{/useRequestMappingOnInterface}}
5353
{{#useBeanValidation}}
5454
@Validated
5555
{{/useBeanValidation}}
5656
{{#operations}}
5757
interface {{classname}} {
5858
59-
{{#operation}}
60-
{{#httpMethod}}
61-
@HttpExchange(
62-
url = PATH_{{#lambda.uppercase}}{{#lambda.snakecase}}{{{operationId}}}{{/lambda.snakecase}}{{/lambda.uppercase}},
59+
{{#operation}}{{!
60+
}}{{^useResponseEntity}} @ResponseStatus({{#springHttpStatus}}{{#responses.0}}{{{code}}}{{/responses.0}}{{/springHttpStatus}})
61+
{{/useResponseEntity}}{{!
62+
}}{{#httpMethod}} @HttpExchange(
63+
url = PATH_{{#lambda.uppercase}}{{#lambda.snakecase}}{{{operationId}}}{{/lambda.snakecase}}{{/lambda.uppercase}} /* "{{#lambdaEscapeInNormalString}}{{{path}}}{{/lambdaEscapeInNormalString}}" */,
6364
method = "{{httpMethod}}"
6465
)
6566
{{/httpMethod}}{{!

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,20 +1065,20 @@ public void generateHttpInterfaceReactiveWithReactorResponseEntity() throws Exce
10651065
"import reactor.core.publisher.Flux\n"
10661066
+ "import reactor.core.publisher.Mono",
10671067
" @HttpExchange(\n"
1068-
+ " url = PATH_GET_INVENTORY,\n"
1068+
+ " url = PATH_GET_INVENTORY /* \"/store/inventory\" */,\n"
10691069
+ " method = \"GET\"\n"
10701070
+ " )\n"
10711071
+ " fun getInventory(\n"
10721072
+ " ): Mono<ResponseEntity<Map<String, kotlin.Int>>>",
10731073
" @HttpExchange(\n"
1074-
+ " url = PATH_DELETE_ORDER,\n"
1074+
+ " url = PATH_DELETE_ORDER /* \"/store/order/{orderId}\" */,\n"
10751075
+ " method = \"DELETE\"\n"
10761076
+ " )\n"
10771077
+ " fun deleteOrder(\n"
10781078
+ " @Parameter(description = \"ID of the order that needs to be deleted\", required = true) @PathVariable(\"orderId\") orderId: kotlin.String\n"
10791079
+ " ): Mono<ResponseEntity<Unit>>",
10801080
" @HttpExchange(\n"
1081-
+ " url = PATH_PLACE_ORDER,\n"
1081+
+ " url = PATH_PLACE_ORDER /* \"/store/order\" */,\n"
10821082
+ " method = \"POST\"\n"
10831083
+ " )\n"
10841084
+ " fun placeOrder(\n"

samples/server/petstore/kotlin-spring-cloud/src/main/kotlin/org/openapitools/api/PetApi.kt

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ interface PetApi {
3535

3636
@RequestMapping(
3737
method = [RequestMethod.POST],
38-
value = ["/pet"],
38+
value = [PATH_ADD_PET /* "/pet" */],
3939
produces = ["application/xml", "application/json"],
4040
consumes = ["application/json", "application/xml"]
4141
)
@@ -48,7 +48,7 @@ interface PetApi {
4848

4949
@RequestMapping(
5050
method = [RequestMethod.DELETE],
51-
value = ["/pet/{petId}"]
51+
value = [PATH_DELETE_PET /* "/pet/{petId}" */]
5252
)
5353
fun deletePet(
5454
@PathVariable("petId") petId: kotlin.Long,
@@ -60,7 +60,7 @@ interface PetApi {
6060

6161
@RequestMapping(
6262
method = [RequestMethod.GET],
63-
value = ["/pet/findByStatus"],
63+
value = [PATH_FIND_PETS_BY_STATUS /* "/pet/findByStatus" */],
6464
produces = ["application/xml", "application/json"]
6565
)
6666
fun findPetsByStatus(
@@ -72,7 +72,7 @@ interface PetApi {
7272

7373
@RequestMapping(
7474
method = [RequestMethod.GET],
75-
value = ["/pet/findByTags"],
75+
value = [PATH_FIND_PETS_BY_TAGS /* "/pet/findByTags" */],
7676
produces = ["application/xml", "application/json"]
7777
)
7878
fun findPetsByTags(
@@ -84,7 +84,7 @@ interface PetApi {
8484

8585
@RequestMapping(
8686
method = [RequestMethod.GET],
87-
value = ["/pet/{petId}"],
87+
value = [PATH_GET_PET_BY_ID /* "/pet/{petId}" */],
8888
produces = ["application/xml", "application/json"]
8989
)
9090
fun getPetById(
@@ -96,7 +96,7 @@ interface PetApi {
9696

9797
@RequestMapping(
9898
method = [RequestMethod.PUT],
99-
value = ["/pet"],
99+
value = [PATH_UPDATE_PET /* "/pet" */],
100100
produces = ["application/xml", "application/json"],
101101
consumes = ["application/json", "application/xml"]
102102
)
@@ -109,7 +109,7 @@ interface PetApi {
109109

110110
@RequestMapping(
111111
method = [RequestMethod.POST],
112-
value = ["/pet/{petId}"],
112+
value = [PATH_UPDATE_PET_WITH_FORM /* "/pet/{petId}" */],
113113
consumes = ["application/x-www-form-urlencoded"]
114114
)
115115
fun updatePetWithForm(
@@ -123,7 +123,7 @@ interface PetApi {
123123

124124
@RequestMapping(
125125
method = [RequestMethod.POST],
126-
value = ["/pet/{petId}/uploadImage"],
126+
value = [PATH_UPLOAD_FILE /* "/pet/{petId}/uploadImage" */],
127127
produces = ["application/json"],
128128
consumes = ["multipart/form-data"]
129129
)
@@ -134,4 +134,16 @@ interface PetApi {
134134
): ResponseEntity<ModelApiResponse> {
135135
return ResponseEntity(HttpStatus.NOT_IMPLEMENTED)
136136
}
137+
138+
companion object {
139+
//for your own safety never directly reuse these path definitions in tests
140+
const val PATH_ADD_PET: String = "/pet"
141+
const val PATH_DELETE_PET: String = "/pet/{petId}"
142+
const val PATH_FIND_PETS_BY_STATUS: String = "/pet/findByStatus"
143+
const val PATH_FIND_PETS_BY_TAGS: String = "/pet/findByTags"
144+
const val PATH_GET_PET_BY_ID: String = "/pet/{petId}"
145+
const val PATH_UPDATE_PET: String = "/pet"
146+
const val PATH_UPDATE_PET_WITH_FORM: String = "/pet/{petId}"
147+
const val PATH_UPLOAD_FILE: String = "/pet/{petId}/uploadImage"
148+
}
137149
}

samples/server/petstore/kotlin-spring-cloud/src/main/kotlin/org/openapitools/api/StoreApi.kt

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ interface StoreApi {
3434

3535
@RequestMapping(
3636
method = [RequestMethod.DELETE],
37-
value = ["/store/order/{orderId}"]
37+
value = [PATH_DELETE_ORDER /* "/store/order/{orderId}" */]
3838
)
3939
fun deleteOrder(
4040
@PathVariable("orderId") orderId: kotlin.String
@@ -45,7 +45,7 @@ interface StoreApi {
4545

4646
@RequestMapping(
4747
method = [RequestMethod.GET],
48-
value = ["/store/inventory"],
48+
value = [PATH_GET_INVENTORY /* "/store/inventory" */],
4949
produces = ["application/json"]
5050
)
5151
fun getInventory(): ResponseEntity<Map<String, kotlin.Int>> {
@@ -55,7 +55,7 @@ interface StoreApi {
5555

5656
@RequestMapping(
5757
method = [RequestMethod.GET],
58-
value = ["/store/order/{orderId}"],
58+
value = [PATH_GET_ORDER_BY_ID /* "/store/order/{orderId}" */],
5959
produces = ["application/xml", "application/json"]
6060
)
6161
fun getOrderById(
@@ -67,7 +67,7 @@ interface StoreApi {
6767

6868
@RequestMapping(
6969
method = [RequestMethod.POST],
70-
value = ["/store/order"],
70+
value = [PATH_PLACE_ORDER /* "/store/order" */],
7171
produces = ["application/xml", "application/json"],
7272
consumes = ["application/json"]
7373
)
@@ -76,4 +76,12 @@ interface StoreApi {
7676
): ResponseEntity<Order> {
7777
return ResponseEntity(HttpStatus.NOT_IMPLEMENTED)
7878
}
79+
80+
companion object {
81+
//for your own safety never directly reuse these path definitions in tests
82+
const val PATH_DELETE_ORDER: String = "/store/order/{orderId}"
83+
const val PATH_GET_INVENTORY: String = "/store/inventory"
84+
const val PATH_GET_ORDER_BY_ID: String = "/store/order/{orderId}"
85+
const val PATH_PLACE_ORDER: String = "/store/order"
86+
}
7987
}

samples/server/petstore/kotlin-spring-cloud/src/main/kotlin/org/openapitools/api/UserApi.kt

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ interface UserApi {
3434

3535
@RequestMapping(
3636
method = [RequestMethod.POST],
37-
value = ["/user"],
37+
value = [PATH_CREATE_USER /* "/user" */],
3838
consumes = ["application/json"]
3939
)
4040
fun createUser(
@@ -46,7 +46,7 @@ interface UserApi {
4646

4747
@RequestMapping(
4848
method = [RequestMethod.POST],
49-
value = ["/user/createWithArray"],
49+
value = [PATH_CREATE_USERS_WITH_ARRAY_INPUT /* "/user/createWithArray" */],
5050
consumes = ["application/json"]
5151
)
5252
fun createUsersWithArrayInput(
@@ -58,7 +58,7 @@ interface UserApi {
5858

5959
@RequestMapping(
6060
method = [RequestMethod.POST],
61-
value = ["/user/createWithList"],
61+
value = [PATH_CREATE_USERS_WITH_LIST_INPUT /* "/user/createWithList" */],
6262
consumes = ["application/json"]
6363
)
6464
fun createUsersWithListInput(
@@ -70,7 +70,7 @@ interface UserApi {
7070

7171
@RequestMapping(
7272
method = [RequestMethod.DELETE],
73-
value = ["/user/{username}"]
73+
value = [PATH_DELETE_USER /* "/user/{username}" */]
7474
)
7575
fun deleteUser(
7676
@PathVariable("username") username: kotlin.String
@@ -81,7 +81,7 @@ interface UserApi {
8181

8282
@RequestMapping(
8383
method = [RequestMethod.GET],
84-
value = ["/user/{username}"],
84+
value = [PATH_GET_USER_BY_NAME /* "/user/{username}" */],
8585
produces = ["application/xml", "application/json"]
8686
)
8787
fun getUserByName(
@@ -93,7 +93,7 @@ interface UserApi {
9393

9494
@RequestMapping(
9595
method = [RequestMethod.GET],
96-
value = ["/user/login"],
96+
value = [PATH_LOGIN_USER /* "/user/login" */],
9797
produces = ["application/xml", "application/json"]
9898
)
9999
fun loginUser(
@@ -106,7 +106,7 @@ interface UserApi {
106106

107107
@RequestMapping(
108108
method = [RequestMethod.GET],
109-
value = ["/user/logout"]
109+
value = [PATH_LOGOUT_USER /* "/user/logout" */]
110110
)
111111
fun logoutUser(): ResponseEntity<Unit> {
112112
return ResponseEntity(HttpStatus.NOT_IMPLEMENTED)
@@ -115,7 +115,7 @@ interface UserApi {
115115

116116
@RequestMapping(
117117
method = [RequestMethod.PUT],
118-
value = ["/user/{username}"],
118+
value = [PATH_UPDATE_USER /* "/user/{username}" */],
119119
consumes = ["application/json"]
120120
)
121121
fun updateUser(
@@ -124,4 +124,16 @@ interface UserApi {
124124
): ResponseEntity<Unit> {
125125
return ResponseEntity(HttpStatus.NOT_IMPLEMENTED)
126126
}
127+
128+
companion object {
129+
//for your own safety never directly reuse these path definitions in tests
130+
const val PATH_CREATE_USER: String = "/user"
131+
const val PATH_CREATE_USERS_WITH_ARRAY_INPUT: String = "/user/createWithArray"
132+
const val PATH_CREATE_USERS_WITH_LIST_INPUT: String = "/user/createWithList"
133+
const val PATH_DELETE_USER: String = "/user/{username}"
134+
const val PATH_GET_USER_BY_NAME: String = "/user/{username}"
135+
const val PATH_LOGIN_USER: String = "/user/login"
136+
const val PATH_LOGOUT_USER: String = "/user/logout"
137+
const val PATH_UPDATE_USER: String = "/user/{username}"
138+
}
127139
}

0 commit comments

Comments
 (0)