Skip to content

Commit 6b4047e

Browse files
committed
Ensure correctness of hasMore attribute of CodegenParameter and CodegenProperty
1 parent 0a7d11a commit 6b4047e

File tree

1 file changed

+53
-1
lines changed

1 file changed

+53
-1
lines changed

gradle-plugin/plugin/src/main/java/com/yelp/codegen/SharedCodegen.kt

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import com.yelp.codegen.utils.safeSuffix
44
import io.swagger.codegen.CodegenConfig
55
import io.swagger.codegen.CodegenModel
66
import io.swagger.codegen.CodegenOperation
7+
import io.swagger.codegen.CodegenParameter
78
import io.swagger.codegen.CodegenProperty
89
import io.swagger.codegen.CodegenType
910
import io.swagger.codegen.DefaultCodegen
@@ -290,6 +291,37 @@ abstract class SharedCodegen : DefaultCodegen(), CodegenConfig {
290291
}
291292
}
292293

294+
override fun postProcessAllModels(objs: Map<String, Any>): Map<String, Any> {
295+
val postProcessedModels = super.postProcessAllModels(objs)
296+
297+
// postProcessedModel does contain a map like Map<ModelName, ContentOfTheModelAsPassedToMustache>
298+
// ContentOfTheModelAsPassedToMustache would look like the following
299+
// {
300+
// <model_name>: {
301+
// <codegen constants>
302+
// "models": [
303+
// {
304+
// "importPath": <String instance>,
305+
// "model": <CodegenModel instance>
306+
// }
307+
// ]
308+
// }
309+
// }
310+
postProcessedModels.values
311+
.asSequence()
312+
.filterIsInstance<Map<String, Any>>()
313+
.mapNotNull { it["models"] }
314+
.filterIsInstance<List<Map<String, Any>>>()
315+
.mapNotNull { it[0]["model"] }
316+
.filterIsInstance<CodegenModel>()
317+
.forEach { codegenModel ->
318+
// Ensure that after all the processing done on the CodegenModel.*Vars, hasMore does still make sense
319+
CodegenModelVar.forEachVarAttribute(codegenModel) { _, properties -> properties.fixHasMoreProperty() }
320+
}
321+
322+
return postProcessedModels
323+
}
324+
293325
/**
294326
* Private method to investigate all the properties of a models, filter only the [RefProperty] and eventually
295327
* propagate the `x-nullable` vendor extension.
@@ -375,7 +407,7 @@ abstract class SharedCodegen : DefaultCodegen(), CodegenConfig {
375407
}
376408

377409
// If we removed the last parameter of the Operation, we should update the `hasMore` flag.
378-
codegenOperation.allParams.lastOrNull()?.hasMore = false
410+
codegenOperation.allParams.fixHasMoreParameter()
379411
}
380412

381413
/**
@@ -627,3 +659,23 @@ abstract class SharedCodegen : DefaultCodegen(), CodegenConfig {
627659
}
628660
}
629661
}
662+
663+
/**
664+
* Small helper to ensurer that the hasMore attribute is properly
665+
* defined within a list of [CodegenProperty]s
666+
*/
667+
internal fun List<CodegenProperty>.fixHasMoreProperty() {
668+
this.forEachIndexed { index, item ->
669+
item.hasMore = index != (this.size - 1)
670+
}
671+
}
672+
673+
/**
674+
* Small helper to ensurer that the hasMore attribute is properly
675+
* defined within a list of [CodegenParameter]s
676+
*/
677+
internal fun List<CodegenParameter>.fixHasMoreParameter() {
678+
this.forEachIndexed { index, item ->
679+
item.hasMore = index != (this.size - 1)
680+
}
681+
}

0 commit comments

Comments
 (0)