Skip to content

Commit

Permalink
Merge pull request #88 from macisamuele/maci-support-moshi-codegen-em…
Browse files Browse the repository at this point in the history
…pty-class-edge-case

Annotate with @JsonClass classes with no parameters/variables
  • Loading branch information
macisamuele committed Feb 4, 2020
2 parents 722fba1 + bd7b750 commit 9de9741
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
7 changes: 6 additions & 1 deletion plugin/src/main/java/com/yelp/codegen/KotlinGenerator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,13 @@ open class KotlinGenerator : SharedCodegen() {
@VisibleForTesting
internal fun addRequiredImports(codegenModel: CodegenModel) {
// 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) {
if (codegenModel.allVars.isNotEmpty() || codegenModel.isEnum) {
codegenModel.imports.add("com.squareup.moshi.Json")
}

if (!codegenModel.isAlias) {
// If we are rendering a model (or enum) we are annotating it with @JsonClass,
// so we need to make sure that we're importing it
codegenModel.imports.add("com.squareup.moshi.JsonClass")
}

Expand Down
4 changes: 2 additions & 2 deletions plugin/src/main/resources/kotlin/data_class.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
* @property {{{name}}} {{description}}
{{/vars}}
*/
{{#hasVars}}@JsonClass(generateAdapter = true)
data {{/hasVars}}class {{classname}} {{#hasVars}}(
@JsonClass(generateAdapter = true)
{{#hasVars}}data {{/hasVars}}class {{classname}} {{#hasVars}}(
{{#requiredVars}}
{{>data_class_req_var}}{{^-last}},
{{/-last}}{{/requiredVars}}{{#hasRequired}}{{#hasOptional}},
Expand Down
20 changes: 19 additions & 1 deletion plugin/src/test/java/com/yelp/codegen/KotlinGeneratorTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@ class KotlinGeneratorTest {
val model = CodegenModel()
val property = CodegenProperty()
property.vendorExtensions = mutableMapOf()
model.vars.add(property)
model.allVars.add(property)

KotlinGenerator().addRequiredImports(model)

assertTrue(model.imports.contains("com.squareup.moshi.Json"))
assertTrue(model.imports.contains("com.squareup.moshi.JsonClass"))
assertFalse(model.imports.contains("com.yelp.test.tools.XNullable"))
}

@Test
Expand All @@ -59,6 +61,20 @@ class KotlinGeneratorTest {
KotlinGenerator().addRequiredImports(model)

assertTrue(model.imports.contains("com.squareup.moshi.Json"))
assertTrue(model.imports.contains("com.squareup.moshi.JsonClass"))
assertFalse(model.imports.contains("com.yelp.test.tools.XNullable"))
}

@Test
fun addRequiredImports_withAlias() {
val model = CodegenModel()
model.isAlias = true

KotlinGenerator().addRequiredImports(model)

assertFalse(model.imports.contains("com.squareup.moshi.Json"))
assertFalse(model.imports.contains("com.squareup.moshi.JsonClass"))
assertFalse(model.imports.contains("com.yelp.test.tools.XNullable"))
}

@Test
Expand All @@ -74,6 +90,8 @@ class KotlinGeneratorTest {

generator.addRequiredImports(model)

assertTrue(model.imports.contains("com.squareup.moshi.Json"))
assertTrue(model.imports.contains("com.squareup.moshi.JsonClass"))
assertTrue(model.imports.contains("com.yelp.test.tools.XNullable"))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

package com.yelp.codegen.generatecodesamples.models

import com.squareup.moshi.JsonClass

/**
*
*/
@JsonClass(generateAdapter = true)
class EmptyModel

0 comments on commit 9de9741

Please sign in to comment.