Skip to content

Commit 3c25de0

Browse files
committed
Fix test for defaultValue for AdditionalProperties fields when using generateAliasAsModel
1 parent 349c850 commit 3c25de0

File tree

140 files changed

+12379
-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.

140 files changed

+12379
-5
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
generatorName: java
2+
outputDir: samples/client/petstore/java/resttemplate-generateAliasAsModel
3+
library: resttemplate
4+
inputSpec: modules/openapi-generator/src/test/resources/3_0/java/generateAliasAsModel.yaml
5+
templateDir: modules/openapi-generator/src/main/resources/Java
6+
generateAliasAsModel: true
7+
additionalProperties:
8+
java8: true
9+
useJakartaEe: true
10+
artifactId: petstore-resttemplate-generateAliasAsModel
11+
hideGenerationTimestamp: "true"
12+
generateConstructorWithAllArgs: true
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
generatorName: spring
2+
outputDir: samples/server/petstore/springboot-generateAliasAsModel
3+
library: spring-boot
4+
inputSpec: modules/openapi-generator/src/test/resources/3_0/java/generateAliasAsModel.yaml
5+
templateDir: modules/openapi-generator/src/main/resources/JavaSpring
6+
generateAliasAsModel: "true"
7+
additionalProperties:
8+
documentationProvider: springfox
9+
useSwaggerUI: false
10+
java8: true
11+
artifactId: spring-boot-generateAliasAsModel
12+
hideGenerationTimestamp: "true"

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,8 +1330,8 @@ public String toDefaultValue(CodegenProperty cp, Schema schema) {
13301330
return null;
13311331
}
13321332

1333-
if (isGenerateAliasAsModel()) {
1334-
// additionalProperties generated
1333+
if (ModelUtils.isGenerateAliasAsModel() && !cp.dataType.contains("Map<")) {
1334+
// Aliased class used as the field name instead of Map<>
13351335
return null;
13361336
}
13371337

modules/openapi-generator/src/test/java/org/openapitools/codegen/java/JavaClientCodegenTest.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3515,7 +3515,9 @@ public void testAdditionalPropertiesWithGenerateAliasAsModelGeneratesCorrectHash
35153515
"AdditionalPropertiesOnlyTypeString additionalPropertiesOnlyTypeString = (AdditionalPropertiesOnlyTypeString) o;",
35163516
"return Objects.equals(this.additionalProperties, additionalPropertiesOnlyTypeString.additionalProperties)");
35173517

3518-
JavaFileAssert.assertThat(files.get("Response.java")).fileContains("AdditionalPropertiesOnlyTypeObject additionalPropertiesOnlyTypeObject;");
3518+
JavaFileAssert.assertThat(files.get("Response.java")).fileContains(
3519+
3520+
"AdditionalPropertiesOnlyTypeObject additionalPropertiesOnlyTypeObject;");
35193521
}
35203522

35213523
@Test
@@ -3555,6 +3557,8 @@ public void testAdditionalPropertiesWithGenerateAliasForMicroProfile() {
35553557
"HashMap<String, Object>",
35563558
"return Objects.hash(name, super.hashCode())");
35573559

3558-
JavaFileAssert.assertThat(files.get("Response.java")).fileContains("AdditionalPropertiesOnlyTypeObject additionalPropertiesOnlyTypeObject;");
3560+
JavaFileAssert.assertThat(files.get("Response.java")).fileContains(
3561+
"Map<String, String> inlineMap = new HashMap<>();",
3562+
"AdditionalPropertiesOnlyTypeObject additionalPropertiesOnlyTypeObject;");
35593563
}
35603564
}

modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5390,7 +5390,8 @@ public void testAdditionalPropertiesWithGenerateAliasAsModelGeneratesCorrectHash
53905390
final CodegenConfigurator configurator = new CodegenConfigurator()
53915391
.setGeneratorName("spring")
53925392
.setGenerateAliasAsModel(true)
5393-
.setInputSpec("src/test/resources/3_0/additionalProperties.yaml")
5393+
//.setInputSpec("src/test/resources/3_0/additionalProperties.yaml")
5394+
.setInputSpec("C:\\dev\\openapi-generator\\modules\\openapi-generator\\src\\test\\resources\\3_0\\petstore-with-fake-endpoints-models-for-testing.yaml")
53945395
.setOutputDir(output.toString().replace("\\", "/"));
53955396

53965397
Map<String, File> files = new DefaultGenerator().opts(configurator.toClientOptInput()).generate()

modules/openapi-generator/src/test/resources/3_0/additionalProperties.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ components:
1717
Response:
1818
type: object
1919
properties:
20+
inlineMap:
21+
type: object
22+
additionalProperties:
23+
type: string
2024
additionalPropertiesOnlyTypeObject:
2125
$ref: '#/components/schemas/AdditionalPropertiesOnlyTypeObject'
2226
additionalPropertiesOnlyTypeString:
Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
openapi: 3.0.0
2+
info:
3+
description: This spec is mainly for testing generateAliasAsModel and
4+
version: 1.0.0
5+
title: generateAliasAsModel
6+
paths:
7+
/fake/additionalProperties-reference:
8+
post:
9+
tags:
10+
- fake
11+
summary: test referenced additionalProperties
12+
description: ''
13+
operationId: testAdditionalPropertiesReference
14+
responses:
15+
'200':
16+
description: successful operation
17+
requestBody:
18+
content:
19+
application/json:
20+
schema:
21+
$ref: '#/components/schemas/AdditionalProperties'
22+
description: request body
23+
required: true
24+
components:
25+
schemas:
26+
AdditionalProperties:
27+
type: object
28+
properties:
29+
additionalPropertiesClassRef:
30+
$ref: "#/components/schemas/AdditionalPropertiesClassRef"
31+
additionalPropertiesClass:
32+
$ref: "#/components/schemas/AdditionalPropertiesClass"
33+
mixed:
34+
$ref: "#/components/schemas/MixedPropertiesAndAdditionalPropertiesClass"
35+
AdditionalPropertiesClassRef:
36+
type: object
37+
properties:
38+
map_string:
39+
$ref: "#/components/schemas/AdditionalPropertiesString"
40+
map_number:
41+
$ref: "#/components/schemas/AdditionalPropertiesNumber"
42+
map_integer:
43+
$ref: "#/components/schemas/AdditionalPropertiesInteger"
44+
map_boolean:
45+
$ref: "#/components/schemas/AdditionalPropertiesBoolean"
46+
anytype_1:
47+
$ref: "#/components/schemas/AnyType1"
48+
AdditionalPropertyClassRef:
49+
type: object
50+
properties:
51+
map_string:
52+
$ref: "#/components/schemas/AdditionalPropertyString"
53+
map_number:
54+
$ref: "#/components/schemas/AdditionalPropertyNumber"
55+
map_integer:
56+
$ref: "#/components/schemas/AdditionalPropertyInteger"
57+
map_boolean:
58+
$ref: "#/components/schemas/AdditionalPropertyBoolean"
59+
map_array_integer:
60+
$ref: "#/components/schemas/AdditionalPropertyArrayInteger"
61+
map_array_anytype:
62+
$ref: "#/components/schemas/AdditionalPropertyArrayAnyType"
63+
map_map_string:
64+
$ref: "#/components/schemas/AdditionalPropertyMapString"
65+
map_map_anytype:
66+
$ref: "#/components/schemas/AdditionalPropertyMapAnyType"
67+
anytype_1:
68+
$ref: "#/components/schemas/AnyType1"
69+
AdditionalPropertyString:
70+
type: object
71+
additionalProperties:
72+
type: string
73+
AdditionalPropertyNumber:
74+
type: object
75+
additionalProperties:
76+
type: number
77+
AdditionalPropertyInteger:
78+
type: object
79+
additionalProperties:
80+
type: integer
81+
AdditionalPropertyBoolean:
82+
type: object
83+
additionalProperties:
84+
type: boolean
85+
AdditionalPropertyArrayInteger:
86+
type: object
87+
additionalProperties:
88+
type: array
89+
items:
90+
type: integer
91+
AdditionalPropertyArrayAnyType:
92+
type: object
93+
additionalProperties:
94+
type: array
95+
items:
96+
type: object
97+
AdditionalPropertyMapString:
98+
type: object
99+
additionalProperties:
100+
type: object
101+
additionalProperties:
102+
type: string
103+
AdditionalPropertyMapAnyType:
104+
type: object
105+
additionalProperties:
106+
type: object
107+
additionalProperties:
108+
type: object
109+
AnyType1:
110+
type: object
111+
AdditionalPropertiesClass:
112+
type: object
113+
properties:
114+
map_string:
115+
type: object
116+
additionalProperties:
117+
type: string
118+
map_number:
119+
type: object
120+
additionalProperties:
121+
type: number
122+
map_integer:
123+
type: object
124+
additionalProperties:
125+
type: integer
126+
map_boolean:
127+
type: object
128+
additionalProperties:
129+
type: boolean
130+
map_array_integer:
131+
type: object
132+
additionalProperties:
133+
type: array
134+
items:
135+
type: integer
136+
map_array_anytype:
137+
type: object
138+
additionalProperties:
139+
type: array
140+
items:
141+
type: object
142+
map_map_string:
143+
type: object
144+
additionalProperties:
145+
type: object
146+
additionalProperties:
147+
type: string
148+
map_map_anytype:
149+
type: object
150+
additionalProperties:
151+
type: object
152+
additionalProperties:
153+
type: object
154+
anytype_1:
155+
type: object
156+
anytype_2: {}
157+
anytype_3:
158+
type: object
159+
properties: {}
160+
AdditionalPropertiesString:
161+
type: object
162+
properties:
163+
name:
164+
type: string
165+
additionalProperties:
166+
type: string
167+
AdditionalPropertiesInteger:
168+
type: object
169+
properties:
170+
name:
171+
type: string
172+
additionalProperties:
173+
type: integer
174+
AdditionalPropertiesNumber:
175+
type: object
176+
properties:
177+
name:
178+
type: string
179+
additionalProperties:
180+
type: number
181+
AdditionalPropertiesBoolean:
182+
type: object
183+
properties:
184+
name:
185+
type: string
186+
additionalProperties:
187+
type: boolean
188+
AdditionalPropertiesArray:
189+
type: object
190+
properties:
191+
name:
192+
type: string
193+
additionalProperties:
194+
type: array
195+
items:
196+
type: object
197+
AdditionalPropertiesObject:
198+
type: object
199+
properties:
200+
name:
201+
type: string
202+
additionalProperties:
203+
type: object
204+
additionalProperties:
205+
type: object
206+
AdditionalPropertiesAnyType:
207+
type: object
208+
properties:
209+
name:
210+
type: string
211+
additionalProperties:
212+
type: object
213+
MixedPropertiesAndAdditionalPropertiesClass:
214+
type: object
215+
properties:
216+
uuid:
217+
type: string
218+
format: uuid
219+
dateTime:
220+
type: string
221+
format: date-time
222+
map:
223+
type: object
224+
additionalProperties:
225+
$ref: "#/components/schemas/Animal"
226+
Animal:
227+
type: object
228+
properties:
229+
className:
230+
type: string
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time
2+
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven
3+
#
4+
# This file is auto-generated by OpenAPI Generator (https://openapi-generator.tech)
5+
6+
name: Java CI with Maven
7+
8+
on:
9+
push:
10+
branches: [ main, master ]
11+
pull_request:
12+
branches: [ main, master ]
13+
14+
jobs:
15+
build:
16+
name: Build generateAliasAsModel
17+
runs-on: ubuntu-latest
18+
strategy:
19+
matrix:
20+
java: [ 17, 21 ]
21+
steps:
22+
- uses: actions/checkout@v4
23+
- name: Set up JDK
24+
uses: actions/setup-java@v4
25+
with:
26+
java-version: ${{ matrix.java }}
27+
distribution: 'temurin'
28+
cache: maven
29+
- name: Build with Maven
30+
run: mvn -B package --no-transfer-progress --file pom.xml
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
*.class
2+
3+
# Mobile Tools for Java (J2ME)
4+
.mtj.tmp/
5+
6+
# Package Files #
7+
*.jar
8+
*.war
9+
*.ear
10+
11+
# exclude jar for gradle wrapper
12+
!gradle/wrapper/*.jar
13+
14+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
15+
hs_err_pid*
16+
17+
# build files
18+
**/target
19+
target
20+
.gradle
21+
build
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# OpenAPI Generator Ignore
2+
# Generated by openapi-generator https://github.com/openapitools/openapi-generator
3+
4+
# Use this file to prevent files from being overwritten by the generator.
5+
# The patterns follow closely to .gitignore or .dockerignore.
6+
7+
# As an example, the C# client generator defines ApiClient.cs.
8+
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
9+
#ApiClient.cs
10+
11+
# You can match any string of characters against a directory, file or extension with a single asterisk (*):
12+
#foo/*/qux
13+
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux
14+
15+
# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
16+
#foo/**/qux
17+
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux
18+
19+
# You can also negate patterns with an exclamation (!).
20+
# For example, you can ignore all files in a docs folder with the file extension .md:
21+
#docs/*.md
22+
# Then explicitly reverse the ignore rule for a single file:
23+
#!docs/README.md

0 commit comments

Comments
 (0)