Skip to content

Commit f3e3ad9

Browse files
committed
Plug PolymorphicFactory into GeneratedCodeConverters Moshi creation
1 parent 35bace7 commit f3e3ad9

File tree

11 files changed

+361
-3
lines changed

11 files changed

+361
-3
lines changed

gradle-plugin/plugin/src/main/resources/kotlin/tools/GeneratedCodeConverters.kt.mustache

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import retrofit2.converter.moshi.MoshiConverterFactory
77
object GeneratedCodeConverters {
88
private val moshi = Moshi.Builder()
99
.add(XNullableAdapterFactory())
10+
.add(PolymorphicAdapterFactory())
1011
.add(TypesAdapterFactory())
1112
.build()
1213

samples/junit-tests/junit_tests_specs.json

Lines changed: 135 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,102 @@
11
{
22
"definitions": {
3-
"empty_model": {
3+
"animal": {
4+
"allOf": [
5+
{
6+
"$ref": "#/definitions/living_thing"
7+
},
8+
{
9+
"discriminator": "type",
10+
"properties": {
11+
"classification": {
12+
"enum": [
13+
"amphibian",
14+
"bird",
15+
"fish",
16+
"insect",
17+
"mammal",
18+
"reptile"
19+
],
20+
"type": "string"
21+
},
22+
"type": {
23+
"type": "string"
24+
}
25+
},
26+
"required": [
27+
"type"
28+
],
29+
"type": "object"
30+
}
31+
],
32+
"description": "This is a generic `animal`. According to the value of `type` you might have more information",
33+
"type": "object",
34+
"x-model": "animal"
35+
},
36+
"bird": {
37+
"allOf": [
38+
{
39+
"$ref": "#/definitions/animal"
40+
},
41+
{
42+
"properties": {
43+
"flight_hours": {
44+
"type": "number"
45+
}
46+
}
47+
}
48+
],
49+
"description": "This is a specific type of `animal`: a `bird`",
50+
"title": "Bird",
451
"type": "object",
5-
"x-model": "empty_model"
52+
"x-model": "bird"
53+
},
54+
"cat": {
55+
"allOf": [
56+
{
57+
"$ref": "#/definitions/animal"
58+
},
59+
{
60+
"properties": {
61+
"age": {
62+
"type": "number"
63+
}
64+
},
65+
"type": "object"
66+
}
67+
],
68+
"description": "This is a specific type of `animal`: a `cat`",
69+
"type": "object",
70+
"x-model": "Cat"
71+
},
72+
"dog": {
73+
"allOf": [
74+
{
75+
"$ref": "#/definitions/animal"
76+
},
77+
{
78+
"properties": {
79+
"size": {
80+
"enum": [
81+
"small",
82+
"medium",
83+
"large"
84+
],
85+
"type": "string"
86+
}
87+
},
88+
"required": [
89+
"size"
90+
],
91+
"type": "object"
92+
}
93+
],
94+
"description": "This is a specific type of `animal`: a `dog`",
95+
"type": "object",
96+
"x-model": "dog"
97+
},
98+
"empty_model": {
99+
"type": "object"
6100
},
7101
"format_responses": {
8102
"properties": {
@@ -25,10 +119,24 @@
25119
"type": "object",
26120
"x-model": "format_responses"
27121
},
122+
"living_thing": {
123+
"description": "This is a base class of `animal`. Created to verify that multiple parents are properly dealt with",
124+
"properties": {
125+
"name": {
126+
"type": "string"
127+
}
128+
},
129+
"required": [
130+
"name"
131+
],
132+
"title": "living_thing",
133+
"type": "object"
134+
},
28135
"nested_additional_properties": {
29136
"additionalProperties": {
30137
"$ref": "#/definitions/top_level_map"
31138
},
139+
"title": "xmodel_has_precedence_so_this_will_not_be_used",
32140
"type": "object",
33141
"x-model": "nested_additional_properties"
34142
},
@@ -142,6 +250,15 @@
142250
"type": "object",
143251
"x-model": "reserved_keywords"
144252
},
253+
"snake": {
254+
"allOf": [
255+
{
256+
"$ref": "#/definitions/animal"
257+
}
258+
],
259+
"description": "This is a specific type of `animal`: a `snake`",
260+
"type": "object"
261+
},
145262
"top_level_enum": {
146263
"enum": [
147264
"TOP_LEVEL_VALUE1",
@@ -397,6 +514,22 @@
397514
"version": "1.1.0"
398515
},
399516
"paths": {
517+
"/check_polymorphism": {
518+
"get": {
519+
"operationId": "get_check_polymorphism",
520+
"responses": {
521+
"200": {
522+
"description": "",
523+
"schema": {
524+
"$ref": "#/definitions/animal"
525+
}
526+
}
527+
},
528+
"tags": [
529+
"polymorphism"
530+
]
531+
}
532+
},
400533
"/empty_endpoint": {
401534
"get": {
402535
"operationId": "get_empty_endpoint",
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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 com.yelp.codegen.generatecodesamples.models.Animal
10+
import io.reactivex.Single
11+
import retrofit2.http.GET
12+
import retrofit2.http.Headers
13+
14+
@JvmSuppressWildcards
15+
interface PolymorphismApi {
16+
/**
17+
* The endpoint is owned by junittests service owner
18+
*/
19+
@Headers(
20+
"X-Operation-ID: get_check_polymorphism"
21+
)
22+
@GET("/check_polymorphism")
23+
fun getCheckPolymorphism(): Single<Animal>
24+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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.models
8+
9+
import com.squareup.moshi.Json
10+
import com.squareup.moshi.JsonClass
11+
import com.yelp.codegen.generatecodesamples.tools.Polymorphic
12+
import com.yelp.codegen.generatecodesamples.tools.optimisticHashCode
13+
14+
/**
15+
* This is a generic `animal`. According to the value of `type` you might have more information
16+
* @property name
17+
* @property classification
18+
* @property type
19+
*/
20+
@JsonClass(generateAdapter = true)
21+
@Polymorphic(
22+
discriminatorField = "type",
23+
discriminatedValues = ["bird", "dog", "snake", "Cat"],
24+
discriminatedClasses = [Bird::class, Dog::class, Snake::class, Cat::class]
25+
)
26+
open class Animal(
27+
@Json(name = "name") @field:Json(name = "name") open var name: String,
28+
@Json(name = "type") @field:Json(name = "type") open var type: String,
29+
@Json(name = "classification") @field:Json(name = "classification") open var classification: Animal.ClassificationEnum? = null
30+
) {
31+
override fun toString(): String {
32+
return "Animal(" +
33+
"name=$name," +
34+
"classification=$classification," +
35+
"type=$type" +
36+
")"
37+
}
38+
39+
override fun hashCode(): Int {
40+
var resultHashCode = 0
41+
resultHashCode = resultHashCode * 31 + this.name.optimisticHashCode()
42+
resultHashCode = resultHashCode * 31 + this.classification.optimisticHashCode()
43+
resultHashCode = resultHashCode * 31 + this.type.optimisticHashCode()
44+
return resultHashCode
45+
}
46+
47+
override fun equals(other: Any?): Boolean {
48+
return if (this === other) {
49+
true
50+
} else {
51+
other is Animal &&
52+
this.name == other.name &&
53+
this.classification == other.classification &&
54+
this.type == other.type
55+
}
56+
}
57+
58+
/**
59+
* Values: AMPHIBIAN, BIRD, FISH, INSECT, MAMMAL, REPTILE
60+
*/
61+
@JsonClass(generateAdapter = false)
62+
enum class ClassificationEnum(val value: String) {
63+
@Json(name = "amphibian") AMPHIBIAN("amphibian"),
64+
@Json(name = "bird") BIRD("bird"),
65+
@Json(name = "fish") FISH("fish"),
66+
@Json(name = "insect") INSECT("insect"),
67+
@Json(name = "mammal") MAMMAL("mammal"),
68+
@Json(name = "reptile") REPTILE("reptile")
69+
}
70+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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.models
8+
9+
import com.squareup.moshi.Json
10+
import com.squareup.moshi.JsonClass
11+
import java.math.BigDecimal
12+
13+
/**
14+
* This is a specific type of `animal`: a `bird`
15+
* @property name
16+
* @property classification
17+
* @property type
18+
* @property flightHours
19+
*/
20+
@JsonClass(generateAdapter = true)
21+
data class Bird(
22+
@Json(name = "name") @field:Json(name = "name") override var name: String,
23+
@Json(name = "type") @field:Json(name = "type") override var type: String,
24+
@Json(name = "flight_hours") @field:Json(name = "flight_hours") var flightHours: BigDecimal? = null,
25+
@Json(name = "classification") @field:Json(name = "classification") override var classification: Animal.ClassificationEnum? = null
26+
) : Animal(name = name, classification = classification, type = type)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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.models
8+
9+
import com.squareup.moshi.Json
10+
import com.squareup.moshi.JsonClass
11+
import java.math.BigDecimal
12+
13+
/**
14+
* This is a specific type of `animal`: a `cat`
15+
* @property name
16+
* @property classification
17+
* @property type
18+
* @property age
19+
*/
20+
@JsonClass(generateAdapter = true)
21+
data class Cat(
22+
@Json(name = "name") @field:Json(name = "name") override var name: String,
23+
@Json(name = "type") @field:Json(name = "type") override var type: String,
24+
@Json(name = "age") @field:Json(name = "age") var age: BigDecimal? = null,
25+
@Json(name = "classification") @field:Json(name = "classification") override var classification: Animal.ClassificationEnum? = null
26+
) : Animal(name = name, classification = classification, type = type)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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.models
8+
9+
import com.squareup.moshi.Json
10+
import com.squareup.moshi.JsonClass
11+
12+
/**
13+
* This is a specific type of `animal`: a `dog`
14+
* @property name
15+
* @property classification
16+
* @property type
17+
* @property size
18+
*/
19+
@JsonClass(generateAdapter = true)
20+
data class Dog(
21+
@Json(name = "size") @field:Json(name = "size") var size: Dog.SizeEnum,
22+
@Json(name = "name") @field:Json(name = "name") override var name: String,
23+
@Json(name = "type") @field:Json(name = "type") override var type: String,
24+
@Json(name = "classification") @field:Json(name = "classification") override var classification: Animal.ClassificationEnum? = null
25+
) : Animal(name = name, classification = classification, type = type) {
26+
/**
27+
* Values: SMALL, MEDIUM, LARGE
28+
*/
29+
@JsonClass(generateAdapter = false)
30+
enum class SizeEnum(val value: String) {
31+
@Json(name = "small") SMALL("small"),
32+
@Json(name = "medium") MEDIUM("medium"),
33+
@Json(name = "large") LARGE("large")
34+
}
35+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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.models
8+
9+
import com.squareup.moshi.Json
10+
import com.squareup.moshi.JsonClass
11+
12+
/**
13+
* This is a base class of `animal`. Created to verify that multiple parents are properly dealt with
14+
* @property name
15+
*/
16+
@JsonClass(generateAdapter = true)
17+
data class LivingThing(
18+
@Json(name = "name") @field:Json(name = "name") var name: String
19+
)

0 commit comments

Comments
 (0)