Skip to content

Commit f39a140

Browse files
committed
Add supporrt for composed models and reference models
1 parent 08a09e3 commit f39a140

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+
if (false == model.properties?.isEmpty() || model.additionalProperties != null) {
180+
"object"
181+
} else {
182+
null
183+
}
184+
} else {
185+
model.type
186+
}
187+
}
188+
is RefModel -> toModelName(model.simpleRef)
189+
is ComposedModel -> {
190+
val allOfModelDefinitions = model.allOf.mapNotNull { allOfItem ->
191+
when (allOfItem) {
192+
is Model, is RefModel -> getModelDataType(allOfItem)
193+
else -> null
194+
}
195+
}
196+
if (allOfModelDefinitions.size == 1) {
197+
allOfModelDefinitions[0]
198+
} else {
199+
null
200+
}
201+
}
202+
else -> null
203+
}
204+
}
205+
167206
override fun fromModel(name: String, model: Model, allDefinitions: MutableMap<String, Model>?): CodegenModel {
168207
propagateXNullable(model, allDefinitions)
169208
val codegenModel = super.fromModel(name, model, allDefinitions)
170209

210+
// Deal with composed models (models with allOf) that are meant to override descriptions and
211+
// with references to references
212+
if (model is ComposedModel || model is RefModel) {
213+
val modelDataType = getModelDataType(model)
214+
if (modelDataType != null) {
215+
codegenModel.isAlias = true
216+
codegenModel.dataType = modelDataType
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)