Skip to content

Commit 2318398

Browse files
samuelAndalonSamuel Vazquez
and
Samuel Vazquez
authored
feat: fix duplicated classes in packages and duplicated properties in constructors from interfaces (#2051)
### 🔗 Related Issues #2050 --------- Co-authored-by: Samuel Vazquez <[email protected]>
1 parent d3ad960 commit 2318398

File tree

9 files changed

+196
-2
lines changed

9 files changed

+196
-2
lines changed

plugins/client/graphql-kotlin-client-generator/src/main/kotlin/com/expediagroup/graphql/plugin/client/generator/GraphQLClientGenerator.kt

+9-1
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,21 @@ class GraphQLClientGenerator(
185185
operationTypeSpec.addType(graphQLResponseTypeSpec)
186186

187187
val polymorphicTypes = mutableListOf<ClassName>()
188+
// prevent colocating duplicated type specs in the same packageName
189+
val typeSpecByPackageName = mutableSetOf<String>()
188190
for ((superClassName, implementations) in context.polymorphicTypes) {
189191
polymorphicTypes.add(superClassName)
190192
val polymorphicTypeSpec = FileSpec.builder(superClassName.packageName, superClassName.simpleName)
191193
for (implementation in implementations) {
192194
polymorphicTypes.add(implementation)
193195
context.typeSpecs[implementation]?.let { typeSpec ->
194-
polymorphicTypeSpec.addType(typeSpec)
196+
if (
197+
typeSpec.name != null &&
198+
!typeSpecByPackageName.contains("${superClassName.packageName}.${typeSpec.name}")
199+
) {
200+
polymorphicTypeSpec.addType(typeSpec)
201+
typeSpecByPackageName.add("${superClassName.packageName}.${typeSpec.name}")
202+
}
195203
}
196204
}
197205
fileSpecs.add(polymorphicTypeSpec.build())

plugins/client/graphql-kotlin-client-generator/src/main/kotlin/com/expediagroup/graphql/plugin/client/generator/types/generateInterfaceTypeSpec.kt

+5-1
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,11 @@ private fun updateImplementationTypeSpecWithSuperInformation(
213213
} else {
214214
property
215215
}
216-
builder.addProperty(updatedProperty)
216+
217+
// add the property to the type builder only if the property was actually uptaded.
218+
if (updatedProperty != property) {
219+
builder.addProperty(updatedProperty)
220+
}
217221
constructor.addParameter(updatedProperty.name, updatedProperty.type)
218222
}
219223
builder.primaryConstructor(constructor.build())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
query UnionSameSelections {
2+
message1 {
3+
__typename
4+
... on ProductRatingLink {
5+
link {
6+
text
7+
}
8+
action {
9+
text
10+
}
11+
}
12+
... on EGDSPlainText {
13+
text
14+
}
15+
}
16+
message2 {
17+
__typename
18+
... on EGDSParagraph {
19+
text
20+
}
21+
... on EGDSPlainText {
22+
text
23+
}
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.expediagroup.graphql.generated
2+
3+
import com.expediagroup.graphql.client.Generated
4+
import com.expediagroup.graphql.client.types.GraphQLClientRequest
5+
import com.expediagroup.graphql.generated.unionsameselections.ProductRatingSupportingMessage
6+
import com.expediagroup.graphql.generated.unionsameselections.ProductSupportingMessage
7+
import com.fasterxml.jackson.`annotation`.JsonProperty
8+
import kotlin.String
9+
import kotlin.collections.List
10+
import kotlin.reflect.KClass
11+
12+
public const val UNION_SAME_SELECTIONS: String =
13+
"query UnionSameSelections {\n message1 {\n __typename\n ... on ProductRatingLink {\n link {\n text\n }\n action {\n text\n }\n }\n ... on EGDSPlainText {\n text\n }\n }\n message2 {\n __typename\n ... on EGDSParagraph {\n text\n }\n ... on EGDSPlainText {\n text\n }\n }\n}"
14+
15+
@Generated
16+
public class UnionSameSelections : GraphQLClientRequest<UnionSameSelections.Result> {
17+
override val query: String = UNION_SAME_SELECTIONS
18+
19+
override val operationName: String = "UnionSameSelections"
20+
21+
override fun responseType(): KClass<UnionSameSelections.Result> =
22+
UnionSameSelections.Result::class
23+
24+
@Generated
25+
public data class Result(
26+
@get:JsonProperty(value = "message1")
27+
public val message1: List<ProductRatingSupportingMessage>,
28+
@get:JsonProperty(value = "message2")
29+
public val message2: List<ProductSupportingMessage>,
30+
)
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.expediagroup.graphql.generated.unionsameselections
2+
3+
import com.expediagroup.graphql.client.Generated
4+
import com.fasterxml.jackson.`annotation`.JsonProperty
5+
import kotlin.String
6+
7+
@Generated
8+
public data class EGDSProductRatingShowTextAction(
9+
@get:JsonProperty(value = "text")
10+
public val text: String,
11+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.expediagroup.graphql.generated.unionsameselections
2+
3+
import com.expediagroup.graphql.client.Generated
4+
import com.fasterxml.jackson.`annotation`.JsonProperty
5+
import kotlin.String
6+
7+
@Generated
8+
public data class EGDSStandardLink(
9+
@get:JsonProperty(value = "text")
10+
public val text: String,
11+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.expediagroup.graphql.generated.unionsameselections
2+
3+
import com.expediagroup.graphql.client.Generated
4+
import com.fasterxml.jackson.`annotation`.JsonProperty
5+
import com.fasterxml.jackson.`annotation`.JsonSubTypes
6+
import com.fasterxml.jackson.`annotation`.JsonTypeInfo
7+
import com.fasterxml.jackson.`annotation`.JsonTypeInfo.As.PROPERTY
8+
import com.fasterxml.jackson.`annotation`.JsonTypeInfo.Id.NAME
9+
import kotlin.String
10+
11+
@Generated
12+
@JsonTypeInfo(
13+
use = JsonTypeInfo.Id.NAME,
14+
include = JsonTypeInfo.As.PROPERTY,
15+
property = "__typename",
16+
defaultImpl = DefaultProductRatingSupportingMessageImplementation::class,
17+
)
18+
@JsonSubTypes(value = [com.fasterxml.jackson.annotation.JsonSubTypes.Type(value =
19+
ProductRatingLink::class,
20+
name="ProductRatingLink"),com.fasterxml.jackson.annotation.JsonSubTypes.Type(value =
21+
EGDSPlainText::class, name="EGDSPlainText")])
22+
public interface ProductRatingSupportingMessage
23+
24+
@Generated
25+
public data class ProductRatingLink(
26+
@get:JsonProperty(value = "link")
27+
public val link: EGDSStandardLink,
28+
@get:JsonProperty(value = "action")
29+
public val action: EGDSProductRatingShowTextAction,
30+
) : ProductRatingSupportingMessage
31+
32+
@Generated
33+
public data class EGDSPlainText(
34+
@get:JsonProperty(value = "text")
35+
public val text: String,
36+
) : ProductRatingSupportingMessage,
37+
ProductSupportingMessage
38+
39+
/**
40+
* Fallback ProductRatingSupportingMessage implementation that will be used when unknown/unhandled
41+
* type is encountered.
42+
*/
43+
@Generated
44+
public class DefaultProductRatingSupportingMessageImplementation() : ProductRatingSupportingMessage
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.expediagroup.graphql.generated.unionsameselections
2+
3+
import com.expediagroup.graphql.client.Generated
4+
import com.fasterxml.jackson.`annotation`.JsonProperty
5+
import com.fasterxml.jackson.`annotation`.JsonSubTypes
6+
import com.fasterxml.jackson.`annotation`.JsonTypeInfo
7+
import com.fasterxml.jackson.`annotation`.JsonTypeInfo.As.PROPERTY
8+
import com.fasterxml.jackson.`annotation`.JsonTypeInfo.Id.NAME
9+
import kotlin.String
10+
11+
@Generated
12+
@JsonTypeInfo(
13+
use = JsonTypeInfo.Id.NAME,
14+
include = JsonTypeInfo.As.PROPERTY,
15+
property = "__typename",
16+
defaultImpl = DefaultProductSupportingMessageImplementation::class,
17+
)
18+
@JsonSubTypes(value = [com.fasterxml.jackson.annotation.JsonSubTypes.Type(value =
19+
EGDSParagraph::class,
20+
name="EGDSParagraph"),com.fasterxml.jackson.annotation.JsonSubTypes.Type(value =
21+
EGDSPlainText::class, name="EGDSPlainText")])
22+
public interface ProductSupportingMessage
23+
24+
@Generated
25+
public data class EGDSParagraph(
26+
@get:JsonProperty(value = "text")
27+
public val text: String,
28+
) : ProductSupportingMessage
29+
30+
/**
31+
* Fallback ProductSupportingMessage implementation that will be used when unknown/unhandled type is
32+
* encountered.
33+
*/
34+
@Generated
35+
public class DefaultProductSupportingMessageImplementation() : ProductSupportingMessage

plugins/client/graphql-kotlin-client-generator/src/test/resources/testSchema.graphql

+25
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ type Query {
112112
unionQuery: BasicUnion!
113113
"Query to test doc strings"
114114
docQuery: DocObject!
115+
message1: [ProductRatingSupportingMessage!]!
116+
message2: [ProductSupportingMessage!]!
115117
}
116118
"Wrapper that holds all supported scalar types"
117119
type ScalarWrapper {
@@ -213,3 +215,26 @@ input ScalarWrapperInput {
213215
"List of custom scalar Locales"
214216
listLocale: [Locale!]!
215217
}
218+
219+
interface EGDSText {
220+
text: String!
221+
}
222+
type EGDSPlainText implements EGDSText {
223+
text: String!
224+
}
225+
type ProductRatingLink {
226+
action: EGDSProductRatingShowTextAction!
227+
link: EGDSStandardLink!
228+
}
229+
type EGDSProductRatingShowTextAction {
230+
text: String!
231+
}
232+
type EGDSStandardLink implements EGDSText {
233+
text: String!
234+
}
235+
union ProductRatingSupportingMessage = EGDSPlainText | ProductRatingLink
236+
union ProductSupportingMessage = EGDSParagraph | EGDSPlainText
237+
238+
type EGDSParagraph implements EGDSText {
239+
text: String!
240+
}

0 commit comments

Comments
 (0)