[BUG] [Kotlin] Wrong default value is generated for non-integer numbers
Created by: aghajani
Bug Report Checklist
-
Have you provided a full/minimal spec to reproduce the issue? -
Have you validated the input using an OpenAPI validator (example)? - [ x Have you tested with the latest master to confirm the issue still exists?
-
Have you searched for related issues/PRs? -
What's the actual output vs expected output?
Description
If the model has fields with
- type
number
and formatfloat
ordouble
- type
integer
and formatint64
then the generated models for kotlin will have syntax errors. Samples below
openapi-generator version
6.1.1
OpenAPI declaration file content or url
openapi: 3.0.0
info:
title: 'Issue X default value number with format'
version: latest
paths:
'/':
get:
operationId: operation
responses:
'200':
description: Success
content:
application/json:
schema:
$ref: '#/components/schemas/ModelWithPropertyHavingDefault'
components:
schemas:
ModelWithPropertyHavingDefault:
properties:
propertyInt:
type: integer
default: 0
format: int32
propertyLong:
type: integer
default: 0
format: int64
propertyFloat1:
type: number
default: 0
format: float
propertyFloat2:
type: number
default: 0.0
format: float
propertyFloat3:
type: number
default: 0.01
format: float
propertyDouble1:
type: number
default: 0
format: double
propertyDouble2:
type: number
default: 0.0
format: double
propertyDouble3:
type: number
default: 0.01
format: double
Generation Details
Steps to reproduce
Try to generate kotlin (client) models, and the output will have:
@SerializedName("propertyLong")
val propertyLong: kotlin.Long? = 0,
@SerializedName("propertyFloat1")
val propertyFloat1: kotlin.Float? = 0,
@SerializedName("propertyFloat2")
val propertyFloat2: kotlin.Float? = 0,
@SerializedName("propertyFloat3")
val propertyFloat3: kotlin.Float? = 0.01,
@SerializedName("propertyDouble1")
val propertyDouble1: kotlin.Double? = 0,
@SerializedName("propertyDouble2")
val propertyDouble2: kotlin.Double? = 0,
which is clearly invalid in Kotlin.
Expected output:
@SerializedName("propertyInt")
val propertyInt: kotlin.Int? = 0,
@SerializedName("propertyLong")
val propertyLong: kotlin.Long? = 0L,
@SerializedName("propertyFloat1")
val propertyFloat1: kotlin.Float? = 0f,
@SerializedName("propertyFloat2")
val propertyFloat2: kotlin.Float? = 0.0f,
@SerializedName("propertyFloat3")
val propertyFloat3: kotlin.Float? = 0.01f,
@SerializedName("propertyDouble1")
val propertyDouble1: kotlin.Double? = 0.0,
@SerializedName("propertyDouble2")
val propertyDouble2: kotlin.Double? = 0.0,
@SerializedName("propertyDouble3")
val propertyDouble3: kotlin.Double? = 0.01
Suggest a fix
The fix should consider the type and shape the output default value (better to be in abstract Kotlin generator)