Skip to content

Commit 4596b6d

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

File tree

9 files changed

+182
-2
lines changed

9 files changed

+182
-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
@@ -179,13 +179,21 @@ class GraphQLClientGenerator(
179179
operationTypeSpec.addType(graphQLResponseTypeSpec)
180180

181181
val polymorphicTypes = mutableListOf<ClassName>()
182+
// prevent colocating duplicated type specs in the same packageName
183+
val typeSpecByPackageName = mutableSetOf<String>()
182184
for ((superClassName, implementations) in context.polymorphicTypes) {
183185
polymorphicTypes.add(superClassName)
184186
val polymorphicTypeSpec = FileSpec.builder(superClassName.packageName, superClassName.simpleName)
185187
for (implementation in implementations) {
186188
polymorphicTypes.add(implementation)
187189
context.typeSpecs[implementation]?.let { typeSpec ->
188-
polymorphicTypeSpec.addType(typeSpec)
190+
if (
191+
typeSpec.name != null &&
192+
!typeSpecByPackageName.contains("${superClassName.packageName}.${typeSpec.name}")
193+
) {
194+
polymorphicTypeSpec.addType(typeSpec)
195+
typeSpecByPackageName.add("${superClassName.packageName}.${typeSpec.name}")
196+
}
189197
}
190198
}
191199
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,28 @@
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 kotlin.String
8+
import kotlin.collections.List
9+
import kotlin.reflect.KClass
10+
11+
public const val UNION_SAME_SELECTIONS: String =
12+
"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}"
13+
14+
@Generated
15+
public class UnionSameSelections : GraphQLClientRequest<UnionSameSelections.Result> {
16+
override val query: String = UNION_SAME_SELECTIONS
17+
18+
override val operationName: String = "UnionSameSelections"
19+
20+
override fun responseType(): KClass<UnionSameSelections.Result> =
21+
UnionSameSelections.Result::class
22+
23+
@Generated
24+
public data class Result(
25+
public val message1: List<ProductRatingSupportingMessage>,
26+
public val message2: List<ProductSupportingMessage>,
27+
)
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.expediagroup.graphql.generated.unionsameselections
2+
3+
import com.expediagroup.graphql.client.Generated
4+
import kotlin.String
5+
6+
@Generated
7+
public data class EGDSProductRatingShowTextAction(
8+
public val text: String,
9+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.expediagroup.graphql.generated.unionsameselections
2+
3+
import com.expediagroup.graphql.client.Generated
4+
import kotlin.String
5+
6+
@Generated
7+
public data class EGDSStandardLink(
8+
public val text: String,
9+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.expediagroup.graphql.generated.unionsameselections
2+
3+
import com.expediagroup.graphql.client.Generated
4+
import com.fasterxml.jackson.`annotation`.JsonSubTypes
5+
import com.fasterxml.jackson.`annotation`.JsonTypeInfo
6+
import com.fasterxml.jackson.`annotation`.JsonTypeInfo.As.PROPERTY
7+
import com.fasterxml.jackson.`annotation`.JsonTypeInfo.Id.NAME
8+
import kotlin.String
9+
10+
@Generated
11+
@JsonTypeInfo(
12+
use = JsonTypeInfo.Id.NAME,
13+
include = JsonTypeInfo.As.PROPERTY,
14+
property = "__typename",
15+
defaultImpl = DefaultProductRatingSupportingMessageImplementation::class,
16+
)
17+
@JsonSubTypes(value = [com.fasterxml.jackson.annotation.JsonSubTypes.Type(value =
18+
ProductRatingLink::class,
19+
name="ProductRatingLink"),com.fasterxml.jackson.annotation.JsonSubTypes.Type(value =
20+
EGDSPlainText::class, name="EGDSPlainText")])
21+
public interface ProductRatingSupportingMessage
22+
23+
@Generated
24+
public data class ProductRatingLink(
25+
public val link: EGDSStandardLink,
26+
public val action: EGDSProductRatingShowTextAction,
27+
) : ProductRatingSupportingMessage
28+
29+
@Generated
30+
public data class EGDSPlainText(
31+
public val text: String,
32+
) : ProductRatingSupportingMessage, ProductSupportingMessage
33+
34+
/**
35+
* Fallback ProductRatingSupportingMessage implementation that will be used when unknown/unhandled
36+
* type is encountered.
37+
*/
38+
@Generated
39+
public class DefaultProductRatingSupportingMessageImplementation() : ProductRatingSupportingMessage
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.expediagroup.graphql.generated.unionsameselections
2+
3+
import com.expediagroup.graphql.client.Generated
4+
import com.fasterxml.jackson.`annotation`.JsonSubTypes
5+
import com.fasterxml.jackson.`annotation`.JsonTypeInfo
6+
import com.fasterxml.jackson.`annotation`.JsonTypeInfo.As.PROPERTY
7+
import com.fasterxml.jackson.`annotation`.JsonTypeInfo.Id.NAME
8+
import kotlin.String
9+
10+
@Generated
11+
@JsonTypeInfo(
12+
use = JsonTypeInfo.Id.NAME,
13+
include = JsonTypeInfo.As.PROPERTY,
14+
property = "__typename",
15+
defaultImpl = DefaultProductSupportingMessageImplementation::class,
16+
)
17+
@JsonSubTypes(value = [com.fasterxml.jackson.annotation.JsonSubTypes.Type(value =
18+
EGDSParagraph::class,
19+
name="EGDSParagraph"),com.fasterxml.jackson.annotation.JsonSubTypes.Type(value =
20+
EGDSPlainText::class, name="EGDSPlainText")])
21+
public interface ProductSupportingMessage
22+
23+
@Generated
24+
public data class EGDSParagraph(
25+
public val text: String,
26+
) : ProductSupportingMessage
27+
28+
/**
29+
* Fallback ProductSupportingMessage implementation that will be used when unknown/unhandled type is
30+
* encountered.
31+
*/
32+
@Generated
33+
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)