icerockdev / moko-network

Network components with codegeneration of rest api for mobile (android & ios) Kotlin Multiplatform development
https://moko.icerock.dev
Apache License 2.0
151 stars 29 forks source link

Add option `enumFallbackNull = true` to support `coerceInputValues` for serialization of enums #124

Closed Alex009 closed 3 years ago

Alex009 commented 3 years ago

For cases where enum in responses of server can change enum values and we should not fail all parsing we need to implement support of https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/json.md#coercing-input-values for it we add into gradle plugin spec configuration new variable - enumFallbackNull: Boolean, and if it true we should generate for all enums default value (if spec already have default value - use it. if no default value - use null).

as result we got support of successful serialization of response:

{
    "type": "unknown"
}

for scheme:

components:
  schemas:
    Color:
      type: string
      enum:
        - black
        - white
        - red
        - green
        - blue

with generated schema:

@Serializable
data class Color(val type: ColorType? = null)

and json config:

Json { coerceInputValues = true }