Skip to content
This repository was archived by the owner on Dec 25, 2024. It is now read-only.

Commit 9d62134

Browse files
author
Mostafa Aghajani
authored
[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
1 parent d25cdbb commit 9d62134

File tree

5 files changed

+159
-2
lines changed

5 files changed

+159
-2
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
generatorName: kotlin
2+
outputDir: samples/client/petstore/kotlin-default-values-numbers
3+
inputSpec: modules/openapi-generator/src/test/resources/3_0/kotlin/issue13506-defaultValue-numbers.yaml
4+
templateDir: modules/openapi-generator/src/main/resources/kotlin-client
5+
additionalProperties:
6+
artifactId: kotlin-default-values-numbers
7+
serializationLibrary: gson
8+
globalProperties:
9+
models: ""

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractKotlinCodegen.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,6 +1001,20 @@ public void postProcessFile(File file, String fileType) {
10011001
}
10021002
}
10031003

1004+
private String fixNumberValue(String number, Schema p) {
1005+
if (ModelUtils.isFloatSchema(p)) {
1006+
return number + "f";
1007+
} else if (ModelUtils.isDoubleSchema(p)) {
1008+
if (number.contains(".")) {
1009+
return number;
1010+
}
1011+
return number + ".0";
1012+
} else if (ModelUtils.isLongSchema(p)) {
1013+
return number + "L";
1014+
}
1015+
return number;
1016+
}
1017+
10041018
@Override
10051019
public String toDefaultValue(Schema schema) {
10061020
Schema p = ModelUtils.getReferencedSchema(this.openAPI, schema);
@@ -1014,11 +1028,11 @@ public String toDefaultValue(Schema schema) {
10141028
// TODO
10151029
} else if (ModelUtils.isNumberSchema(p)) {
10161030
if (p.getDefault() != null) {
1017-
return p.getDefault().toString();
1031+
return fixNumberValue(p.getDefault().toString(), p);
10181032
}
10191033
} else if (ModelUtils.isIntegerSchema(p)) {
10201034
if (p.getDefault() != null) {
1021-
return p.getDefault().toString();
1035+
return fixNumberValue(p.getDefault().toString(), p);
10221036
}
10231037
} else if (ModelUtils.isURISchema(p)) {
10241038
if (p.getDefault() != null) {
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
openapi: 3.0.0
2+
info:
3+
title: 'Issue X default value number with format'
4+
version: latest
5+
paths:
6+
'/':
7+
get:
8+
operationId: operation
9+
responses:
10+
'200':
11+
description: Success
12+
content:
13+
application/json:
14+
schema:
15+
$ref: '#/components/schemas/ModelWithPropertyHavingDefault'
16+
components:
17+
schemas:
18+
ModelWithPropertyHavingDefault:
19+
properties:
20+
propertyInt:
21+
type: integer
22+
default: 0
23+
format: int32
24+
propertyLong:
25+
type: integer
26+
default: 0
27+
format: int64
28+
propertyFloat1:
29+
type: number
30+
default: 0
31+
format: float
32+
propertyFloat2:
33+
type: number
34+
default: 0.0
35+
format: float
36+
propertyFloat3:
37+
type: number
38+
default: 0.01
39+
format: float
40+
propertyDouble1:
41+
type: number
42+
default: 0
43+
format: double
44+
propertyDouble2:
45+
type: number
46+
default: 0.0
47+
format: double
48+
propertyDouble3:
49+
type: number
50+
default: 0.01
51+
format: double
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
# ModelWithPropertyHavingDefault
3+
4+
## Properties
5+
Name | Type | Description | Notes
6+
------------ | ------------- | ------------- | -------------
7+
**propertyInt** | **kotlin.Int** | | [optional]
8+
**propertyLong** | **kotlin.Long** | | [optional]
9+
**propertyFloat1** | **kotlin.Float** | | [optional]
10+
**propertyFloat2** | **kotlin.Float** | | [optional]
11+
**propertyFloat3** | **kotlin.Float** | | [optional]
12+
**propertyDouble1** | **kotlin.Double** | | [optional]
13+
**propertyDouble2** | **kotlin.Double** | | [optional]
14+
**propertyDouble3** | **kotlin.Double** | | [optional]
15+
16+
17+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* Issue X default value number with format
3+
*
4+
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
5+
*
6+
* The version of the OpenAPI document: latest
7+
*
8+
*
9+
* Please note:
10+
* This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
11+
* Do not edit this file manually.
12+
*/
13+
14+
@file:Suppress(
15+
"ArrayInDataClass",
16+
"EnumEntryName",
17+
"RemoveRedundantQualifierName",
18+
"UnusedImport"
19+
)
20+
21+
package org.openapitools.client.models
22+
23+
24+
import com.google.gson.annotations.SerializedName
25+
26+
/**
27+
*
28+
*
29+
* @param propertyInt
30+
* @param propertyLong
31+
* @param propertyFloat1
32+
* @param propertyFloat2
33+
* @param propertyFloat3
34+
* @param propertyDouble1
35+
* @param propertyDouble2
36+
* @param propertyDouble3
37+
*/
38+
39+
data class ModelWithPropertyHavingDefault (
40+
41+
@SerializedName("propertyInt")
42+
val propertyInt: kotlin.Int? = 0,
43+
44+
@SerializedName("propertyLong")
45+
val propertyLong: kotlin.Long? = 0L,
46+
47+
@SerializedName("propertyFloat1")
48+
val propertyFloat1: kotlin.Float? = 0f,
49+
50+
@SerializedName("propertyFloat2")
51+
val propertyFloat2: kotlin.Float? = 0.0f,
52+
53+
@SerializedName("propertyFloat3")
54+
val propertyFloat3: kotlin.Float? = 0.01f,
55+
56+
@SerializedName("propertyDouble1")
57+
val propertyDouble1: kotlin.Double? = 0.0,
58+
59+
@SerializedName("propertyDouble2")
60+
val propertyDouble2: kotlin.Double? = 0.0,
61+
62+
@SerializedName("propertyDouble3")
63+
val propertyDouble3: kotlin.Double? = 0.01
64+
65+
)
66+

0 commit comments

Comments
 (0)