Skip to content
This repository has been archived by the owner on Aug 26, 2021. It is now read-only.

Commit

Permalink
Merge pull request #138 from vimeo/develop
Browse files Browse the repository at this point in the history
Release 2.4.0
  • Loading branch information
anthonycr authored Oct 24, 2017
2 parents b6332d1 + 6927c1a commit 7e6741c
Show file tree
Hide file tree
Showing 44 changed files with 1,092 additions and 1,292 deletions.
15 changes: 6 additions & 9 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
#### Ticket
**Required for Vimeans only**
[TICKET_NUMBER](https://vimean.atlassian.net/browse/TICKET_NUMBER)
#### Issue
(If applicable)
- https://github.com/vimeo/stag-java/issues/NUMBER

#### Ticket Summary
A brief but thorough description of the ticket.

#### Implementation Summary
A brief but thorough description of the changes put in place to address the ticket.
#### Summary
A brief but thorough description of the changes.

#### How to Test
Detailed *list* of what to test and how to test it. Including all edge cases.
Detailed list of what to test and how to test it. Include all edge cases.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Change Log
==========

Version 2.4.0 *(2017-10-24)*
----------------------------
- Fixed bug where generated type adapters would write out an empty JSON object for a `null` value.
- Type adapters now write `null` to JSON instead of `{}` for `null` field values.
- Rewrote compiler tests in Kotlin and improved their readability.

Version 2.3.3 *(2017-09-15)*
----------------------------
- `isXYZ()` and `setXYZ()` are now accepted as getter and setter names for boolean fields named `isXYZ` or `mIsXYZ`.
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ buildscript {
apply plugin: 'net.ltgt.apt'
dependencies {
def stagVersion = '2.3.3'
def stagVersion = '2.4.0'
compile "com.vimeo.stag:stag-library:$stagVersion"
apt "com.vimeo.stag:stag-library-compiler:$stagVersion"
}
Expand All @@ -57,7 +57,7 @@ gradle.projectsEvaluated {

```groovy
dependencies {
def stagVersion = '2.3.3'
def stagVersion = '2.4.0'
compile "com.vimeo.stag:stag-library:$stagVersion"
annotationProcessor "com.vimeo.stag:stag-library-compiler:$stagVersion"
}
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {

ext.kotlin_version = '1.1.4-3'
ext.kotlin_version = '1.1.51'
ext.jacoco_version = '0.7.9' // See http://www.eclemma.org/jacoco/

// android dependencies
Expand Down Expand Up @@ -40,5 +40,5 @@ allprojects {

subprojects {
group = 'com.vimeo.stag'
version = '2.3.3'
version = '2.4.0'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.vimeo.sample_java_model;

import com.google.gson.annotations.SerializedName;
import com.vimeo.stag.UseStag;

import org.jetbrains.annotations.Nullable;

/**
* A test case for an object containing null fields.
* <p>
* Created by restainoa on 10/20/17.
*/
@UseStag
public class NullFields {

@Nullable
@SerializedName("hello")
public Object hello;


@Override
public boolean equals(Object o) {
if (this == o) { return true; }
if (o == null || getClass() != o.getClass()) { return false; }

NullFields that = (NullFields) o;

return hello != null ? hello.equals(that.hello) : that.hello == null;

}

@Override
public int hashCode() {
return hello != null ? hello.hashCode() : 0;
}
}
1 change: 1 addition & 0 deletions sample-kotlin-model/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ dependencies {

compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
testCompile 'uk.co.jemos.podam:podam:7.1.0.RELEASE'
}

repositories {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@ import com.vimeo.stag.UseStag
* Created by anthonycr on 9/2/17.
*/
@UseStag
class BoolFields {

@SerializedName("test1")
var test1: Boolean? = null

@SerializedName("test2")
var isTest2: Boolean? = null

}
data class BoolFields(
@SerializedName("test1") var test1: Boolean? = null,
@SerializedName("test2") var isTest2: Boolean? = null
)
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,22 @@ class KotlinConcreteExample : KotlinGenericExample<String>() {

var kotlinObject: KotlinSamples? = null

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
if (!super.equals(other)) return false

other as KotlinConcreteExample

if (kotlinObject != other.kotlinObject) return false

return true
}

override fun hashCode(): Int {
var result = super.hashCode()
result = 31 * result + (kotlinObject?.hashCode() ?: 0)
return result
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,19 @@ abstract class KotlinGenericExample<T> {

var genericField: T? = null

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as KotlinGenericExample<*>

if (genericField != other.genericField) return false

return true
}

override fun hashCode(): Int {
return genericField?.hashCode() ?: 0
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,11 @@ import com.vimeo.stag.UseStag
* Created by restainoa on 5/8/17.
*/
@UseStag
class KotlinSamples {
var stringField: String? = null

var nonNullStringField: String = "default"

var intField: Int? = null

var longField: Long = 1

@SerializedName("boolean_field")
var booleanField: Boolean? = null

var testField: Any? = null
}
data class KotlinSamples(
@SerializedName("string_field") var stringField: String? = null,
@SerializedName("non_null_string_field") var nonNullStringField: String = "default",
@SerializedName("int_field") var intField: Int? = null,
@SerializedName("long_field") var longField: Long = 1,
@SerializedName("boolean_field") var booleanField: Boolean? = null,
var notAnnotatedField: Int? = null // will still be picked up by the compiler and will look for json field named "nonAnnotatedField"
)
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ import org.junit.Test
class BoolFieldsTest {

@Test
fun name() {
fun verifyTypeAdapterWasGenerated() {
Utils.verifyTypeAdapterGeneration(BoolFields::class)
}

@Test
fun verifyTypeAdapterCorrect() {
Utils.verifyTypeAdapterCorrectness(BoolFields::class)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ import org.junit.Test
*/
class KotlinConcreteExampleTest {

@Test fun verifyTypeAdapterGenerated() {
@Test
fun verifyTypeAdapterGenerated() {
Utils.verifyTypeAdapterGeneration(KotlinConcreteExample::class)
}

@Test
fun verifyTypeAdapterCorrectness() {
Utils.verifyTypeAdapterCorrectness(KotlinConcreteExample::class)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ class KotlinSamplesTest {
Utils.verifyTypeAdapterGeneration(KotlinSamples::class)
}

@Test
fun verifyTypeAdapterCorrectness() {
Utils.verifyTypeAdapterCorrectness(KotlinSamples::class)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import com.google.gson.TypeAdapter
import com.google.gson.reflect.TypeToken
import com.vimeo.sample_kotlin.stag.generated.Stag
import junit.framework.Assert
import junit.framework.Assert.assertEquals
import uk.co.jemos.podam.api.PodamFactoryImpl
import kotlin.reflect.KClass

/**
Expand Down Expand Up @@ -50,5 +52,24 @@ class Utils {
val typeAdapter = getTypeAdapter(clazz)
Assert.assertNull("Type adapter should not have been generated by Stag", typeAdapter)
}

/**
* Verifies that the type adapter for [clazz] is correct. It does this by manufacturing an
* instance of the class, writing it to JSON, and then reading that object back out of JSON
* and comparing the two instances.
*
* @param clazz the [KClass] to use to get the [TypeAdapter].
*/
fun <T : Any> verifyTypeAdapterCorrectness(clazz: KClass<T>) {
val factory = PodamFactoryImpl()

val obj: T = factory.manufacturePojo<T>(clazz.java)
val typeAdapter: TypeAdapter<T>? = getTypeAdapter(clazz)

val json = typeAdapter?.toJson(obj)

assertEquals(obj, typeAdapter?.fromJson(json))
}

}
}
11 changes: 8 additions & 3 deletions stag-library-compiler/build.gradle
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
apply plugin: 'java'
apply plugin: 'kotlin'
apply plugin: 'jacoco'
apply plugin: 'maven-publish'

compileJava {
sourceCompatibility = 1.7
targetCompatibility = 1.7
sourceCompatibility = "1.7"
targetCompatibility = "1.7"
}

jacoco {
Expand All @@ -23,7 +24,9 @@ jacocoTestReport.dependsOn test

dependencies {
testCompile 'junit:junit:4.12'
testCompile 'com.google.testing.compile:compile-testing:0.8'
testCompile 'org.assertj:assertj-core:3.8.0'
testCompile 'com.google.testing.compile:compile-testing:0.12'
testCompile project(path: ':sample-java-model')

// Forcibly add test resources to test classpath: https://code.google.com/p/android/issues/detail?id=64887
testRuntime files('build/resources/test')
Expand All @@ -34,6 +37,8 @@ dependencies {
compile 'com.squareup:javapoet:1.8.0'
compile 'com.intellij:annotations:12.0@jar'
compile 'com.google.auto.service:auto-service:1.0-rc3'

compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
}

test {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ private TypeSpec getAdapterFactorySpec() {
}

@NotNull
private MethodSpec getSetOrThrowMethodSpec() {
private static MethodSpec getSetOrThrowMethodSpec() {
return MethodSpec.methodBuilder("setOrThrow")
.returns(TypeName.VOID)
.addParameter(Gson.class, "gson")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -724,11 +724,11 @@ private static MethodSpec getWriteMethodSpec(@NotNull TypeName typeName,
.addAnnotation(Override.class)
.addException(IOException.class);

builder.addStatement("writer.beginObject()");
builder.beginControlFlow("if (object == null)");
builder.addStatement("writer.endObject()");
builder.addStatement("writer.nullValue()");
builder.addStatement("return");
builder.endControlFlow();
builder.addStatement("writer.beginObject()");

for (Map.Entry<FieldAccessor, TypeMirror> element : memberVariables.entrySet()) {
FieldAccessor fieldAccessor = element.getKey();
Expand Down
Loading

0 comments on commit 7e6741c

Please sign in to comment.