Skip to content

Commit 60ed4a3

Browse files
committed
allow models named file and other utils default java classes.
1 parent 758c3e1 commit 60ed4a3

File tree

4 files changed

+193
-7
lines changed

4 files changed

+193
-7
lines changed

src/main/java/io/swagger/codegen/v3/generators/DefaultCodegenConfig.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1717,6 +1717,9 @@ public CodegenProperty fromProperty(String name, Schema propertySchema) {
17171717
CodegenProperty cp = fromProperty("inner", new ObjectSchema());
17181718
updatePropertyForMap(codegenProperty, cp);
17191719
} else {
1720+
if (isObjectSchema(propertySchema)) {
1721+
codegenProperty.getVendorExtensions().put("x-is-object", Boolean.TRUE);
1722+
}
17201723
setNonArrayMapProperty(codegenProperty, type);
17211724
}
17221725
return codegenProperty;
@@ -3147,6 +3150,26 @@ private void addVars(CodegenModel codegenModel, List<CodegenProperty> vars, Map<
31473150
codegenModel.readWriteVars.add(codegenProperty);
31483151
}
31493152
}
3153+
// check if one of the property is a object and has import mapping.
3154+
List<CodegenProperty> modelProperties = vars.stream()
3155+
.filter(codegenProperty -> getBooleanValue(codegenProperty, "x-is-object") && importMapping.containsKey(codegenProperty.baseType))
3156+
.collect(Collectors.toList());
3157+
3158+
for (CodegenProperty modelProperty : modelProperties) {
3159+
List<CodegenProperty> codegenProperties = vars.stream()
3160+
.filter(codegenProperty -> !getBooleanValue(codegenProperty, "x-is-object")
3161+
&& importMapping.containsKey(codegenProperty.baseType)
3162+
&& codegenProperty.baseType.equals(modelProperty.baseType))
3163+
.collect(Collectors.toList());
3164+
if (codegenProperties == null || codegenProperties.isEmpty()) {
3165+
continue;
3166+
}
3167+
for (CodegenProperty codegenProperty : codegenProperties) {
3168+
codegenModel.imports.remove(codegenProperty.baseType);
3169+
codegenProperty.datatype = importMapping.get(codegenProperty.baseType);
3170+
codegenProperty.datatypeWithEnum = codegenProperty.datatype;
3171+
}
3172+
}
31503173
}
31513174

31523175
/**

src/main/java/io/swagger/codegen/v3/generators/java/AbstractJavaCodegen.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -598,12 +598,6 @@ public String toParamName(String name) {
598598

599599
@Override
600600
public String toModelName(final String name) {
601-
// We need to check if import-mapping has a different model for this class, so we use it
602-
// instead of the auto-generated one.
603-
if (importMapping.containsKey(name)) {
604-
return importMapping.get(name);
605-
}
606-
607601
final String sanitizedName = sanitizeName(name);
608602

609603
String nameWithPrefixSuffix = sanitizedName;
@@ -1449,4 +1443,9 @@ public void setLanguageArguments(List<CodegenArgument> languageArguments) {
14491443

14501444
super.setLanguageArguments(languageArguments);
14511445
}
1446+
1447+
@Override
1448+
public boolean ignoreImportMapping() {
1449+
return true;
1450+
}
14521451
}

src/test/java/io/swagger/codegen/v3/generators/java/JavaClientCodegenTest.java

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package io.swagger.codegen.v3.generators.java;
22

3+
import io.swagger.codegen.v3.CodegenConfig;
34
import io.swagger.codegen.v3.CodegenConstants;
45
import io.swagger.codegen.v3.CodegenModel;
56
import io.swagger.codegen.v3.CodegenModelFactory;
67
import io.swagger.codegen.v3.CodegenModelType;
78
import io.swagger.codegen.v3.CodegenParameter;
9+
import io.swagger.codegen.v3.CodegenProperty;
10+
import io.swagger.util.Json;
11+
import io.swagger.v3.oas.models.OpenAPI;
812
import io.swagger.v3.oas.models.media.ArraySchema;
913
import io.swagger.v3.oas.models.media.ComposedSchema;
1014
import io.swagger.v3.oas.models.media.Content;
@@ -14,6 +18,9 @@
1418
import io.swagger.v3.oas.models.media.Schema;
1519
import io.swagger.v3.oas.models.media.StringSchema;
1620
import io.swagger.v3.oas.models.parameters.RequestBody;
21+
import io.swagger.v3.parser.OpenAPIV3Parser;
22+
import io.swagger.v3.parser.core.models.ParseOptions;
23+
import io.swagger.v3.parser.core.models.SwaggerParseResult;
1724
import io.swagger.v3.parser.util.SchemaTypeUtil;
1825
import org.testng.Assert;
1926
import org.testng.annotations.Test;
@@ -154,7 +161,7 @@ public void arraysInRequestBody() throws Exception {
154161
Assert.assertEquals(codegenParameter2.description, "A list of list of values");
155162
Assert.assertEquals(codegenParameter2.dataType, "List<List<Integer>>");
156163
Assert.assertEquals(codegenParameter2.baseType, "List");
157-
164+
158165
RequestBody body3 = new RequestBody();
159166
body3.setDescription("A list of points");
160167
body3.setContent(new Content().addMediaType("application/json", new MediaType().schema(new ArraySchema().items(new ObjectSchema().$ref("#/components/schemas/Point")))));
@@ -260,4 +267,99 @@ public void customTemplates() throws Exception {
260267
codegen.processOpts();
261268
Assert.assertEquals(codegen.templateDir(), String.join(File.separator,"user", "custom", "location"));
262269
}
270+
271+
@Test
272+
public void testModelNamedFile() {
273+
final OpenAPI openAPI = getOpenAPI("3_0_0/model_named_file.yaml");
274+
final CodegenConfig config = new JavaClientCodegen();
275+
config.preprocessOpenAPI(openAPI);
276+
277+
final Schema modelFile = openAPI.getComponents().getSchemas().get("File");
278+
final Schema modelSetting = openAPI.getComponents().getSchemas().get("Setting");
279+
280+
final CodegenModel codegenModelFile = config.fromModel("File", modelFile, openAPI.getComponents().getSchemas());
281+
final CodegenModel codegenModelSetting = config.fromModel("Setting", modelSetting, openAPI.getComponents().getSchemas());
282+
283+
Assert.assertEquals(codegenModelFile.name, "File");
284+
Assert.assertEquals(codegenModelSetting.name, "Setting");
285+
286+
final List<CodegenProperty> codegenProperties = codegenModelSetting.getVars();
287+
288+
Assert.assertEquals(codegenProperties.size(), 2);
289+
290+
CodegenProperty fileProperty = codegenProperties.stream().filter(property -> property.name.equals("file")).findAny().get();
291+
292+
Assert.assertEquals(fileProperty.name, "file");
293+
Assert.assertEquals(fileProperty.baseType, "File");
294+
Assert.assertEquals(fileProperty.datatype, "File");
295+
296+
CodegenProperty documentProperty = codegenProperties.stream().filter(property -> property.name.equals("document")).findAny().get();
297+
298+
Assert.assertEquals(documentProperty.name, "document");
299+
Assert.assertEquals(documentProperty.baseType, "File");
300+
Assert.assertEquals(documentProperty.datatype, "java.io.File");
301+
302+
Assert.assertFalse(codegenModelSetting.imports.stream().anyMatch(_import -> _import.equals("java.io.File")));
303+
}
304+
305+
private OpenAPI getOpenAPI(String filePath) {
306+
OpenAPIV3Parser openApiParser = new OpenAPIV3Parser();
307+
ParseOptions options = new ParseOptions();
308+
options.setResolve(true);
309+
options.setFlatten(true);
310+
SwaggerParseResult parseResult = openApiParser.readLocation(filePath, null, options);
311+
312+
return parseResult.getOpenAPI();
313+
}
314+
315+
316+
317+
318+
319+
320+
321+
322+
323+
324+
325+
326+
327+
328+
329+
330+
331+
332+
333+
334+
335+
336+
337+
338+
339+
340+
341+
342+
343+
344+
345+
346+
347+
348+
349+
350+
351+
352+
353+
354+
355+
356+
357+
358+
359+
360+
361+
362+
363+
364+
263365
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
openapi: 3.0.0
2+
servers:
3+
- description: SwaggerHub API Auto Mocking
4+
url: /
5+
info:
6+
description: This is a simple API
7+
version: "1.0.0"
8+
title: Simple Inventory API
9+
10+
paths:
11+
/inventory:
12+
post:
13+
tags:
14+
- Inventory
15+
summary: adds an inventory item
16+
operationId: addInventory
17+
description: Adds an item to the system
18+
responses:
19+
'201':
20+
description: item created
21+
'400':
22+
description: 'invalid input, object invalid'
23+
'409':
24+
description: an existing item already exists
25+
requestBody:
26+
content:
27+
application/json:
28+
schema:
29+
$ref: '#/components/schemas/Setting'
30+
description: Inventory item to add
31+
components:
32+
schemas:
33+
Archive:
34+
type: string
35+
format: binary
36+
37+
File:
38+
description: File.
39+
type: object
40+
properties:
41+
prop1:
42+
type: string
43+
44+
Date:
45+
type: object
46+
properties:
47+
place:
48+
type: string
49+
50+
Setting:
51+
description: Setting.
52+
properties:
53+
file:
54+
$ref: '#/components/schemas/File'
55+
date:
56+
$ref: '#/components/schemas/Date'
57+
document:
58+
type: string
59+
format: binary
60+
time:
61+
type: string
62+
format: date-time

0 commit comments

Comments
 (0)