Skip to content

Commit

Permalink
Fix inline enum detection in arrays. (#134)
Browse files Browse the repository at this point in the history
All enums inside arrays were being treated as inlined definitions, even when they are not.
  • Loading branch information
cjbooms authored May 19, 2022
1 parent f4399f8 commit b22bca6
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import com.cjbooms.fabrikt.model.SchemaInfo
import com.cjbooms.fabrikt.model.SourceApi
import com.cjbooms.fabrikt.util.KaizenParserExtensions.getSuperType
import com.cjbooms.fabrikt.util.KaizenParserExtensions.isComplexTypedAdditionalProperties
import com.cjbooms.fabrikt.util.KaizenParserExtensions.isEnumDefinition
import com.cjbooms.fabrikt.util.KaizenParserExtensions.isInlinedEnumDefinition
import com.cjbooms.fabrikt.util.KaizenParserExtensions.isInlinedObjectDefinition
import com.cjbooms.fabrikt.util.KaizenParserExtensions.isInlinedTypedAdditionalProperties
import com.cjbooms.fabrikt.util.KaizenParserExtensions.isOneOfPolymorphicTypes
Expand Down Expand Up @@ -221,7 +221,7 @@ class JacksonModelGenerator(
it.name.toModelClassName(enclosingModelName), props
)
}
items.isEnumDefinition() ->
items.isInlinedEnumDefinition() ->
setOf(
buildEnumClass(
KotlinTypeInfo.from(items, "items", enclosingModelName) as KotlinTypeInfo.Enum
Expand Down
5 changes: 2 additions & 3 deletions src/main/kotlin/com/cjbooms/fabrikt/model/PropertyInfo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ 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.isSimpleMapDefinition
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
import com.cjbooms.fabrikt.util.KaizenParserExtensions.isSimpleMapDefinition
import com.cjbooms.fabrikt.util.KaizenParserExtensions.safeType
import com.cjbooms.fabrikt.util.KaizenParserExtensions.toModelClassName
import com.cjbooms.fabrikt.util.NormalisedString.camelCase
Expand Down Expand Up @@ -72,7 +71,7 @@ sealed class PropertyInfo {
property.value,
settings.markAsInherited,
this,
if (property.value.isInlinedArrayDefinition() || property.value.itemsSchema.isEnumDefinition())
if (property.value.isInlinedArrayDefinition() || property.value.itemsSchema.isInlinedEnumDefinition())
enclosingSchema
else null
)
Expand Down
9 changes: 8 additions & 1 deletion src/test/resources/examples/enumExamples/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ components:
enum_ref:
$ref: '#/components/schemas/EnumObject'
extensible_enum_ref:
$ref: '#/components/schemas/ExtensibleEnumObject'
$ref: '#/components/schemas/ExtensibleEnumObject'
list_enums:
$ref: '#/components/schemas/ListEnums'

EnumObject:
type: string
Expand All @@ -51,5 +53,10 @@ components:
- application/json
- application/x.some-type+json
- application/x.some-other-type+json;version=2

ListEnums:
type: array
items:
$ref: '#/components/schemas/ContentType'


5 changes: 4 additions & 1 deletion src/test/resources/examples/enumExamples/models/Models.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ data class EnumHolder(
val enumRef: EnumObject? = null,
@param:JsonProperty("extensible_enum_ref")
@get:JsonProperty("extensible_enum_ref")
val extensibleEnumRef: ExtensibleEnumObject? = null
val extensibleEnumRef: ExtensibleEnumObject? = null,
@param:JsonProperty("list_enums")
@get:JsonProperty("list_enums")
val listEnums: List<ContentType>? = null
)

enum class EnumHolderArrayOfEnums(
Expand Down

0 comments on commit b22bca6

Please sign in to comment.