Skip to content

Commit

Permalink
Improvements to inlined enum class generation. (#55)
Browse files Browse the repository at this point in the history
* Improvements to inlined enum class generation.
Inlined enums now get a class name prefixed with the class name that declares them. This should help avoid conflicts between enums with the same name inlined in different schemas

* formatting

* Fixing GraalVM native config
  • Loading branch information
cjbooms authored Jun 18, 2021
1 parent e7f72a3 commit a101db4
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 60 deletions.
3 changes: 2 additions & 1 deletion src/main/kotlin/com/cjbooms/fabrikt/model/KotlinTypeInfo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ sealed class KotlinTypeInfo(val modelKClass: KClass<*>, val generatedModelClassN
OasType.Date -> Date
OasType.DateTime -> DateTime
OasType.Text -> Text
OasType.Enum -> Enum(schema.getEnumValues(), schema.toModelClassName())
OasType.Enum ->
Enum(schema.getEnumValues(), schema.toModelClassName(enclosingName.toModelClassName()))
OasType.Double -> Double
OasType.Float -> Float
OasType.Number -> Numeric
Expand Down
36 changes: 26 additions & 10 deletions src/main/kotlin/com/cjbooms/fabrikt/model/PropertyInfo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import com.cjbooms.fabrikt.util.KaizenParserExtensions.getKeyIfSingleDiscriminat
import com.cjbooms.fabrikt.util.KaizenParserExtensions.hasAdditionalProperties
import com.cjbooms.fabrikt.util.KaizenParserExtensions.hasNoDiscriminator
import com.cjbooms.fabrikt.util.KaizenParserExtensions.isDiscriminatorProperty
import com.cjbooms.fabrikt.util.KaizenParserExtensions.isEnumDefinition
import com.cjbooms.fabrikt.util.KaizenParserExtensions.isInLinedObjectUnderAllOf
import com.cjbooms.fabrikt.util.KaizenParserExtensions.isInlineableMapDefinition
import com.cjbooms.fabrikt.util.KaizenParserExtensions.isInlinedArrayDefinition
import com.cjbooms.fabrikt.util.KaizenParserExtensions.isInlinedEnumDefinition
import com.cjbooms.fabrikt.util.KaizenParserExtensions.isInlinedObjectDefinition
import com.cjbooms.fabrikt.util.KaizenParserExtensions.isRequired
import com.cjbooms.fabrikt.util.KaizenParserExtensions.isSchemaLess
Expand Down Expand Up @@ -68,7 +70,9 @@ sealed class PropertyInfo {
property.value,
settings.markAsInherited,
this,
if (property.value.isInlinedArrayDefinition()) enclosingSchema else null
if (property.value.isInlinedArrayDefinition() || property.value.itemsSchema.isEnumDefinition())
enclosingSchema
else null
)
OasType.Object.type ->
if (property.value.isInlineableMapDefinition() || property.value.isSchemaLess())
Expand All @@ -85,7 +89,9 @@ sealed class PropertyInfo {
)
else if (property.value.isInlinedObjectDefinition())
ObjectInlinedField(
isRequired = isRequired(property, settings.markReadWriteOnlyOptional, settings.markAllOptional),
isRequired = isRequired(
property, settings.markReadWriteOnlyOptional, settings.markAllOptional
),
oasKey = property.key,
schema = property.value,
isInherited = settings.markAsInherited,
Expand All @@ -110,7 +116,10 @@ sealed class PropertyInfo {
schema = property.value,
isInherited = settings.markAsInherited,
isPolymorphicDiscriminator = isDiscriminatorProperty(property),
maybeDiscriminator = enclosingSchema?.let { this.getKeyIfSingleDiscriminatorValue(property, it) }
maybeDiscriminator = enclosingSchema?.let {
this.getKeyIfSingleDiscriminatorValue(property, it)
},
enclosingSchema = if (property.value.isInlinedEnumDefinition()) this else null
)
}
}
Expand Down Expand Up @@ -138,9 +147,11 @@ sealed class PropertyInfo {
val schema: Schema,
override val isInherited: Boolean,
val isPolymorphicDiscriminator: Boolean,
val maybeDiscriminator: DiscriminatorKey?
val maybeDiscriminator: DiscriminatorKey?,
val enclosingSchema: Schema? = null
) : PropertyInfo() {
override val typeInfo: KotlinTypeInfo = KotlinTypeInfo.from(schema, oasKey)
override val typeInfo: KotlinTypeInfo =
KotlinTypeInfo.from(schema, oasKey, enclosingSchema?.toModelClassName() ?: "")
val pattern: String? = schema.safeField(Schema::getPattern)
val maxLength: Int? = schema.safeField(Schema::getMaxLength)
val minLength: Int? = schema.safeField(Schema::getMinLength)
Expand All @@ -152,9 +163,12 @@ sealed class PropertyInfo {
private fun <T> Schema.safeField(getField: Schema.() -> T?): T? =
when {
this.getField() != null -> this.getField()
allOfSchemas?.firstOrNull { it.getField() != null } != null -> allOfSchemas.first { it.getField() != null }.getField()
oneOfSchemas?.firstOrNull { it.getField() != null } != null -> oneOfSchemas.first { it.getField() != null }.getField()
anyOfSchemas?.firstOrNull { it.getField() != null } != null -> anyOfSchemas.first { it.getField() != null }.getField()
allOfSchemas?.firstOrNull { it.getField() != null } != null ->
allOfSchemas.first { it.getField() != null }.getField()
oneOfSchemas?.firstOrNull { it.getField() != null } != null ->
oneOfSchemas.first { it.getField() != null }.getField()
anyOfSchemas?.firstOrNull { it.getField() != null } != null ->
anyOfSchemas.first { it.getField() != null }.getField()
else -> null
}
}
Expand All @@ -172,7 +186,8 @@ sealed class PropertyInfo {
val parentSchema: Schema,
val enclosingSchema: Schema?
) : PropertyInfo(), CollectionValidation {
override val typeInfo: KotlinTypeInfo = KotlinTypeInfo.from(schema, oasKey, enclosingSchema?.toModelClassName() ?: "")
override val typeInfo: KotlinTypeInfo =
KotlinTypeInfo.from(schema, oasKey, enclosingSchema?.toModelClassName() ?: "")
override val minItems: Int? = schema.minItems
override val maxItems: Int? = schema.maxItems
}
Expand Down Expand Up @@ -205,7 +220,8 @@ sealed class PropertyInfo {
val parentSchema: Schema,
val enclosingSchema: Schema?
) : PropertyInfo() {
override val typeInfo: KotlinTypeInfo = KotlinTypeInfo.from(schema, oasKey, enclosingSchema?.toModelClassName() ?: "")
override val typeInfo: KotlinTypeInfo =
KotlinTypeInfo.from(schema, oasKey, enclosingSchema?.toModelClassName() ?: "")
}

data class AdditionalProperties(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ object KaizenParserExtensions {
fun Schema.isInlinedObjectDefinition() =
isObjectType() && !isSchemaLess() && Overlay.of(this).pathFromRoot.contains("properties")

fun Schema.isInlinedEnumDefinition() =
isEnumDefinition() && !isSchemaLess() && Overlay.of(this).pathFromRoot.contains("properties")

fun Schema.isInlinedArrayDefinition() =
isArrayType() && !isSchemaLess() && this.itemsSchema.isInlinedObjectDefinition()

Expand Down
4 changes: 2 additions & 2 deletions src/test/resources/examples/defaultValues/models/Models.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ data class PersonWithDefaults(
@param:JsonProperty("enum_default")
@get:JsonProperty("enum_default")
@get:NotNull
val enumDefault: EnumDefault = EnumDefault.TALL,
val enumDefault: PersonWithDefaultsEnumDefault = PersonWithDefaultsEnumDefault.TALL,
@param:JsonProperty("boolean_default")
@get:JsonProperty("boolean_default")
@get:NotNull
Expand All @@ -30,7 +30,7 @@ data class PersonWithDefaults(
val stringPhrase: String = "Cowabunga Dude"
)

enum class EnumDefault(
enum class PersonWithDefaultsEnumDefault(
@JsonValue
val value: String
) {
Expand Down
12 changes: 6 additions & 6 deletions src/test/resources/examples/enumExamples/models/Models.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import kotlin.collections.List
data class EnumHolder(
@param:JsonProperty("array_of_enums")
@get:JsonProperty("array_of_enums")
val arrayOfEnums: List<ArrayOfEnums>? = null,
val arrayOfEnums: List<EnumHolderArrayOfEnums>? = null,
@param:JsonProperty("inlined_enum")
@get:JsonProperty("inlined_enum")
val inlinedEnum: InlinedEnum? = null,
val inlinedEnum: EnumHolderInlinedEnum? = null,
@param:JsonProperty("inlined_extensible_enum")
@get:JsonProperty("inlined_extensible_enum")
val inlinedExtensibleEnum: InlinedExtensibleEnum? = null,
val inlinedExtensibleEnum: EnumHolderInlinedExtensibleEnum? = null,
@param:JsonProperty("enum_ref")
@get:JsonProperty("enum_ref")
val enumRef: EnumObject? = null,
Expand All @@ -23,7 +23,7 @@ data class EnumHolder(
val extensibleEnumRef: ExtensibleEnumObject? = null
)

enum class ArrayOfEnums(
enum class EnumHolderArrayOfEnums(
@JsonValue
val value: String
) {
Expand All @@ -32,7 +32,7 @@ enum class ArrayOfEnums(
ARRAY_ENUM_TWO("array_enum_two");
}

enum class InlinedEnum(
enum class EnumHolderInlinedEnum(
@JsonValue
val value: String
) {
Expand All @@ -43,7 +43,7 @@ enum class InlinedEnum(
INLINED_THREE("inlined_three");
}

enum class InlinedExtensibleEnum(
enum class EnumHolderInlinedExtensibleEnum(
@JsonValue
val value: String
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ data class ContainingExternalReference(
val someExternalReference: ExternalObject? = null
)

enum class Enum(
enum class ExternalObjectThreeEnum(
@JsonValue
val value: String
) {
Expand All @@ -29,7 +29,7 @@ data class ExternalObjectThree(
@param:JsonProperty("enum")
@get:JsonProperty("enum")
@get:NotNull
val enum: Enum,
val enum: ExternalObjectThreeEnum,
@param:JsonProperty("description")
@get:JsonProperty("description")
@get:NotNull
Expand Down
41 changes: 34 additions & 7 deletions src/test/resources/examples/githubApi/models/Models.kt
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ data class ContributorQueryResult(
val items: List<Contributor>
)

enum class Status(
enum class ContributorStatus(
@JsonValue
val value: String
) {
Expand Down Expand Up @@ -128,7 +128,7 @@ data class Contributor(
@param:JsonProperty("status")
@get:JsonProperty("status")
@get:NotNull
val status: Status,
val status: ContributorStatus,
@param:JsonProperty("etag")
@get:JsonProperty("etag")
val etag: String? = null,
Expand Down Expand Up @@ -156,6 +156,15 @@ data class OrganisationQueryResult(
val items: List<Organisation>
)

enum class OrganisationStatus(
@JsonValue
val value: String
) {
ACTIVE("active"),

INACTIVE("inactive");
}

data class Webhook(
@param:JsonProperty("url")
@get:JsonProperty("url")
Expand Down Expand Up @@ -194,7 +203,7 @@ data class Organisation(
@param:JsonProperty("status")
@get:JsonProperty("status")
@get:NotNull
val status: Status,
val status: OrganisationStatus,
@param:JsonProperty("etag")
@get:JsonProperty("etag")
val etag: String? = null,
Expand Down Expand Up @@ -226,7 +235,16 @@ data class RepositoryQueryResult(
val items: List<Repository>
)

enum class Visibility(
enum class RepositoryStatus(
@JsonValue
val value: String
) {
ACTIVE("active"),

INACTIVE("inactive");
}

enum class RepositoryVisibility(
@JsonValue
val value: String
) {
Expand Down Expand Up @@ -263,7 +281,7 @@ data class Repository(
@param:JsonProperty("status")
@get:JsonProperty("status")
@get:NotNull
val status: Status,
val status: RepositoryStatus,
@param:JsonProperty("etag")
@get:JsonProperty("etag")
val etag: String? = null,
Expand All @@ -277,7 +295,7 @@ data class Repository(
val name: String,
@param:JsonProperty("visibility")
@get:JsonProperty("visibility")
val visibility: Visibility? = null,
val visibility: RepositoryVisibility? = null,
@param:JsonProperty("tags")
@get:JsonProperty("tags")
val tags: List<String>? = null
Expand All @@ -298,6 +316,15 @@ data class PullRequestQueryResult(
val items: List<PullRequest>
)

enum class PullRequestStatus(
@JsonValue
val value: String
) {
ACTIVE("active"),

INACTIVE("inactive");
}

data class Author(
@param:JsonProperty("name")
@get:JsonProperty("name")
Expand Down Expand Up @@ -335,7 +362,7 @@ data class PullRequest(
@param:JsonProperty("status")
@get:JsonProperty("status")
@get:NotNull
val status: Status,
val status: PullRequestStatus,
@param:JsonProperty("etag")
@get:JsonProperty("etag")
val etag: String? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"allDeclaredFields" : true,
"allPublicFields" : true
}, {
"name" : "examples.githubApi.models.Status",
"name" : "examples.githubApi.models.ContributorStatus",
"allDeclaredConstructors" : true,
"allPublicConstructors" : true,
"allDeclaredMethods" : true,
Expand All @@ -62,6 +62,14 @@
"allPublicMethods" : true,
"allDeclaredFields" : true,
"allPublicFields" : true
}, {
"name" : "examples.githubApi.models.OrganisationStatus",
"allDeclaredConstructors" : true,
"allPublicConstructors" : true,
"allDeclaredMethods" : true,
"allPublicMethods" : true,
"allDeclaredFields" : true,
"allPublicFields" : true
}, {
"name" : "examples.githubApi.models.Webhook",
"allDeclaredConstructors" : true,
Expand All @@ -87,7 +95,15 @@
"allDeclaredFields" : true,
"allPublicFields" : true
}, {
"name" : "examples.githubApi.models.Visibility",
"name" : "examples.githubApi.models.RepositoryStatus",
"allDeclaredConstructors" : true,
"allPublicConstructors" : true,
"allDeclaredMethods" : true,
"allPublicMethods" : true,
"allDeclaredFields" : true,
"allPublicFields" : true
}, {
"name" : "examples.githubApi.models.RepositoryVisibility",
"allDeclaredConstructors" : true,
"allPublicConstructors" : true,
"allDeclaredMethods" : true,
Expand All @@ -110,6 +126,14 @@
"allPublicMethods" : true,
"allDeclaredFields" : true,
"allPublicFields" : true
}, {
"name" : "examples.githubApi.models.PullRequestStatus",
"allDeclaredConstructors" : true,
"allPublicConstructors" : true,
"allDeclaredMethods" : true,
"allPublicMethods" : true,
"allDeclaredFields" : true,
"allPublicFields" : true
}, {
"name" : "examples.githubApi.models.Author",
"allDeclaredConstructors" : true,
Expand Down
Loading

0 comments on commit a101db4

Please sign in to comment.