Skip to content

Commit e607915

Browse files
authored
Merge pull request #32 from Yelp/fix-31-brackets-in-param-names
Fix for square brackets in the operation name
2 parents cf332b2 + d5b95f1 commit e607915

File tree

19 files changed

+660
-4
lines changed

19 files changed

+660
-4
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ You can find some **examples** in this repository to help you set up your genera
7575

7676
* [samples/kotlin-android](/samples/kotlin-android) Contains an example of an Android Library configured with a `build.gradle.kts` file, using Kotlin as scripting language.
7777

78+
* [samples/junit-tests](/samples/junit-tests) This sample contains specs used to test edge cases and scenarios that have been reported in the issue tracker or that are worth testing.
79+
7880
## How the generated code will look like
7981

8082
[Here](/SAMPLES.md) you can find some examples of how the generated code will look like in your project.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
@file:Suppress("Unused")
22

33
object PublishingVersions {
4-
const val PLUGIN_VERSION = "1.0.0"
4+
const val PLUGIN_VERSION = "1.1.0-SNAPSHOT"
55
const val PLUGIN_GROUP = "com.yelp.codegen"
66
const val PLUGIN_ARTIFACT = "plugin"
77
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,4 +425,15 @@ class KotlinGenerator : SharedCodegen() {
425425
// Override the swagger version with the one provided from command line.
426426
swagger.info.version = additionalProperties[SPEC_VERSION] as String
427427
}
428+
429+
/**
430+
* Function used to sanitize the name for operation generations.
431+
* The superclass is not providing the correct string pattern (See #31).
432+
*
433+
* Here we override the provided pattern to include also square brackets in during the
434+
* parameter name generation.
435+
*/
436+
override fun removeNonNameElementToCamelCase(name: String?): String {
437+
return super.removeNonNameElementToCamelCase(name, "[-_:;#\\[\\]]")
438+
}
428439
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,14 @@ class KotlinGeneratorTest {
1111
assert(KotlinGenerator().toModelName("model with dot.s") == "ModelWithDotS")
1212
assert(KotlinGenerator().toModelName("model with userscore_s") == "ModelWithUserscoreS")
1313
}
14+
15+
@Test
16+
fun removeNonNameElementToCamelCase_withSquareBrackets() {
17+
assert(KotlinGenerator().removeNonNameElementToCamelCase("type[]") == "type")
18+
assert(KotlinGenerator().removeNonNameElementToCamelCase("type[value]") == "typeValue")
19+
assert(KotlinGenerator().removeNonNameElementToCamelCase("type[") == "type")
20+
assert(KotlinGenerator().removeNonNameElementToCamelCase("type]") == "type")
21+
assert(KotlinGenerator().removeNonNameElementToCamelCase("[type]") == "type")
22+
assert(KotlinGenerator().removeNonNameElementToCamelCase("[type]key") == "typeKey")
23+
}
1424
}

samples/junit-tests/build.gradle

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
buildscript {
2+
repositories {
3+
mavenLocal()
4+
gradlePluginPortal()
5+
google()
6+
mavenCentral()
7+
jcenter()
8+
}
9+
10+
dependencies {
11+
classpath "com.android.tools.build:gradle:3.2.1"
12+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.21"
13+
classpath "com.yelp.codegen:plugin:1.1.0-SNAPSHOT"
14+
}
15+
}
16+
17+
apply plugin: "com.android.library"
18+
apply plugin: "kotlin-android"
19+
apply plugin: "com.yelp.codegen.plugin"
20+
21+
android {
22+
compileSdkVersion = 28
23+
defaultConfig {
24+
minSdkVersion 21
25+
targetSdkVersion 28
26+
versionCode = 1
27+
versionName = "1.0"
28+
}
29+
}
30+
31+
dependencies {
32+
// Kotlin
33+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.21"
34+
implementation "org.jetbrains.kotlin:kotlin-reflect:1.3.21"
35+
36+
// Moshi
37+
implementation "com.squareup.moshi:moshi:1.8.0"
38+
implementation "com.squareup.moshi:moshi-adapters:1.8.0"
39+
implementation "com.squareup.moshi:moshi-kotlin:1.8.0"
40+
implementation "com.squareup.retrofit2:converter-moshi:2.5.0"
41+
implementation "com.squareup.retrofit2:adapter-rxjava2:2.5.0"
42+
43+
// Date Support
44+
implementation "com.jakewharton.threetenabp:threetenabp:1.1.1"
45+
46+
// RxJava
47+
implementation "io.reactivex.rxjava2:rxjava:2.2.4"
48+
implementation "io.reactivex.rxjava2:rxandroid:2.1.0"
49+
50+
// Testing Dependencies
51+
testImplementation "junit:junit:4.12"
52+
testImplementation "com.squareup.okhttp3:mockwebserver:3.12.3"
53+
}
54+
55+
generateSwagger {
56+
platform = "kotlin"
57+
packageName = "com.yelp.codegen.generatecodesamples"
58+
specName = "junittests"
59+
inputFile = file("./junit_tests_specs.json")
60+
outputDir = file("./src/main/java/")
61+
}
62+
63+
repositories {
64+
mavenCentral()
65+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
{
2+
"definitions": {},
3+
"info": {
4+
"description": "This spec is used to have JUnit Tests to check the generated code.",
5+
"title": "JUnit Tests",
6+
"version": "1.0.0"
7+
},
8+
"paths": {
9+
"/brackets/in/parameter/name": {
10+
"get": {
11+
"description": "Make sure that brackets in parameter name are treated properly",
12+
"operationId": "getBracketsInParameterName",
13+
"parameters": [
14+
{
15+
"in": "query",
16+
"name": "page",
17+
"required": false,
18+
"type": "string"
19+
},
20+
{
21+
"in": "query",
22+
"name": "page[]",
23+
"required": false,
24+
"type": "string"
25+
},
26+
{
27+
"in": "query",
28+
"name": "datePosted[before]",
29+
"required": false,
30+
"type": "string"
31+
},
32+
{
33+
"in": "query",
34+
"name": "datePosted[strictly_before]",
35+
"required": false,
36+
"type": "string"
37+
},
38+
{
39+
"in": "query",
40+
"name": "datePosted[after]",
41+
"required": false,
42+
"type": "string"
43+
},
44+
{
45+
"in": "query",
46+
"name": "datePosted[strictly_after]",
47+
"required": false,
48+
"type": "string"
49+
}
50+
],
51+
"responses": {
52+
"200": {
53+
"description": "successful operation"
54+
}
55+
},
56+
"summary": "Test brackets in parameter name"
57+
}
58+
}
59+
},
60+
"swagger": "2.0"
61+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2+
package="com.yelp.samplelibrary"/>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* NOTE: This class is auto generated by the Swagger Gradle Codegen for the following API: JUnit Tests
3+
*
4+
* More info on this tool is available on https://github.com/Yelp/swagger-gradle-codegen
5+
*/
6+
7+
package com.yelp.codegen.generatecodesamples.apis
8+
9+
import io.reactivex.Completable
10+
import retrofit2.http.GET
11+
import retrofit2.http.Headers
12+
13+
@JvmSuppressWildcards
14+
interface DefaultApi {
15+
/**
16+
* Test brackets in parameter name
17+
* Make sure that brackets in parameter name are treated properly
18+
* The endpoint is owned by junittests service owner
19+
* @param page (optional)
20+
* @param page2 (optional)
21+
* @param datePostedBefore (optional)
22+
* @param datePostedStrictlyBefore (optional)
23+
* @param datePostedAfter (optional)
24+
* @param datePostedStrictlyAfter (optional)
25+
*/
26+
@Headers(
27+
"X-Operation-ID: getBracketsInParameterName"
28+
)
29+
30+
@GET("/brackets/in/parameter/name")
31+
fun getBracketsInParameterName(
32+
@retrofit2.http.Query("page") page: String?,
33+
@retrofit2.http.Query("page[]") page2: String?,
34+
@retrofit2.http.Query("datePosted[before]") datePostedBefore: String?,
35+
@retrofit2.http.Query("datePosted[strictly_before]") datePostedStrictlyBefore: String?,
36+
@retrofit2.http.Query("datePosted[after]") datePostedAfter: String?,
37+
@retrofit2.http.Query("datePosted[strictly_after]") datePostedStrictlyAfter: String?
38+
): Completable
39+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.yelp.codegen.generatecodesamples.tools
2+
3+
import retrofit2.Converter
4+
import retrofit2.Retrofit
5+
import java.lang.reflect.Type
6+
7+
internal class CollectionFormatConverterFactory : Converter.Factory() {
8+
9+
override fun stringConverter(type: Type, annotations: Array<out Annotation>, retrofit: Retrofit): Converter<*, String>? {
10+
val rawType = getRawType(type)
11+
if (rawType == String::class.java || rawType == List::class.java)
12+
annotations.forEach {
13+
when (it) {
14+
is CSV -> return CollectionFormatConverter(",")
15+
is SSV -> return CollectionFormatConverter(" ")
16+
is TSV -> return CollectionFormatConverter("\t")
17+
is PIPES -> return CollectionFormatConverter("|")
18+
}
19+
}
20+
return null
21+
}
22+
23+
private class CollectionFormatConverter(private val separator: String) : Converter<Any, String> {
24+
override fun convert(value: Any): String {
25+
when (value) {
26+
is String -> return value
27+
is List<*> -> return value.joinToString(separator)
28+
}
29+
throw RuntimeException("Unsupported type")
30+
}
31+
}
32+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.yelp.codegen.generatecodesamples.tools
2+
3+
@Target(AnnotationTarget.FIELD, AnnotationTarget.VALUE_PARAMETER)
4+
@Retention(AnnotationRetention.RUNTIME)
5+
annotation class CSV
6+
7+
@Target(AnnotationTarget.FIELD, AnnotationTarget.VALUE_PARAMETER)
8+
@Retention(AnnotationRetention.RUNTIME)
9+
annotation class SSV
10+
11+
@Target(AnnotationTarget.FIELD, AnnotationTarget.VALUE_PARAMETER)
12+
@Retention(AnnotationRetention.RUNTIME)
13+
annotation class TSV
14+
15+
@Target(AnnotationTarget.FIELD, AnnotationTarget.VALUE_PARAMETER)
16+
@Retention(AnnotationRetention.RUNTIME)
17+
annotation class PIPES

0 commit comments

Comments
 (0)