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

Remove leading slash if Swagger Spec are specifying a basePath #50

Merged
merged 2 commits into from
Jul 25, 2019
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
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()) {
cortinico marked this conversation as resolved.
Show resolved Hide resolved
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)
}
}