Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add supporrt for composed models and reference models #22

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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