From 96e290a31c88180505a3b37501267660177f0d94 Mon Sep 17 00:00:00 2001 From: Samuele Maci Date: Mon, 3 Feb 2020 14:57:17 +0100 Subject: [PATCH] Annotate with @JsonClass classes with no parameters/variables --- plugin/src/main/java/com/yelp/codegen/KotlinGenerator.kt | 7 ++++++- plugin/src/main/resources/kotlin/data_class.mustache | 4 ++-- .../yelp/codegen/generatecodesamples/models/EmptyModel.kt | 3 +++ 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/plugin/src/main/java/com/yelp/codegen/KotlinGenerator.kt b/plugin/src/main/java/com/yelp/codegen/KotlinGenerator.kt index c3d16734..27ce41ec 100644 --- a/plugin/src/main/java/com/yelp/codegen/KotlinGenerator.kt +++ b/plugin/src/main/java/com/yelp/codegen/KotlinGenerator.kt @@ -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") } diff --git a/plugin/src/main/resources/kotlin/data_class.mustache b/plugin/src/main/resources/kotlin/data_class.mustache index b94d32e8..583641bd 100644 --- a/plugin/src/main/resources/kotlin/data_class.mustache +++ b/plugin/src/main/resources/kotlin/data_class.mustache @@ -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}}, diff --git a/samples/junit-tests/src/main/java/com/yelp/codegen/generatecodesamples/models/EmptyModel.kt b/samples/junit-tests/src/main/java/com/yelp/codegen/generatecodesamples/models/EmptyModel.kt index 90e2967d..54386a0f 100644 --- a/samples/junit-tests/src/main/java/com/yelp/codegen/generatecodesamples/models/EmptyModel.kt +++ b/samples/junit-tests/src/main/java/com/yelp/codegen/generatecodesamples/models/EmptyModel.kt @@ -6,7 +6,10 @@ package com.yelp.codegen.generatecodesamples.models +import com.squareup.moshi.JsonClass + /** * */ +@JsonClass(generateAdapter = true) class EmptyModel