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

Reintroduce special handling for inlined aggregation of 1. #266

Merged
merged 1 commit into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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("/")
Expand Down Expand Up @@ -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<Schema> =
(allOfSchemas ?: emptyList()) + (anyOfSchemas ?: emptyList())

/**
* The `pathFromRoot` of a property schema ends with
* `/properties/<name of property>`, 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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down