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

Allow Code-geneated Moshi Adapters #82

Merged
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ java_pid*.hprof
local.properties
out/
venv/

# Samples folders -> content is generated by running ./gradlew generateSwagger
samples/kotlin-android-with-moshi-kotlin-codegen/src/main/java/
samples/kotlin-coroutines/src/main/java/
macisamuele marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions plugin/src/main/java/com/yelp/codegen/KotlinGenerator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ open class KotlinGenerator : SharedCodegen() {
// If there are any vars, we will mark them with the @Json annotation so we have to make sure to import it
if (codegenModel.vars.isNotEmpty() || codegenModel.isEnum) {
codegenModel.imports.add("com.squareup.moshi.Json")
codegenModel.imports.add("com.squareup.moshi.JsonClass")
}

// Add import for @XNullable annotation if there are any XNullable properties
Expand Down
4 changes: 3 additions & 1 deletion plugin/src/main/resources/kotlin/data_class.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
* @property {{{name}}} {{description}}
{{/vars}}
*/
{{#hasVars}}data {{/hasVars}}class {{classname}} {{#hasVars}}(
{{#hasVars}}@JsonClass(generateAdapter = true)
data {{/hasVars}}class {{classname}} {{#hasVars}}(
{{#requiredVars}}
{{>data_class_req_var}}{{^-last}},
{{/-last}}{{/requiredVars}}{{#hasRequired}}{{#hasOptional}},
Expand All @@ -17,6 +18,7 @@
* {{{description}}}
* Values: {{#allowableValues}}{{#enumVars}}{{&name}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}}
*/
@JsonClass(generateAdapter = false)
enum class {{enumName}}(val value: {{complexType}}){ {{#allowableValues}}{{#enumVars}}
@Json(name = {{{value}}}) {{&name}}({{{value}}}){{^-last}},{{/-last}}{{#-last}}{{/-last}}{{/enumVars}}{{/allowableValues}}
}
Expand Down
1 change: 1 addition & 0 deletions plugin/src/main/resources/kotlin/enum_class.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* {{{description}}}
* Values: {{#allowableValues}}{{#enumVars}}{{&name}}{{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}}
*/
@JsonClass(generateAdapter = false)
enum class {{classname}}(val value: {{dataType}}){
{{#allowableValues}}{{#enumVars}} @Json(name = {{{value}}}) {{&name}}({{{value}}}){{^-last}},{{/-last}}
{{/enumVars}}{{/allowableValues}}}
2 changes: 2 additions & 0 deletions samples/junit-tests/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ apply plugin: "com.android.library"
apply plugin: "kotlin-android"
apply plugin: "com.yelp.codegen.plugin"
apply plugin: "io.gitlab.arturbosch.detekt"
apply plugin: "kotlin-kapt"

android {
compileSdkVersion = 28
Expand All @@ -43,6 +44,7 @@ dependencies {
implementation "com.squareup.retrofit2:retrofit:2.7.1"
implementation "com.squareup.retrofit2:converter-moshi:2.7.1"
implementation "com.squareup.retrofit2:adapter-rxjava2:2.7.1"
kapt "com.squareup.moshi:moshi-kotlin-codegen:1.9.2"

// Date Support
implementation "com.jakewharton.threetenabp:threetenabp:1.2.2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package com.yelp.codegen.generatecodesamples.models

import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import org.threeten.bp.LocalDate
import org.threeten.bp.ZonedDateTime

Expand All @@ -16,6 +17,7 @@ import org.threeten.bp.ZonedDateTime
* @property datetimeProperty
* @property enumProperty
*/
@JsonClass(generateAdapter = true)
data class FormatResponses(
@Json(name = "date_property") @field:Json(name = "date_property") var dateProperty: LocalDate? = null,
@Json(name = "datetime_property") @field:Json(name = "datetime_property") var datetimeProperty: ZonedDateTime? = null,
Expand All @@ -25,6 +27,7 @@ data class FormatResponses(
*
* Values: VALUE1, VALUE2
*/
@JsonClass(generateAdapter = false)
enum class EnumPropertyEnum(val value: String) {
@Json(name = "VALUE1") VALUE1("VALUE1"),
@Json(name = "VALUE2") VALUE2("VALUE2")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
package com.yelp.codegen.generatecodesamples.models

import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import java.math.BigDecimal

/**
*
* @property numberArray
* @property stringArray
*/
@JsonClass(generateAdapter = true)
data class PropertyArray(
@Json(name = "number_array") @field:Json(name = "number_array") var numberArray: List<BigDecimal>? = null,
@Json(name = "string_array") @field:Json(name = "string_array") var stringArray: List<String>? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package com.yelp.codegen.generatecodesamples.models

import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import java.math.BigDecimal

/**
Expand All @@ -15,6 +16,7 @@ import java.math.BigDecimal
* @property objectMap
* @property stringMap
*/
@JsonClass(generateAdapter = true)
data class PropertyMap(
@Json(name = "number_map") @field:Json(name = "number_map") var numberMap: Map<String, BigDecimal>? = null,
@Json(name = "object_map") @field:Json(name = "object_map") var objectMap: Map<String, Any?>? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package com.yelp.codegen.generatecodesamples.models

import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import java.math.BigDecimal

/**
Expand All @@ -17,6 +18,7 @@ import java.math.BigDecimal
* @property numberProperty
* @property stringProperty
*/
@JsonClass(generateAdapter = true)
data class RequiredTypeResponses(
@Json(name = "boolean_property") @field:Json(name = "boolean_property") var booleanProperty: Boolean,
@Json(name = "enum_property") @field:Json(name = "enum_property") var enumProperty: RequiredTypeResponses.EnumPropertyEnum,
Expand All @@ -28,6 +30,7 @@ data class RequiredTypeResponses(
*
* Values: VALUE1, VALUE2
*/
@JsonClass(generateAdapter = false)
enum class EnumPropertyEnum(val value: String) {
@Json(name = "VALUE1") VALUE1("VALUE1"),
@Json(name = "VALUE2") VALUE2("VALUE2")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package com.yelp.codegen.generatecodesamples.models

import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass

/**
*
Expand All @@ -17,6 +18,7 @@ import com.squareup.moshi.Json
* @property `var`
* @property `when`
*/
@JsonClass(generateAdapter = true)
data class ReservedKeywords(
@Json(name = "class") @field:Json(name = "class") var `class`: String? = null,
@Json(name = "for") @field:Json(name = "for") var `for`: String? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
package com.yelp.codegen.generatecodesamples.models

import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass

/**
*
* Values: VALUE1,VALUE2
*/
@JsonClass(generateAdapter = false)
enum class TopLevelEnum(val value: String) {
@Json(name = "TOP_LEVEL_VALUE1") VALUE1("TOP_LEVEL_VALUE1"),
@Json(name = "TOP_LEVEL_VALUE2") VALUE2("TOP_LEVEL_VALUE2")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package com.yelp.codegen.generatecodesamples.models

import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import java.math.BigDecimal

/**
Expand All @@ -16,6 +17,7 @@ import java.math.BigDecimal
* @property numberProperty
* @property stringProperty
*/
@JsonClass(generateAdapter = true)
data class TypeResponses(
@Json(name = "boolean_property") @field:Json(name = "boolean_property") var booleanProperty: Boolean? = null,
@Json(name = "integer_property") @field:Json(name = "integer_property") var integerProperty: Int? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package com.yelp.codegen.generatecodesamples.models

import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import com.yelp.codegen.generatecodesamples.tools.XNullable
import org.threeten.bp.LocalDate
import org.threeten.bp.ZonedDateTime
Expand All @@ -17,6 +18,7 @@ import org.threeten.bp.ZonedDateTime
* @property datetimeProperty
* @property doubleProperty
*/
@JsonClass(generateAdapter = true)
data class XnullableFormatResponses(
@Json(name = "date_property") @field:Json(name = "date_property") @XNullable var dateProperty: LocalDate? = null,
@Json(name = "datetime_property") @field:Json(name = "datetime_property") @XNullable var datetimeProperty: ZonedDateTime? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package com.yelp.codegen.generatecodesamples.models

import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import com.yelp.codegen.generatecodesamples.tools.XNullable
import java.math.BigDecimal

Expand All @@ -15,6 +16,7 @@ import java.math.BigDecimal
* @property numberArray
* @property stringArray
*/
@JsonClass(generateAdapter = true)
data class XnullablePropertyArray(
@Json(name = "number_array") @field:Json(name = "number_array") @XNullable var numberArray: List<BigDecimal?>? = null,
@Json(name = "string_array") @field:Json(name = "string_array") @XNullable var stringArray: List<String?>? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package com.yelp.codegen.generatecodesamples.models

import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import com.yelp.codegen.generatecodesamples.tools.XNullable
import java.math.BigDecimal

Expand All @@ -16,6 +17,7 @@ import java.math.BigDecimal
* @property objectMap
* @property stringMap
*/
@JsonClass(generateAdapter = true)
data class XnullablePropertyMap(
@Json(name = "number_map") @field:Json(name = "number_map") @XNullable var numberMap: Map<String, BigDecimal?>? = null,
@Json(name = "object_map") @field:Json(name = "object_map") @XNullable var objectMap: Map<String, Any?>? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package com.yelp.codegen.generatecodesamples.models

import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import com.yelp.codegen.generatecodesamples.tools.XNullable
import java.math.BigDecimal

Expand All @@ -15,6 +16,7 @@ import java.math.BigDecimal
* @property numberArray
* @property stringArray
*/
@JsonClass(generateAdapter = true)
data class XnullableRequiredPropertyArray(
@Json(name = "number_array") @field:Json(name = "number_array") @XNullable var numberArray: List<BigDecimal?>? = null,
@Json(name = "string_array") @field:Json(name = "string_array") @XNullable var stringArray: List<String?>? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package com.yelp.codegen.generatecodesamples.models

import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import com.yelp.codegen.generatecodesamples.tools.XNullable
import java.math.BigDecimal

Expand All @@ -16,6 +17,7 @@ import java.math.BigDecimal
* @property objectMap
* @property stringMap
*/
@JsonClass(generateAdapter = true)
data class XnullableRequiredPropertyMap(
@Json(name = "number_map") @field:Json(name = "number_map") @XNullable var numberMap: Map<String, BigDecimal?>? = null,
@Json(name = "object_map") @field:Json(name = "object_map") @XNullable var objectMap: Map<String, Any?>? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package com.yelp.codegen.generatecodesamples.models

import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import com.yelp.codegen.generatecodesamples.tools.XNullable
import java.math.BigDecimal

Expand All @@ -18,6 +19,7 @@ import java.math.BigDecimal
* @property numberProperty
* @property stringProperty
*/
@JsonClass(generateAdapter = true)
data class XnullableRequiredTypeResponses(
@Json(name = "boolean_property") @field:Json(name = "boolean_property") @XNullable var booleanProperty: Boolean? = null,
@Json(name = "enum_property") @field:Json(name = "enum_property") @XNullable var enumProperty: XnullableRequiredTypeResponses.EnumPropertyEnum? = null,
Expand All @@ -29,6 +31,7 @@ data class XnullableRequiredTypeResponses(
*
* Values: VALUE1, VALUE2
*/
@JsonClass(generateAdapter = false)
enum class EnumPropertyEnum(val value: String) {
@Json(name = "VALUE1") VALUE1("VALUE1"),
@Json(name = "VALUE2") VALUE2("VALUE2")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package com.yelp.codegen.generatecodesamples.models

import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import com.yelp.codegen.generatecodesamples.tools.XNullable
import java.math.BigDecimal

Expand All @@ -18,6 +19,7 @@ import java.math.BigDecimal
* @property numberProperty
* @property stringProperty
*/
@JsonClass(generateAdapter = true)
data class XnullableTypeResponses(
@Json(name = "boolean_property") @field:Json(name = "boolean_property") @XNullable var booleanProperty: Boolean? = null,
@Json(name = "enum_property") @field:Json(name = "enum_property") @XNullable var enumProperty: XnullableTypeResponses.EnumPropertyEnum? = null,
Expand All @@ -29,6 +31,7 @@ data class XnullableTypeResponses(
*
* Values: VALUE1, VALUE2
*/
@JsonClass(generateAdapter = false)
enum class EnumPropertyEnum(val value: String) {
@Json(name = "VALUE1") VALUE1("VALUE1"),
@Json(name = "VALUE2") VALUE2("VALUE2")
Expand Down
60 changes: 60 additions & 0 deletions samples/kotlin-android-with-moshi-kotlin-codegen/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
plugins {
macisamuele marked this conversation as resolved.
Show resolved Hide resolved
id("com.android.library") version "3.5.3"
kotlin("android") version "1.3.61"
id("com.yelp.codegen.plugin") version "1.3.0"
kotlin("kapt") version "1.3.61"
}

android {
compileSdkVersion(28)
defaultConfig {
minSdkVersion(21)
targetSdkVersion(28)
versionCode = 1
versionName = "1.0"
}
buildTypes {
getByName("release") {
isMinifyEnabled = true
proguardFiles("moshi.pro")
}
}
}

dependencies {
// Kotlin
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.61")

// Moshi + OkHttp + Retrofit
implementation("com.squareup.moshi:moshi:1.9.2")
implementation("com.squareup.moshi:moshi-adapters:1.9.2")
implementation("com.squareup.moshi:moshi-kotlin:1.9.2")
implementation("com.squareup.okhttp3:okhttp:3.12.3")
implementation("com.squareup.retrofit2:retrofit:2.7.1")
implementation("com.squareup.retrofit2:converter-moshi:2.7.1")
implementation("com.squareup.retrofit2:adapter-rxjava2:2.7.1")
kapt("com.squareup.moshi:moshi-kotlin-codegen:1.9.2")

// Date Support
implementation("com.jakewharton.threetenabp:threetenabp:1.2.2")

// RxJava
implementation("io.reactivex.rxjava2:rxjava:2.2.17")
implementation("io.reactivex.rxjava2:rxandroid:2.1.1")
}

generateSwagger {
platform = "kotlin"
packageName = "com.yelp.codegen.samples"
specName = "sample_specs"
specVersion = "1.0.0"
inputFile = file("../sample_specs.json")
outputDir = file("./src/main/java/")
features {
headersToRemove = arrayOf("Accept-Language")
}
}

repositories {
mavenCentral()
}
Loading