Skip to content

Commit 4a7a427

Browse files
committed
Ensure correctness of hasMore attribute of CodegenParameter and CodegenProperty
1 parent 7a2369f commit 4a7a427

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
@@ -293,6 +294,37 @@ abstract class SharedCodegen : DefaultCodegen(), CodegenConfig {
293294
}
294295
}
295296

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

380412
// If we removed the last parameter of the Operation, we should update the `hasMore` flag.
381-
codegenOperation.allParams.lastOrNull()?.hasMore = false
413+
codegenOperation.allParams.fixHasMoreParameter()
382414
}
383415

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

0 commit comments

Comments
 (0)