Skip to content

Commit

Permalink
Merge pull request #50 from Yelp/fix-49-trailing-slash
Browse files Browse the repository at this point in the history
Remove leading slash if Swagger Spec are specifying a basePath
  • Loading branch information
cortinico authored Jul 25, 2019
2 parents a3da180 + 4491f6c commit 08975a0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
7 changes: 7 additions & 0 deletions plugin/src/main/java/com/yelp/codegen/KotlinGenerator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class KotlinGenerator : SharedCodegen() {

private val apiDocPath = "docs/"
private val modelDocPath = "docs/"
internal var basePath: String? = null

private val retrofitImport = mapOf(
"GET" to "retrofit2.http.GET",
Expand Down Expand Up @@ -407,6 +408,11 @@ class KotlinGenerator : SharedCodegen() {
getHeadersToIgnore().forEach { headerName ->
ignoreHeaderParameter(headerName, codegenOperation)
}

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

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

// Override the swagger version with the one provided from command line.
swagger.info.version = additionalProperties[SPEC_VERSION] as String
this.basePath = swagger.basePath
}

/**
Expand Down
27 changes: 27 additions & 0 deletions plugin/src/test/java/com/yelp/codegen/KotlinGeneratorTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.yelp.codegen
import io.swagger.codegen.CodegenModel
import io.swagger.codegen.CodegenProperty
import io.swagger.models.Info
import io.swagger.models.Operation
import io.swagger.models.Swagger
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
Expand Down Expand Up @@ -314,6 +315,30 @@ class KotlinGeneratorTest {
assertEquals("typeKey", KotlinGenerator().removeNonNameElementToCamelCase("[type]key"))
}

@Test
fun fromOperation_withBasePath_removeLeadingSlash() {
val generator = KotlinGenerator()
generator.basePath = "/v2"
val operation = Operation()
val swagger = Swagger()

val codegenOperation = generator.fromOperation("/helloworld", "GET", operation, mutableMapOf(), swagger)

assertEquals("helloworld", codegenOperation.path)
}

@Test
fun fromOperation_withNoBasePath_leadingSlashIsNotRemoved() {
val generator = KotlinGenerator()
generator.basePath = null
val operation = Operation()
val swagger = Swagger()

val codegenOperation = generator.fromOperation("/helloworld", "GET", operation, mutableMapOf(), swagger)

assertEquals("/helloworld", codegenOperation.path)
}

@Test
fun preprocessSwagger() {
val generator = KotlinGenerator()
Expand All @@ -322,8 +347,10 @@ class KotlinGeneratorTest {
val swagger = Swagger()
swagger.info = Info()
swagger.info.version = "1.0.0"
swagger.basePath = "/v2"
generator.preprocessSwagger(swagger)

assertEquals("42.0.0", swagger.info.version)
assertEquals("/v2", generator.basePath)
}
}

0 comments on commit 08975a0

Please sign in to comment.