diff --git a/src/main/kotlin/com/cjbooms/fabrikt/util/KaizenParserExtensions.kt b/src/main/kotlin/com/cjbooms/fabrikt/util/KaizenParserExtensions.kt index 8d15db01..26d8fb08 100644 --- a/src/main/kotlin/com/cjbooms/fabrikt/util/KaizenParserExtensions.kt +++ b/src/main/kotlin/com/cjbooms/fabrikt/util/KaizenParserExtensions.kt @@ -45,7 +45,7 @@ object KaizenParserExtensions { ) private fun Schema.isAggregatedObject(): Boolean = - allOfSchemas?.isNotEmpty() == true || anyOfSchemas?.isNotEmpty() == true + combinedAnyOfAndAllOfSchemas().size > 1 fun Schema.isInlinedTypedAdditionalProperties() = isObjectType() && !isSchemaLess() && Overlay.of(this).pathFromRoot.contains("additionalProperties") @@ -191,6 +191,7 @@ object KaizenParserExtensions { fun Schema.safeName(): String = when { isOneOfPolymorphicTypes() -> this.oneOfSchemas.first().allOfSchemas.first().safeName() + isInlinedAggregationOfExactlyOne() -> combinedAnyOfAndAllOfSchemas().first().safeName() name != null -> name else -> Overlay.of(this).pathFromRoot .splitToSequence("/") @@ -224,6 +225,29 @@ object KaizenParserExtensions { fun Schema.isOneOfSuperInterface() = discriminator != null && discriminator.propertyName != null && oneOfSchemas.isNotEmpty() + private fun Schema.isInlinedAggregationOfExactlyOne() = + combinedAnyOfAndAllOfSchemas().size == 1 && isInlinedPropertySchema() + + private fun Schema.combinedAnyOfAndAllOfSchemas(): List = + (allOfSchemas ?: emptyList()) + (anyOfSchemas ?: emptyList()) + + /** + * The `pathFromRoot` of a property schema ends with + * `/properties/`, so we check if the + * penultimate segment is `properties`. + */ + private fun Schema.isInlinedPropertySchema(): Boolean { + val path = Overlay.of(this).pathFromRoot + val lastSegment = path.lastIndexOf('/') + if (lastSegment != -1) { + val penultimateSegment = path.lastIndexOf('/', lastSegment - 1) + if (penultimateSegment != -1) { + return path.startsWith("/properties/", penultimateSegment) + } + } + return false + } + fun OpenApi3.basePath(): String = servers .firstOrNull() diff --git a/src/test/resources/examples/inlinedAggregatedObjects/models/Models.kt b/src/test/resources/examples/inlinedAggregatedObjects/models/Models.kt index 185f46bd..deecc15b 100644 --- a/src/test/resources/examples/inlinedAggregatedObjects/models/Models.kt +++ b/src/test/resources/examples/inlinedAggregatedObjects/models/Models.kt @@ -16,20 +16,13 @@ public data class Dog( @param:JsonProperty("owner") @get:JsonProperty("owner") @get:Valid - public val owner: DogOwner? = null, + public val owner: Person? = null, @param:JsonProperty("walker") @get:JsonProperty("walker") @get:Valid public val walker: DogWalker? = null, ) -public data class DogOwner( - @param:JsonProperty("name") - @get:JsonProperty("name") - @get:NotNull - public val name: String, -) - public data class DogWalker( @param:JsonProperty("name") @get:JsonProperty("name")