Skip to content

Commit

Permalink
Fix incorrect header parameters generation for OpenFeignClient (#233)
Browse files Browse the repository at this point in the history
* Fix incorrect header parameters generation for OpenFeignClient

* Fix param names according to PR comments

* Fix tests

---------

Co-authored-by: tanoshkin <[email protected]>
  • Loading branch information
tanoshkin-cian and tanoshkin authored Sep 1, 2023
1 parent 81a003c commit e485ba5
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,9 @@ class OpenFeignInterfaceGenerator(
}

return if (headersValueParts.isNotEmpty()) {
val format = if (headersValueParts.size == 1) "%L" else "value = [%L]"
OpenFeignAnnotations.headersBuilder()
.addMember(format, headersValueParts.joinToString(", "))
.build()
val headersBuilder = OpenFeignAnnotations.headersBuilder()
headersValueParts.forEach { headersBuilder.addMember("%S", it) }
return headersBuilder.build()
} else {
null
}
Expand All @@ -241,15 +240,15 @@ class OpenFeignInterfaceGenerator(
"{${parameter.name}}"
}
return buildCodeBlock {
add("%S", "${parameter.originalName}: $parameterValue")
add("%L", "${parameter.originalName}: $parameterValue")
}.toString()
}

private fun getDefaultAcceptHeaderAnnotationValue(): String? {
return operation.getPrimaryContentMediaType()?.key?.let { mediaType ->
buildCodeBlock {
add(
"%S",
"%L",
"${ClientGeneratorUtils.ACCEPT_HEADER_NAME}: $mediaType",
)
}.toString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ interface ExamplePath1Client {
* @param acceptHeader
*/
@RequestLine("GET /example-path-1?explode_list_query_param={explodeListQueryParam}&query_param2={queryParam2}")
@Headers(value = ["Accept: {acceptHeader}", "Accept: application/vnd.custom.media+xml"])
@Headers(
"Accept: {acceptHeader}",
"Accept: application/vnd.custom.media+xml"
)
fun getExamplePath1(
@Param("explodeListQueryParam") explodeListQueryParam: List<String>? = null,
@Param("queryParam2") queryParam2: Int? = null,
Expand All @@ -43,7 +46,10 @@ interface ExamplePath2Client {
* @param accept the content type accepted by the client
*/
@RequestLine("GET /example-path-2?explode_list_query_param={explodeListQueryParam}&query_param2={queryParam2}")
@Headers(value = ["Accept: {accept}", "Accept: application/vnd.custom.media+xml"])
@Headers(
"Accept: {accept}",
"Accept: application/vnd.custom.media+xml"
)
fun getExamplePath2(
@Param("explodeListQueryParam") explodeListQueryParam: List<String>? = null,
@Param("queryParam2") queryParam2: Int? = null,
Expand All @@ -60,7 +66,10 @@ interface MultipleResponseSchemasClient {
* @param accept the content type accepted by the client
*/
@RequestLine("GET /multiple-response-schemas")
@Headers(value = ["Accept: {accept}", "Accept: application/json"])
@Headers(
"Accept: {accept}",
"Accept: application/json"
)
fun getMultipleResponseSchemas(
@Param("accept") accept: ContentType? = null,
@HeaderMap
Expand Down
15 changes: 15 additions & 0 deletions src/test/resources/examples/openFeignClient/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ paths:
parameters:
- $ref: "#/components/parameters/ListQueryParamExploded"
- $ref: "#/components/parameters/QueryParam2"
- $ref: "#/components/parameters/HeaderParam1"
- $ref: "#/components/parameters/HeaderParam2"
responses:
200:
description: "successful operation"
Expand Down Expand Up @@ -169,6 +171,19 @@ components:
schema:
type: boolean
required: false
HeaderParam1:
name: "header_param1"
in: "header"
schema:
type: string
required: false
HeaderParam2:
name: "header_param2"
in: "header"
schema:
type: string
required: false


headers:
Location:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,20 @@ interface ExamplePath1Client {
*
* @param explodeListQueryParam
* @param queryParam2
* @param headerParam1
* @param headerParam2
*/
@RequestLine("GET /example-path-1?explode_list_query_param={explodeListQueryParam}&query_param2={queryParam2}")
@Headers("Accept: application/vnd.custom.media+json")
@Headers(
"header_param1: {headerParam1}",
"header_param2: {headerParam2}",
"Accept: application/vnd.custom.media+json"
)
fun getExamplePath1(
@Param("explodeListQueryParam") explodeListQueryParam: List<String>? = null,
@Param("queryParam2") queryParam2: Int? = null,
@Param("headerParam1") headerParam1: String? = null,
@Param("headerParam2") headerParam2: String? = null,
@HeaderMap additionalHeaders: Map<String, String> = emptyMap()
): QueryResult

Expand Down Expand Up @@ -55,7 +63,10 @@ interface ExamplePath2Client {
* @param ifNoneMatch The RFC7232 If-None-Match header field
*/
@RequestLine("GET /example-path-2/{pathParam}?limit={limit}&query_param2={queryParam2}")
@Headers(value = ["If-None-Match: {ifNoneMatch}", "Accept: application/json"])
@Headers(
"If-None-Match: {ifNoneMatch}",
"Accept: application/json"
)
fun getExamplePath2PathParam(
@Param("pathParam") pathParam: String,
@Param("limit") limit: Int = 500,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,20 @@ interface ExamplePath1Client {
*
* @param explodeListQueryParam
* @param queryParam2
* @param headerParam1
* @param headerParam2
*/
@RequestLine("GET /example-path-1?explode_list_query_param={explodeListQueryParam}&query_param2={queryParam2}")
@Headers("Accept: application/vnd.custom.media+json")
@Headers(
"header_param1: {headerParam1}",
"header_param2: {headerParam2}",
"Accept: application/vnd.custom.media+json"
)
suspend fun getExamplePath1(
@Param("explodeListQueryParam") explodeListQueryParam: List<String>? = null,
@Param("queryParam2") queryParam2: Int? = null,
@Param("headerParam1") headerParam1: String? = null,
@Param("headerParam2") headerParam2: String? = null,
@HeaderMap additionalHeaders: Map<String, String> = emptyMap()
): QueryResult

Expand Down Expand Up @@ -55,7 +63,10 @@ interface ExamplePath2Client {
* @param ifNoneMatch The RFC7232 If-None-Match header field
*/
@RequestLine("GET /example-path-2/{pathParam}?limit={limit}&query_param2={queryParam2}")
@Headers(value = ["If-None-Match: {ifNoneMatch}", "Accept: application/json"])
@Headers(
"If-None-Match: {ifNoneMatch}",
"Accept: application/json"
)
suspend fun getExamplePath2PathParam(
@Param("pathParam") pathParam: String,
@Param("limit") limit: Int = 500,
Expand Down

0 comments on commit e485ba5

Please sign in to comment.