Skip to content

Commit

Permalink
Issue with default value for additional property (oneOf) (#270)
Browse files Browse the repository at this point in the history
* Add extra property to test spec for discriminatedOneOf

* Fix issue where non-discriminator property is treated as discriminator
  • Loading branch information
ulrikandersen authored Mar 6, 2024
1 parent 61af512 commit cabeb0a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ object KaizenParserExtensions {
fun Schema.isDiscriminatorProperty(api: OpenApi3, prop: Map.Entry<String, Schema>): Boolean =
discriminator?.propertyName == prop.key ||
findOneOfSuperInterface(api.schemas.values.toList()).any { oneOf ->
oneOf.discriminator?.mappings?.values?.any { it.endsWith("/$name") } ?: false
oneOf.discriminator?.propertyName == prop.key
&& oneOf.discriminator?.mappings?.values?.any { it.endsWith("/$name") } ?: false
}

fun Schema.findOneOfSuperInterface(allSchemas: List<Schema>): Set<Schema> {
Expand Down
6 changes: 6 additions & 0 deletions src/test/resources/examples/discriminatedOneOf/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ components:
type: object
required:
- status
- mode
properties:
status:
$ref: '#/components/schemas/Status'
mode:
type: string
enum:
- mode1
- mode2
18 changes: 18 additions & 0 deletions src/test/resources/examples/discriminatedOneOf/models/Models.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,30 @@ public data class StateA(
) : State

public data class StateB(
@get:JsonProperty("mode")
@get:NotNull
public val mode: StateBMode,
@get:JsonProperty("status")
@get:NotNull
@param:JsonProperty("status")
public val status: Status = Status.B,
) : State

public enum class StateBMode(
@JsonValue
public val `value`: String,
) {
MODE1("mode1"),
MODE2("mode2"),
;

public companion object {
private val mapping: Map<String, StateBMode> = values().associateBy(StateBMode::value)

public fun fromValue(`value`: String): StateBMode? = mapping[value]
}
}

public enum class Status(
@JsonValue
public val `value`: String,
Expand Down

0 comments on commit cabeb0a

Please sign in to comment.