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

HeadersToRemove should apply also to top level headers #121

Merged
merged 1 commit into from
Feb 21, 2020
Merged
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 @@ -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