[Kotlin] Fix wrong default value is generated for non-integer numbers (#13507)

* ISSUE-13506 fix number value for default values when they are number

* ISSUE-13506 test and sample update
This commit is contained in:
Mostafa Aghajani 2022-09-23 16:02:49 +02:00 committed by GitHub
parent d25cdbb2ce
commit 9d621342e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 159 additions and 2 deletions

View File

@ -0,0 +1,9 @@
generatorName: kotlin
outputDir: samples/client/petstore/kotlin-default-values-numbers
inputSpec: modules/openapi-generator/src/test/resources/3_0/kotlin/issue13506-defaultValue-numbers.yaml
templateDir: modules/openapi-generator/src/main/resources/kotlin-client
additionalProperties:
artifactId: kotlin-default-values-numbers
serializationLibrary: gson
globalProperties:
models: ""

View File

@ -1001,6 +1001,20 @@ public abstract class AbstractKotlinCodegen extends DefaultCodegen implements Co
}
}
private String fixNumberValue(String number, Schema p) {
if (ModelUtils.isFloatSchema(p)) {
return number + "f";
} else if (ModelUtils.isDoubleSchema(p)) {
if (number.contains(".")) {
return number;
}
return number + ".0";
} else if (ModelUtils.isLongSchema(p)) {
return number + "L";
}
return number;
}
@Override
public String toDefaultValue(Schema schema) {
Schema p = ModelUtils.getReferencedSchema(this.openAPI, schema);
@ -1014,11 +1028,11 @@ public abstract class AbstractKotlinCodegen extends DefaultCodegen implements Co
// TODO
} else if (ModelUtils.isNumberSchema(p)) {
if (p.getDefault() != null) {
return p.getDefault().toString();
return fixNumberValue(p.getDefault().toString(), p);
}
} else if (ModelUtils.isIntegerSchema(p)) {
if (p.getDefault() != null) {
return p.getDefault().toString();
return fixNumberValue(p.getDefault().toString(), p);
}
} else if (ModelUtils.isURISchema(p)) {
if (p.getDefault() != null) {

View File

@ -0,0 +1,51 @@
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

View File

@ -0,0 +1,17 @@
# ModelWithPropertyHavingDefault
## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**propertyInt** | **kotlin.Int** | | [optional]
**propertyLong** | **kotlin.Long** | | [optional]
**propertyFloat1** | **kotlin.Float** | | [optional]
**propertyFloat2** | **kotlin.Float** | | [optional]
**propertyFloat3** | **kotlin.Float** | | [optional]
**propertyDouble1** | **kotlin.Double** | | [optional]
**propertyDouble2** | **kotlin.Double** | | [optional]
**propertyDouble3** | **kotlin.Double** | | [optional]

View File

@ -0,0 +1,66 @@
/**
* Issue X default value number with format
*
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
*
* The version of the OpenAPI document: latest
*
*
* Please note:
* This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* Do not edit this file manually.
*/
@file:Suppress(
"ArrayInDataClass",
"EnumEntryName",
"RemoveRedundantQualifierName",
"UnusedImport"
)
package org.openapitools.client.models
import com.google.gson.annotations.SerializedName
/**
*
*
* @param propertyInt
* @param propertyLong
* @param propertyFloat1
* @param propertyFloat2
* @param propertyFloat3
* @param propertyDouble1
* @param propertyDouble2
* @param propertyDouble3
*/
data class ModelWithPropertyHavingDefault (
@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
)