Skip to content

Commit 08975a0

Browse files
authored
Merge pull request #50 from Yelp/fix-49-trailing-slash
Remove leading slash if Swagger Spec are specifying a basePath
2 parents a3da180 + 4491f6c commit 08975a0

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class KotlinGenerator : SharedCodegen() {
3333

3434
private val apiDocPath = "docs/"
3535
private val modelDocPath = "docs/"
36+
internal var basePath: String? = null
3637

3738
private val retrofitImport = mapOf(
3839
"GET" to "retrofit2.http.GET",
@@ -407,6 +408,11 @@ class KotlinGenerator : SharedCodegen() {
407408
getHeadersToIgnore().forEach { headerName ->
408409
ignoreHeaderParameter(headerName, codegenOperation)
409410
}
411+
412+
// Let's remove the leading
413+
if (!basePath.isNullOrBlank()) {
414+
codegenOperation.path = codegenOperation.path.removePrefix("/")
415+
}
410416
return codegenOperation
411417
}
412418

@@ -432,6 +438,7 @@ class KotlinGenerator : SharedCodegen() {
432438

433439
// Override the swagger version with the one provided from command line.
434440
swagger.info.version = additionalProperties[SPEC_VERSION] as String
441+
this.basePath = swagger.basePath
435442
}
436443

437444
/**

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.yelp.codegen
33
import io.swagger.codegen.CodegenModel
44
import io.swagger.codegen.CodegenProperty
55
import io.swagger.models.Info
6+
import io.swagger.models.Operation
67
import io.swagger.models.Swagger
78
import org.junit.Assert.assertEquals
89
import org.junit.Assert.assertFalse
@@ -314,6 +315,30 @@ class KotlinGeneratorTest {
314315
assertEquals("typeKey", KotlinGenerator().removeNonNameElementToCamelCase("[type]key"))
315316
}
316317

318+
@Test
319+
fun fromOperation_withBasePath_removeLeadingSlash() {
320+
val generator = KotlinGenerator()
321+
generator.basePath = "/v2"
322+
val operation = Operation()
323+
val swagger = Swagger()
324+
325+
val codegenOperation = generator.fromOperation("/helloworld", "GET", operation, mutableMapOf(), swagger)
326+
327+
assertEquals("helloworld", codegenOperation.path)
328+
}
329+
330+
@Test
331+
fun fromOperation_withNoBasePath_leadingSlashIsNotRemoved() {
332+
val generator = KotlinGenerator()
333+
generator.basePath = null
334+
val operation = Operation()
335+
val swagger = Swagger()
336+
337+
val codegenOperation = generator.fromOperation("/helloworld", "GET", operation, mutableMapOf(), swagger)
338+
339+
assertEquals("/helloworld", codegenOperation.path)
340+
}
341+
317342
@Test
318343
fun preprocessSwagger() {
319344
val generator = KotlinGenerator()
@@ -322,8 +347,10 @@ class KotlinGeneratorTest {
322347
val swagger = Swagger()
323348
swagger.info = Info()
324349
swagger.info.version = "1.0.0"
350+
swagger.basePath = "/v2"
325351
generator.preprocessSwagger(swagger)
326352

327353
assertEquals("42.0.0", swagger.info.version)
354+
assertEquals("/v2", generator.basePath)
328355
}
329356
}

0 commit comments

Comments
 (0)