Skip to content

Commit

Permalink
Merge pull request #121 from Yelp/cortinico/fix-120-headerToRemove-on…
Browse files Browse the repository at this point in the history
…-top-level

HeadersToRemove should apply also to top level headers
  • Loading branch information
macisamuele committed Feb 21, 2020
2 parents 5901101 + 7e9b7a6 commit d073b62
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -431,13 +431,16 @@ open class KotlinGenerator : SharedCodegen() {
}
}

codegenOperation.imports.add("retrofit2.http.Headers")

// Let's remove the leading
if (!basePath.isNullOrBlank()) {
codegenOperation.path = codegenOperation.path.removePrefix("/")
}

// Let's process the top level Headers and eventually add the retrofit annotations.
processTopLevelHeaders(codegenOperation)
if (codegenOperation.vendorExtensions[HAS_OPERATION_HEADERS] == true) {
codegenOperation.imports.add("retrofit2.http.Headers")
}

// Let's make sure we import all the types, also the inner ones (see #76).
codegenOperation.responses.forEach {
Expand Down Expand Up @@ -508,6 +511,10 @@ open class KotlinGenerator : SharedCodegen() {
}
}

// Process the
val headersToIgnore = getHeadersToIgnore()
topLevelHeaders.removeIf { it.first in headersToIgnore }

operation.vendorExtensions[HAS_OPERATION_HEADERS] = topLevelHeaders.isNotEmpty()
operation.vendorExtensions[OPERATION_HEADERS] = topLevelHeaders
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,21 @@ class KotlinGeneratorTest {
assertEquals(testOperationId, firstPair.second as String)
}

@Test
fun processTopLevelHeaders_withOperationIdAndHeadersToIgnore_hasNoHeaders() {
val testOperationId = "aTestOperationId"
val generator = KotlinGenerator()
val operation = CodegenOperation()
generator.additionalProperties()[HEADERS_TO_IGNORE] = HEADER_X_OPERATION_ID
operation.vendorExtensions = mutableMapOf(X_OPERATION_ID to (testOperationId as Any))

generator.processTopLevelHeaders(operation)

assertEquals(false, operation.vendorExtensions["hasOperationHeaders"])
val headerMap = operation.vendorExtensions["operationHeaders"] as List<*>
assertEquals(0, headerMap.size)
}

@Test
fun processTopLevelHeaders_withConsumes_hasContentTypeHeader() {
val generator = KotlinGenerator()
Expand All @@ -398,6 +413,23 @@ class KotlinGeneratorTest {
assertEquals("application/json", firstPair.second as String)
}

@Test
fun processTopLevelHeaders_withConsumesAndHeadersToIgnore_hasNoContentTypeHeader() {
val generator = KotlinGenerator()
val operation = CodegenOperation()
generator.additionalProperties()[HEADERS_TO_IGNORE] = HEADER_CONTENT_TYPE
operation.vendorExtensions = mutableMapOf()
operation.consumes = listOf(
mapOf("mediaType" to "application/json")
)

generator.processTopLevelHeaders(operation)

assertEquals(false, operation.vendorExtensions["hasOperationHeaders"])
val headerMap = operation.vendorExtensions["operationHeaders"] as List<*>
assertEquals(0, headerMap.size)
}

@Test
fun processTopLevelHeaders_withFormParams_hasNoContentTypeHeader() {
val generator = KotlinGenerator()
Expand Down

0 comments on commit d073b62

Please sign in to comment.