You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hey folks 👋
Firstly great job with the playground, it's incredibly useful and a great time saver for iterations.
I wanted to get your opinion on the following usecase.
Assume that there are two type of Actions- DanceAction and WorkAction. There are equivalently two Locations- Home and Workplace. DanceAction can only be performed at Home, and WorkAction can only be performed at Work.
This can be represented in Kotlin as follows,
sealed class Action {
abstract val location: Location
}
sealed class Location
data object Home : Location()
data object Work : Location()
data class DanceAction(
override val location: Home,
) : Action()
data class WorkAction(
override val location: Work,
) : Action()
Fabrikt currently generates the following code (trimmed imports for brevity),
// Action
package com.example.models
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.EXISTING_PROPERTY,
property = "location",
visible = true,
)
@JsonSubTypes()
public sealed class Action(
public open val location: Location,
)
// Location
package com.example.models
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.EXISTING_PROPERTY,
property = "type",
visible = true,
)
@JsonSubTypes(JsonSubTypes.Type(value = Home::class, name = "Home"),JsonSubTypes.Type(value =
Work::class, name = "Work"))
public sealed class Location() {
public abstract val type: LocationType
}
// LocationType
package com.example.models
public enum class LocationType(
@JsonValue
public val `value`: String,
) {
HOME("Home"),
WORK("Work"),
;
public companion object {
private val mapping: Map<String, LocationType> = entries.associateBy(LocationType::value)
public fun fromValue(`value`: String): LocationType? = mapping[value]
}
}
// Home
package com.example.models
public data class Home(
@get:JsonProperty("type")
@get:NotNull
@param:JsonProperty("type")
override val type: LocationType = LocationType.HOME,
) : Location()
// Work
package com.example.models
public data class Work(
@get:JsonProperty("type")
@get:NotNull
@param:JsonProperty("type")
override val type: LocationType = LocationType.WORK,
) : Location()
// DanceAction
package com.example.models
public data class DanceAction(
@param:JsonProperty("location")
@get:JsonProperty("location")
@get:NotNull
@get:Valid
override val location: Location,
) : Action(location)
// WorkAction
package com.example.models
public data class WorkAction(
@param:JsonProperty("location")
@get:JsonProperty("location")
@get:NotNull
@get:Valid
override val location: Location,
) : Action(location)
Please note the location field in WorkAction being of type Location instead of Work. Do you feel it would be idiomatic to support this?
The text was updated successfully, but these errors were encountered:
I would like @cjbooms to chime in a well, but I can provide my feedback:
I am not sure how, if at all, the standard deals with overriding properties like in this case, but it seems OK to let the specific schema (DanceAction/WorkAction) take precedence over the base schema. My concern would be that we are overlooking other cases where it is not the expected behaviour.
Hey folks 👋
Firstly great job with the playground, it's incredibly useful and a great time saver for iterations.
I wanted to get your opinion on the following usecase.
Assume that there are two type of Actions- DanceAction and WorkAction. There are equivalently two Locations- Home and Workplace. DanceAction can only be performed at Home, and WorkAction can only be performed at Work.
This can be represented in Kotlin as follows,
Given an OpenAPI as follows,
Fabrikt currently generates the following code (trimmed imports for brevity),
Please note the location field in WorkAction being of type
Location
instead ofWork
. Do you feel it would be idiomatic to support this?The text was updated successfully, but these errors were encountered: