Skip to content

Commit 66b7420

Browse files
authored
fix(java): default values from allOfs are not correctly definied (#22200)
* fix: extend check for default values of integer and number schema * add testcases to openapi specs * generate java clients with the updated fix
1 parent 9b8ab65 commit 66b7420

File tree

65 files changed

+3848
-5
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+3848
-5
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,20 +1359,20 @@ public String toDefaultValue(CodegenProperty cp, Schema schema) {
13591359

13601360
return String.format(Locale.ROOT, "new %s<>()",
13611361
instantiationTypes().getOrDefault("map", "HashMap"));
1362-
} else if (ModelUtils.isIntegerSchema(schema)) {
1362+
} else if (ModelUtils.isIntegerSchema(schema) || cp.isInteger || cp.isLong) {
13631363
if (schema.getDefault() != null) {
1364-
if (SchemaTypeUtil.INTEGER64_FORMAT.equals(schema.getFormat())) {
1364+
if (SchemaTypeUtil.INTEGER64_FORMAT.equals(schema.getFormat()) || cp.isLong) {
13651365
return schema.getDefault().toString() + "l";
13661366
} else {
13671367
return schema.getDefault().toString();
13681368
}
13691369
}
13701370
return null;
1371-
} else if (ModelUtils.isNumberSchema(schema)) {
1371+
} else if (ModelUtils.isNumberSchema(schema) || cp.isFloat || cp.isDouble) {
13721372
if (schema.getDefault() != null) {
1373-
if (SchemaTypeUtil.FLOAT_FORMAT.equals(schema.getFormat())) {
1373+
if (SchemaTypeUtil.FLOAT_FORMAT.equals(schema.getFormat()) || cp.isFloat) {
13741374
return schema.getDefault().toString() + "f";
1375-
} else if (SchemaTypeUtil.DOUBLE_FORMAT.equals(schema.getFormat())) {
1375+
} else if (SchemaTypeUtil.DOUBLE_FORMAT.equals(schema.getFormat()) || cp.isDouble) {
13761376
return schema.getDefault().toString() + "d";
13771377
} else {
13781378
return "new BigDecimal(\"" + schema.getDefault().toString() + "\")";

modules/openapi-generator/src/test/resources/3_0/java/native/petstore-with-fake-endpoints-models-for-testing-with-http-signature.yaml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2240,3 +2240,39 @@ components:
22402240
deprecated: true
22412241
items:
22422242
$ref: '#/components/schemas/Bar'
2243+
LongId:
2244+
description: "Id as long"
2245+
type: integer
2246+
format: int64
2247+
Weight:
2248+
description: "Weight as float"
2249+
format: float
2250+
type: number
2251+
Height:
2252+
description: "Height as double"
2253+
format: double
2254+
type: number
2255+
AllOfRefToLong:
2256+
description: "Object with allOf ref to long"
2257+
type: object
2258+
properties:
2259+
id:
2260+
allOf:
2261+
- $ref: '#/components/schemas/LongId'
2262+
default: 10
2263+
AllOfRefToFloat:
2264+
description: "Object with allOf ref to float"
2265+
type: object
2266+
properties:
2267+
weight:
2268+
allOf:
2269+
- $ref: '#/components/schemas/Weight'
2270+
default: 7.89
2271+
AllOfRefToDouble:
2272+
description: "Object with allOf ref to double"
2273+
type: object
2274+
properties:
2275+
height:
2276+
allOf:
2277+
- $ref: '#/components/schemas/Height'
2278+
default: 32.1

modules/openapi-generator/src/test/resources/3_0/java/petstore-with-fake-endpoints-models-for-testing-okhttp-gson.yaml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2830,3 +2830,39 @@ components:
28302830
[ "h3", "Header 3" ],
28312831
[ "h4", "Header 4" ]
28322832
]
2833+
LongId:
2834+
description: "Id as long"
2835+
type: integer
2836+
format: int64
2837+
Weight:
2838+
description: "Weight as float"
2839+
format: float
2840+
type: number
2841+
Height:
2842+
description: "Height as double"
2843+
format: double
2844+
type: number
2845+
AllOfRefToLong:
2846+
description: "Object with allOf ref to long"
2847+
type: object
2848+
properties:
2849+
id:
2850+
allOf:
2851+
- $ref: '#/components/schemas/LongId'
2852+
default: 10
2853+
AllOfRefToFloat:
2854+
description: "Object with allOf ref to float"
2855+
type: object
2856+
properties:
2857+
weight:
2858+
allOf:
2859+
- $ref: '#/components/schemas/Weight'
2860+
default: 7.89
2861+
AllOfRefToDouble:
2862+
description: "Object with allOf ref to double"
2863+
type: object
2864+
properties:
2865+
height:
2866+
allOf:
2867+
- $ref: '#/components/schemas/Height'
2868+
default: 32.1

modules/openapi-generator/src/test/resources/3_0/java/petstore-with-fake-endpoints-models-for-testing-with-http-signature.yaml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2231,3 +2231,39 @@ components:
22312231
deprecated: true
22322232
items:
22332233
$ref: '#/components/schemas/Bar'
2234+
LongId:
2235+
description: "Id as long"
2236+
type: integer
2237+
format: int64
2238+
Weight:
2239+
description: "Weight as float"
2240+
format: float
2241+
type: number
2242+
Height:
2243+
description: "Height as double"
2244+
format: double
2245+
type: number
2246+
AllOfRefToLong:
2247+
description: "Object with allOf ref to long"
2248+
type: object
2249+
properties:
2250+
id:
2251+
allOf:
2252+
- $ref: '#/components/schemas/LongId'
2253+
default: 10
2254+
AllOfRefToFloat:
2255+
description: "Object with allOf ref to float"
2256+
type: object
2257+
properties:
2258+
weight:
2259+
allOf:
2260+
- $ref: '#/components/schemas/Weight'
2261+
default: 7.89
2262+
AllOfRefToDouble:
2263+
description: "Object with allOf ref to double"
2264+
type: object
2265+
properties:
2266+
height:
2267+
allOf:
2268+
- $ref: '#/components/schemas/Height'
2269+
default: 32.1

samples/client/petstore/java/jersey3/.openapi-generator/FILES

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ api/openapi.yaml
66
build.gradle
77
build.sbt
88
docs/AdditionalPropertiesClass.md
9+
docs/AllOfRefToDouble.md
10+
docs/AllOfRefToFloat.md
11+
docs/AllOfRefToLong.md
912
docs/Animal.md
1013
docs/AnotherFakeApi.md
1114
docs/Apple.md
@@ -125,6 +128,9 @@ src/main/java/org/openapitools/client/auth/OAuth.java
125128
src/main/java/org/openapitools/client/auth/OAuthFlow.java
126129
src/main/java/org/openapitools/client/model/AbstractOpenApiSchema.java
127130
src/main/java/org/openapitools/client/model/AdditionalPropertiesClass.java
131+
src/main/java/org/openapitools/client/model/AllOfRefToDouble.java
132+
src/main/java/org/openapitools/client/model/AllOfRefToFloat.java
133+
src/main/java/org/openapitools/client/model/AllOfRefToLong.java
128134
src/main/java/org/openapitools/client/model/Animal.java
129135
src/main/java/org/openapitools/client/model/Apple.java
130136
src/main/java/org/openapitools/client/model/AppleReq.java
@@ -199,3 +205,6 @@ src/main/java/org/openapitools/client/model/TriangleInterface.java
199205
src/main/java/org/openapitools/client/model/User.java
200206
src/main/java/org/openapitools/client/model/Whale.java
201207
src/main/java/org/openapitools/client/model/Zebra.java
208+
src/test/java/org/openapitools/client/model/AllOfRefToDoubleTest.java
209+
src/test/java/org/openapitools/client/model/AllOfRefToFloatTest.java
210+
src/test/java/org/openapitools/client/model/AllOfRefToLongTest.java

samples/client/petstore/java/jersey3/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ Class | Method | HTTP request | Description
163163
## Documentation for Models
164164

165165
- [AdditionalPropertiesClass](docs/AdditionalPropertiesClass.md)
166+
- [AllOfRefToDouble](docs/AllOfRefToDouble.md)
167+
- [AllOfRefToFloat](docs/AllOfRefToFloat.md)
168+
- [AllOfRefToLong](docs/AllOfRefToLong.md)
166169
- [Animal](docs/Animal.md)
167170
- [Apple](docs/Apple.md)
168171
- [AppleReq](docs/AppleReq.md)

samples/client/petstore/java/jersey3/api/openapi.yaml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2293,6 +2293,42 @@ components:
22932293
$ref: "#/components/schemas/Bar"
22942294
type: array
22952295
type: object
2296+
LongId:
2297+
description: Id as long
2298+
format: int64
2299+
type: integer
2300+
Weight:
2301+
description: Weight as float
2302+
format: float
2303+
type: number
2304+
Height:
2305+
description: Height as double
2306+
format: double
2307+
type: number
2308+
AllOfRefToLong:
2309+
description: Object with allOf ref to long
2310+
properties:
2311+
id:
2312+
allOf:
2313+
- $ref: "#/components/schemas/LongId"
2314+
default: 10
2315+
type: object
2316+
AllOfRefToFloat:
2317+
description: Object with allOf ref to float
2318+
properties:
2319+
weight:
2320+
allOf:
2321+
- $ref: "#/components/schemas/Weight"
2322+
default: 7.89
2323+
type: object
2324+
AllOfRefToDouble:
2325+
description: Object with allOf ref to double
2326+
properties:
2327+
height:
2328+
allOf:
2329+
- $ref: "#/components/schemas/Height"
2330+
default: 32.1
2331+
type: object
22962332
_foo_get_default_response:
22972333
example:
22982334
string:
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
3+
# AllOfRefToDouble
4+
5+
Object with allOf ref to double
6+
7+
## Properties
8+
9+
| Name | Type | Description | Notes |
10+
|------------ | ------------- | ------------- | -------------|
11+
|**height** | **Double** | Height as double | [optional] |
12+
13+
14+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
3+
# AllOfRefToFloat
4+
5+
Object with allOf ref to float
6+
7+
## Properties
8+
9+
| Name | Type | Description | Notes |
10+
|------------ | ------------- | ------------- | -------------|
11+
|**weight** | **Float** | Weight as float | [optional] |
12+
13+
14+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
3+
# AllOfRefToLong
4+
5+
Object with allOf ref to long
6+
7+
## Properties
8+
9+
| Name | Type | Description | Notes |
10+
|------------ | ------------- | ------------- | -------------|
11+
|**id** | **Long** | Id as long | [optional] |
12+
13+
14+

0 commit comments

Comments
 (0)