Skip to content

Commit

Permalink
Merge pull request #22 from macisamuele/maci-support-coposed-models-a…
Browse files Browse the repository at this point in the history
…nd-reference-of-references

Add support for composed models and reference models
  • Loading branch information
cortinico committed May 15, 2019
2 parents b88709e + 8f37a16 commit 838fe72
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions plugin/src/main/java/com/yelp/codegen/SharedCodegen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import io.swagger.codegen.CodegenType
import io.swagger.codegen.DefaultCodegen
import io.swagger.codegen.SupportingFile
import io.swagger.models.ArrayModel
import io.swagger.models.ComposedModel
import io.swagger.models.Model
import io.swagger.models.ModelImpl
import io.swagger.models.Operation
import io.swagger.models.RefModel
import io.swagger.models.Swagger
import io.swagger.models.properties.ArrayProperty
import io.swagger.models.properties.MapProperty
Expand Down Expand Up @@ -164,14 +166,65 @@ abstract class SharedCodegen : DefaultCodegen(), CodegenConfig {
}
}

/**
* Given a model determine what is the underlying data type ensuring.
* The method takes care of:
* * references to references
* * composed models where only one of the allOf items is responsible for type definition
*/
private fun getModelDataType(model: Model?): String? {
return when (model) {
is ModelImpl -> {
if (model.type != null) {
model.type
} else {
if (false == model.properties?.isEmpty() ||
model.additionalProperties != null) {
"object"
} else {
null
}
}
}
is RefModel -> toModelName(model.simpleRef)
is ComposedModel -> {
val allOfModelDefinitions = model.allOf.mapNotNull { allOfItem ->
when (allOfItem) {
is Model, is RefModel -> getModelDataType(allOfItem)
else -> null
}
}
if (allOfModelDefinitions.size == 1) {
allOfModelDefinitions[0]
} else {
null
}
}
else -> null
}
}

override fun fromModel(name: String, model: Model, allDefinitions: MutableMap<String, Model>?): CodegenModel {
propagateXNullable(model, allDefinitions)
val codegenModel = super.fromModel(name, model, allDefinitions)

// Deal with composed models (models with allOf) that are meant to override descriptions and
// with references to references
if (model is ComposedModel || model is RefModel) {
getModelDataType(model)?.let {
codegenModel.isAlias = true
codegenModel.dataType = it
// This workaround is done to prevent regeneration of enums that would not be used anyway as
// the current codegenModel is a pure type alias
codegenModel.hasEnums = false
}
}

// Top level array Models should generate a typealias.
if (codegenModel.isArrayModel) {
codegenModel.isAlias = true
}

// If model is an Alias will generate a typealias. We need to check if the type is aliasing
// to any 'x-nullable' annotated model.
if (codegenModel.isAlias) {
Expand Down

0 comments on commit 838fe72

Please sign in to comment.