Skip to content

Commit 838fe72

Browse files
authored
Merge pull request #22 from macisamuele/maci-support-coposed-models-and-reference-of-references
Add support for composed models and reference models
2 parents b88709e + 8f37a16 commit 838fe72

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ import io.swagger.codegen.CodegenType
1010
import io.swagger.codegen.DefaultCodegen
1111
import io.swagger.codegen.SupportingFile
1212
import io.swagger.models.ArrayModel
13+
import io.swagger.models.ComposedModel
1314
import io.swagger.models.Model
1415
import io.swagger.models.ModelImpl
1516
import io.swagger.models.Operation
17+
import io.swagger.models.RefModel
1618
import io.swagger.models.Swagger
1719
import io.swagger.models.properties.ArrayProperty
1820
import io.swagger.models.properties.MapProperty
@@ -164,14 +166,65 @@ abstract class SharedCodegen : DefaultCodegen(), CodegenConfig {
164166
}
165167
}
166168

169+
/**
170+
* Given a model determine what is the underlying data type ensuring.
171+
* The method takes care of:
172+
* * references to references
173+
* * composed models where only one of the allOf items is responsible for type definition
174+
*/
175+
private fun getModelDataType(model: Model?): String? {
176+
return when (model) {
177+
is ModelImpl -> {
178+
if (model.type != null) {
179+
model.type
180+
} else {
181+
if (false == model.properties?.isEmpty() ||
182+
model.additionalProperties != null) {
183+
"object"
184+
} else {
185+
null
186+
}
187+
}
188+
}
189+
is RefModel -> toModelName(model.simpleRef)
190+
is ComposedModel -> {
191+
val allOfModelDefinitions = model.allOf.mapNotNull { allOfItem ->
192+
when (allOfItem) {
193+
is Model, is RefModel -> getModelDataType(allOfItem)
194+
else -> null
195+
}
196+
}
197+
if (allOfModelDefinitions.size == 1) {
198+
allOfModelDefinitions[0]
199+
} else {
200+
null
201+
}
202+
}
203+
else -> null
204+
}
205+
}
206+
167207
override fun fromModel(name: String, model: Model, allDefinitions: MutableMap<String, Model>?): CodegenModel {
168208
propagateXNullable(model, allDefinitions)
169209
val codegenModel = super.fromModel(name, model, allDefinitions)
170210

211+
// Deal with composed models (models with allOf) that are meant to override descriptions and
212+
// with references to references
213+
if (model is ComposedModel || model is RefModel) {
214+
getModelDataType(model)?.let {
215+
codegenModel.isAlias = true
216+
codegenModel.dataType = it
217+
// This workaround is done to prevent regeneration of enums that would not be used anyway as
218+
// the current codegenModel is a pure type alias
219+
codegenModel.hasEnums = false
220+
}
221+
}
222+
171223
// Top level array Models should generate a typealias.
172224
if (codegenModel.isArrayModel) {
173225
codegenModel.isAlias = true
174226
}
227+
175228
// If model is an Alias will generate a typealias. We need to check if the type is aliasing
176229
// to any 'x-nullable' annotated model.
177230
if (codegenModel.isAlias) {

0 commit comments

Comments
 (0)