Skip to content

Commit

Permalink
Issue #36: Generated class names are inconsistent for inline array ob…
Browse files Browse the repository at this point in the history
…jects (#38)
  • Loading branch information
averabaq authored Apr 30, 2021
1 parent a2b4cd8 commit fa13de9
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 31 deletions.
39 changes: 8 additions & 31 deletions src/main/kotlin/com/cjbooms/fabrikt/model/PropertyInfo.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package com.cjbooms.fabrikt.model

import com.cjbooms.fabrikt.util.KaizenParserExtensions.getKeyIfDiscriminator
import com.cjbooms.fabrikt.util.KaizenParserExtensions.getPolymorphicSubTypes
import com.cjbooms.fabrikt.util.KaizenParserExtensions.hasAdditionalProperties
import com.cjbooms.fabrikt.util.KaizenParserExtensions.isDiscriminatorProperty
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.isInlinedObjectDefinition
import com.cjbooms.fabrikt.util.KaizenParserExtensions.isPolymorphicSuperType
import com.cjbooms.fabrikt.util.KaizenParserExtensions.isRequired
import com.cjbooms.fabrikt.util.KaizenParserExtensions.isSchemaLess
import com.cjbooms.fabrikt.util.KaizenParserExtensions.safeType
import com.cjbooms.fabrikt.util.KaizenParserExtensions.toModelClassName
import com.cjbooms.fabrikt.util.NormalisedString.camelCase
import com.reprezen.kaizen.oasparser.model3.OpenApi3
import com.reprezen.kaizen.oasparser.model3.Schema

sealed class PropertyInfo {
Expand Down Expand Up @@ -60,15 +58,16 @@ sealed class PropertyInfo {
settings: Settings,
enclosingSchema: Schema? = null
): Collection<PropertyInfo> {
val mainProperties = properties.map { property ->
val mainProperties: List<PropertyInfo> = properties.map { property ->
when (property.value.safeType()) {
OasType.Array.type ->
ListField(
isRequired(property, settings.markReadWriteOnlyOptional, settings.markAllOptional),
property.key,
property.value,
settings.markAsInherited,
this
this,
if (property.value.isInlinedArrayDefinition()) enclosingSchema else null
)
OasType.Object.type ->
if (property.value.isInlineableMapDefinition() || property.value.isSchemaLess())
Expand Down Expand Up @@ -115,29 +114,14 @@ sealed class PropertyInfo {
}
}
}.filterNotNull()

return if (hasAdditionalProperties())
mainProperties
.plus(
AdditionalProperties(additionalPropertiesSchema, settings.markAsInherited, this)
)
else mainProperties
}

fun Schema.propertiesInPolymorphicHierarchy(api3: OpenApi3, settings: Settings): Collection<PropertyInfo> {
if (!isPolymorphicSuperType()) throw IllegalArgumentException("Model is not a polymorphic super type")
val results = emptyList<PropertyInfo>() +
topLevelProperties(settings.copy(markAsInherited = true)) +
getPolymorphicSubTypes(api3).flatMap { subType ->
emptyList<PropertyInfo>() +
subType.getInLinedProperties(settings) +
subType.allOfSchemas
.filterNot { it == this }
.flatMap { it.topLevelProperties(settings.copy(markAsInherited = !it.isInLinedObjectUnderAllOf())) } +
subType.oneOfSchemas.flatMap { it.topLevelProperties(settings.copy(markAllOptional = true)) } +
subType.anyOfSchemas.flatMap { it.topLevelProperties(settings.copy(markAllOptional = true)) }
}
return results.toSet()
}
}

sealed class DiscriminatorKey(val stringValue: String) {
Expand Down Expand Up @@ -179,22 +163,15 @@ sealed class PropertyInfo {
val maxItems: Int?
}

class DummyField(
override val oasKey: String,
override val typeInfo: KotlinTypeInfo,
override val isRequired: Boolean = true,
override val minItems: Int? = null,
override val maxItems: Int? = null
) : PropertyInfo(), CollectionValidation

data class ListField(
override val isRequired: Boolean,
override val oasKey: String,
val schema: Schema,
override val isInherited: Boolean,
val parentSchema: Schema
val parentSchema: Schema,
val enclosingSchema: Schema?
) : PropertyInfo(), CollectionValidation {
override val typeInfo: KotlinTypeInfo = KotlinTypeInfo.from(schema, oasKey)
override val typeInfo: KotlinTypeInfo = KotlinTypeInfo.from(schema, oasKey, enclosingSchema?.toModelClassName() ?: "")
override val minItems: Int? = schema.minItems
override val maxItems: Int? = schema.maxItems
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ object KaizenParserExtensions {
fun Schema.isInlinedObjectDefinition() =
isObjectType() && !isSchemaLess() && Overlay.of(this).pathFromRoot.contains("properties")

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

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

Expand Down Expand Up @@ -85,6 +88,8 @@ object KaizenParserExtensions {

fun Schema.isObjectType() = OasType.Object.type == type

fun Schema.isArrayType() = OasType.Array.type == type

fun Schema.isNotDefined() = type == null && !(hasAllOfSchemas() || hasOneOfSchemas() || hasAnyOfSchemas())

fun Schema.getSchemaNameInParent(): String? = Overlay.of(this).pathInParent
Expand Down
16 changes: 16 additions & 0 deletions src/test/resources/examples/inLinedObject/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,19 @@ components:
type: string
direct:
type: string

ThirdInlineObject:
type: object
properties:
generation:
type: object
properties:
urls:
type: array
items:
type: object
properties:
version:
type: string
view_name:
type: string
24 changes: 24 additions & 0 deletions src/test/resources/examples/inLinedObject/models/Models.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonProperty
import javax.validation.Valid
import javax.validation.constraints.NotNull
import kotlin.String
import kotlin.collections.List

data class FirstInlineObject(
@param:JsonProperty("generation")
Expand Down Expand Up @@ -74,3 +75,26 @@ data class SecondInlineObjectGeneration(
@get:JsonProperty("direct")
val direct: String? = null
)

data class ThirdInlineObject(
@param:JsonProperty("generation")
@get:JsonProperty("generation")
@get:Valid
val generation: ThirdInlineObjectGeneration? = null
)

data class ThirdInlineObjectUrls(
@param:JsonProperty("version")
@get:JsonProperty("version")
val version: String? = null
)

data class ThirdInlineObjectGeneration(
@param:JsonProperty("urls")
@get:JsonProperty("urls")
@get:Valid
val urls: List<ThirdInlineObjectUrls>? = null,
@param:JsonProperty("view_name")
@get:JsonProperty("view_name")
val viewName: String? = null
)

0 comments on commit fa13de9

Please sign in to comment.