Skip to content

Commit 2bef805

Browse files
committed
Log warning if overlapping schema paths defined
1 parent 418cd09 commit 2bef805

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

graphql-dgs-codegen-core/src/main/kotlin/com/netflix/graphql/dgs/codegen/CodeGen.kt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,8 @@ class CodeGen(
191191
* the schema information for the parser.
192192
*/
193193
private fun loadSchemaReaders(vararg readerBuilders: MultiSourceReader.Builder) {
194+
validateNoOverlappingPaths(config.schemaFiles)
195+
194196
readerBuilders.forEach { rb ->
195197
val schemaFiles =
196198
config.schemaFiles
@@ -209,6 +211,28 @@ class CodeGen(
209211
}
210212
}
211213

214+
/**
215+
* Validates that there are no overlapping paths in the schema files to prevent duplicate loading.
216+
* Throws an [IllegalArgumentException] if any path is an ancestor of another.
217+
*/
218+
private fun validateNoOverlappingPaths(schemaFiles: Set<File>) {
219+
val canonicalPaths = schemaFiles.map { it.canonicalFile }
220+
221+
for (i in canonicalPaths.indices) {
222+
for (j in i + 1 until canonicalPaths.size) {
223+
val path1 = canonicalPaths[i]
224+
val path2 = canonicalPaths[j]
225+
226+
if (path1.startsWith(path2) || path2.startsWith(path1)) {
227+
val (parent, child) = if (path1.startsWith(path2)) path2 to path1 else path1 to path2
228+
logger.warn(
229+
"Schema path '$child' is a descendant of '$parent'. Remove one of these paths from your configuration to avoid loading duplicate schema files.",
230+
)
231+
}
232+
}
233+
}
234+
}
235+
212236
private fun generateJava(): CodeGenResult {
213237
val definitions = document.definitions
214238
// data types

graphql-dgs-codegen-core/src/main/kotlin/com/netflix/graphql/dgs/codegen/generators/kotlin/KotlinDataTypeGenerator.kt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -266,12 +266,14 @@ abstract class AbstractKotlinDataTypeGenerator(
266266
}
267267

268268
val unionTypes =
269-
document.getDefinitionsOfType(UnionTypeDefinition::class.java).filter { union ->
270-
union.memberTypes
271-
.asSequence()
272-
.map { it as TypeName }
273-
.any { it.name == name }
274-
}
269+
document
270+
.getDefinitionsOfType(UnionTypeDefinition::class.java)
271+
.filter { union ->
272+
union.memberTypes
273+
.asSequence()
274+
.map { it as TypeName }
275+
.any { it.name == name }
276+
}.distinctBy { it.name }
275277

276278
val interfaceTypes = interfaces + unionTypes
277279
interfaceTypes.forEach {

0 commit comments

Comments
 (0)