Skip to content

Commit d073b62

Browse files
authored
Merge pull request #121 from Yelp/cortinico/fix-120-headerToRemove-on-top-level
HeadersToRemove should apply also to top level headers
2 parents 5901101 + 7e9b7a6 commit d073b62

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

gradle-plugin/plugin/src/main/java/com/yelp/codegen/KotlinGenerator.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,13 +431,16 @@ open class KotlinGenerator : SharedCodegen() {
431431
}
432432
}
433433

434-
codegenOperation.imports.add("retrofit2.http.Headers")
435-
436434
// Let's remove the leading
437435
if (!basePath.isNullOrBlank()) {
438436
codegenOperation.path = codegenOperation.path.removePrefix("/")
439437
}
438+
439+
// Let's process the top level Headers and eventually add the retrofit annotations.
440440
processTopLevelHeaders(codegenOperation)
441+
if (codegenOperation.vendorExtensions[HAS_OPERATION_HEADERS] == true) {
442+
codegenOperation.imports.add("retrofit2.http.Headers")
443+
}
441444

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

514+
// Process the
515+
val headersToIgnore = getHeadersToIgnore()
516+
topLevelHeaders.removeIf { it.first in headersToIgnore }
517+
511518
operation.vendorExtensions[HAS_OPERATION_HEADERS] = topLevelHeaders.isNotEmpty()
512519
operation.vendorExtensions[OPERATION_HEADERS] = topLevelHeaders
513520
}

gradle-plugin/plugin/src/test/java/com/yelp/codegen/KotlinGeneratorTest.kt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,21 @@ class KotlinGeneratorTest {
379379
assertEquals(testOperationId, firstPair.second as String)
380380
}
381381

382+
@Test
383+
fun processTopLevelHeaders_withOperationIdAndHeadersToIgnore_hasNoHeaders() {
384+
val testOperationId = "aTestOperationId"
385+
val generator = KotlinGenerator()
386+
val operation = CodegenOperation()
387+
generator.additionalProperties()[HEADERS_TO_IGNORE] = HEADER_X_OPERATION_ID
388+
operation.vendorExtensions = mutableMapOf(X_OPERATION_ID to (testOperationId as Any))
389+
390+
generator.processTopLevelHeaders(operation)
391+
392+
assertEquals(false, operation.vendorExtensions["hasOperationHeaders"])
393+
val headerMap = operation.vendorExtensions["operationHeaders"] as List<*>
394+
assertEquals(0, headerMap.size)
395+
}
396+
382397
@Test
383398
fun processTopLevelHeaders_withConsumes_hasContentTypeHeader() {
384399
val generator = KotlinGenerator()
@@ -398,6 +413,23 @@ class KotlinGeneratorTest {
398413
assertEquals("application/json", firstPair.second as String)
399414
}
400415

416+
@Test
417+
fun processTopLevelHeaders_withConsumesAndHeadersToIgnore_hasNoContentTypeHeader() {
418+
val generator = KotlinGenerator()
419+
val operation = CodegenOperation()
420+
generator.additionalProperties()[HEADERS_TO_IGNORE] = HEADER_CONTENT_TYPE
421+
operation.vendorExtensions = mutableMapOf()
422+
operation.consumes = listOf(
423+
mapOf("mediaType" to "application/json")
424+
)
425+
426+
generator.processTopLevelHeaders(operation)
427+
428+
assertEquals(false, operation.vendorExtensions["hasOperationHeaders"])
429+
val headerMap = operation.vendorExtensions["operationHeaders"] as List<*>
430+
assertEquals(0, headerMap.size)
431+
}
432+
401433
@Test
402434
fun processTopLevelHeaders_withFormParams_hasNoContentTypeHeader() {
403435
val generator = KotlinGenerator()

0 commit comments

Comments
 (0)