Skip to content

Commit 8e0d919

Browse files
authored
Merge pull request #580 from swagger-api/model_named_file
allow models named file and other utils default java classes.
2 parents 7d94b9b + 0c1fd25 commit 8e0d919

File tree

10 files changed

+336
-43
lines changed

10 files changed

+336
-43
lines changed

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

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ public abstract class DefaultCodegenConfig implements CodegenConfig {
171171
protected String ignoreFilePathOverride;
172172
protected boolean useOas2 = false;
173173
protected boolean copyFistAllOfProperties = false;
174+
protected boolean ignoreImportMapping;
174175

175176
public List<CliOption> cliOptions() {
176177
return cliOptions;
@@ -181,6 +182,12 @@ public void processOpts() {
181182
this.setTemplateDir((String) additionalProperties.get(CodegenConstants.TEMPLATE_DIR));
182183
}
183184

185+
if (additionalProperties.get(CodegenConstants.IGNORE_IMPORT_MAPPING_OPTION) != null) {
186+
setIgnoreImportMapping(Boolean.parseBoolean( additionalProperties.get(CodegenConstants.IGNORE_IMPORT_MAPPING_OPTION).toString()));
187+
} else {
188+
setIgnoreImportMapping(defaultIgnoreImportMappingOption());
189+
}
190+
184191
if (additionalProperties.containsKey(CodegenConstants.TEMPLATE_VERSION)) {
185192
this.setTemplateVersion((String) additionalProperties.get(CodegenConstants.TEMPLATE_VERSION));
186193
}
@@ -1717,6 +1724,9 @@ public CodegenProperty fromProperty(String name, Schema propertySchema) {
17171724
CodegenProperty cp = fromProperty("inner", new ObjectSchema());
17181725
updatePropertyForMap(codegenProperty, cp);
17191726
} else {
1727+
if (isObjectSchema(propertySchema)) {
1728+
codegenProperty.getVendorExtensions().put("x-is-object", Boolean.TRUE);
1729+
}
17201730
setNonArrayMapProperty(codegenProperty, type);
17211731
}
17221732
return codegenProperty;
@@ -3147,6 +3157,29 @@ private void addVars(CodegenModel codegenModel, List<CodegenProperty> vars, Map<
31473157
codegenModel.readWriteVars.add(codegenProperty);
31483158
}
31493159
}
3160+
// check if one of the property is a object and has import mapping.
3161+
List<CodegenProperty> modelProperties = vars.stream()
3162+
.filter(codegenProperty -> getBooleanValue(codegenProperty, "x-is-object") && importMapping.containsKey(codegenProperty.baseType))
3163+
.collect(Collectors.toList());
3164+
if (modelProperties == null || modelProperties.isEmpty()) {
3165+
return;
3166+
}
3167+
3168+
for (CodegenProperty modelProperty : modelProperties) {
3169+
List<CodegenProperty> codegenProperties = vars.stream()
3170+
.filter(codegenProperty -> !getBooleanValue(codegenProperty, "x-is-object")
3171+
&& importMapping.containsKey(codegenProperty.baseType)
3172+
&& codegenProperty.baseType.equals(modelProperty.baseType))
3173+
.collect(Collectors.toList());
3174+
if (codegenProperties == null || codegenProperties.isEmpty()) {
3175+
continue;
3176+
}
3177+
for (CodegenProperty codegenProperty : codegenProperties) {
3178+
codegenModel.imports.remove(codegenProperty.baseType);
3179+
codegenProperty.datatype = importMapping.get(codegenProperty.baseType);
3180+
codegenProperty.datatypeWithEnum = codegenProperty.datatype;
3181+
}
3182+
}
31503183
}
31513184

31523185
/**
@@ -4146,7 +4179,10 @@ else if (Parameter.StyleEnum.SPACEDELIMITED.equals(parameter.getStyle())) {
41464179
}
41474180

41484181
public boolean isObjectSchema (Schema schema) {
4149-
if (schema instanceof ObjectSchema ||schema instanceof ComposedSchema) {
4182+
if (schema == null) {
4183+
return false;
4184+
}
4185+
if (schema instanceof ObjectSchema || schema instanceof ComposedSchema) {
41504186
return true;
41514187
}
41524188
if (SchemaTypeUtil.OBJECT_TYPE.equalsIgnoreCase(schema.getType()) && !(schema instanceof MapSchema)) {
@@ -4241,7 +4277,15 @@ public void setUnflattenedOpenAPI(OpenAPI unflattenedOpenAPI) {
42414277
this.unflattenedOpenAPI = unflattenedOpenAPI;
42424278
}
42434279

4244-
public boolean ignoreImportMapping() {
4280+
public boolean getIgnoreImportMapping() {
4281+
return ignoreImportMapping;
4282+
}
4283+
4284+
public void setIgnoreImportMapping(boolean ignoreImportMapping) {
4285+
this.ignoreImportMapping = ignoreImportMapping;
4286+
}
4287+
4288+
public boolean defaultIgnoreImportMappingOption() {
42454289
return false;
42464290
}
42474291
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -600,10 +600,10 @@ public String toParamName(String name) {
600600
public String toModelName(final String name) {
601601
// We need to check if import-mapping has a different model for this class, so we use it
602602
// instead of the auto-generated one.
603-
if (importMapping.containsKey(name)) {
603+
604+
if (!getIgnoreImportMapping() && importMapping.containsKey(name)) {
604605
return importMapping.get(name);
605606
}
606-
607607
final String sanitizedName = sanitizeName(name);
608608

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

14501450
super.setLanguageArguments(languageArguments);
14511451
}
1452+
1453+
@Override
1454+
public boolean defaultIgnoreImportMappingOption() {
1455+
return true;
1456+
}
14521457
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package io.swagger.codegen.v3.generators;
2+
3+
import io.swagger.v3.oas.models.OpenAPI;
4+
import io.swagger.v3.parser.OpenAPIV3Parser;
5+
import io.swagger.v3.parser.core.models.ParseOptions;
6+
import io.swagger.v3.parser.core.models.SwaggerParseResult;
7+
8+
public abstract class AbstractCodegenTest {
9+
10+
protected OpenAPI getOpenAPI(String filePath) {
11+
OpenAPIV3Parser openApiParser = new OpenAPIV3Parser();
12+
ParseOptions options = new ParseOptions();
13+
options.setResolve(true);
14+
options.setFlatten(true);
15+
SwaggerParseResult parseResult = openApiParser.readLocation(filePath, null, options);
16+
17+
return parseResult.getOpenAPI();
18+
}
19+
}

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

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,26 @@
22

33
import io.swagger.codegen.v3.CodegenOperation;
44
import io.swagger.codegen.v3.CodegenResponse;
5+
import io.swagger.codegen.v3.generators.AbstractCodegenTest;
6+
import io.swagger.v3.oas.models.OpenAPI;
57
import io.swagger.v3.oas.models.Operation;
6-
import io.swagger.v3.oas.models.media.ArraySchema;
7-
import io.swagger.v3.oas.models.media.Content;
8-
import io.swagger.v3.oas.models.media.MediaType;
9-
import io.swagger.v3.oas.models.media.ObjectSchema;
10-
import io.swagger.v3.oas.models.media.Schema;
11-
import io.swagger.v3.oas.models.responses.ApiResponse;
12-
import io.swagger.v3.oas.models.responses.ApiResponses;
138
import org.testng.Assert;
149
import org.testng.annotations.Test;
1510

1611
import java.util.Collections;
1712
import java.util.HashMap;
1813
import java.util.Map;
1914

20-
public class JavaCXFClientCodegenTest {
15+
public class JavaCXFClientCodegenTest extends AbstractCodegenTest {
2116

2217
@Test
2318
public void responseWithoutContent() throws Exception {
24-
final Schema listOfPets = new ArraySchema()
25-
.items(new Schema<>().$ref("#/components/schemas/Pet"));
26-
Operation operation = new Operation().responses(new ApiResponses()
27-
.addApiResponse("200", new ApiResponse()
28-
.description("Return a list of pets")
29-
.content(new Content().addMediaType("application/json",
30-
new MediaType().schema(listOfPets))))
31-
.addApiResponse("400", new ApiResponse()
32-
.description("Error")));
33-
final Map<String, Schema> allDefinitions = Collections.singletonMap("Pet", new ObjectSchema());
19+
final OpenAPI openAPI = getOpenAPI("3_0_0/response_without_content.yaml");
20+
final Operation operation = openAPI.getPaths().get("/pets").getGet();
3421

3522
final JavaCXFClientCodegen codegen = new JavaCXFClientCodegen();
36-
final CodegenOperation co = codegen.fromOperation("getAllPets", "GET", operation, allDefinitions);
23+
codegen.preprocessOpenAPI(openAPI);
24+
final CodegenOperation co = codegen.fromOperation("getAllPets", "GET", operation, openAPI.getComponents().getSchemas(), openAPI);
3725

3826
Map<String, Object> objs = new HashMap<>();
3927
objs.put("operations", Collections.singletonMap("operation", Collections.singletonList(co)));

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

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
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.codegen.v3.generators.DefaultCodegenConfig;
11+
import io.swagger.util.Json;
12+
import io.swagger.v3.oas.models.OpenAPI;
813
import io.swagger.v3.oas.models.media.ArraySchema;
914
import io.swagger.v3.oas.models.media.ComposedSchema;
1015
import io.swagger.v3.oas.models.media.Content;
@@ -14,6 +19,9 @@
1419
import io.swagger.v3.oas.models.media.Schema;
1520
import io.swagger.v3.oas.models.media.StringSchema;
1621
import io.swagger.v3.oas.models.parameters.RequestBody;
22+
import io.swagger.v3.parser.OpenAPIV3Parser;
23+
import io.swagger.v3.parser.core.models.ParseOptions;
24+
import io.swagger.v3.parser.core.models.SwaggerParseResult;
1725
import io.swagger.v3.parser.util.SchemaTypeUtil;
1826
import org.testng.Assert;
1927
import org.testng.annotations.Test;
@@ -154,7 +162,7 @@ public void arraysInRequestBody() throws Exception {
154162
Assert.assertEquals(codegenParameter2.description, "A list of list of values");
155163
Assert.assertEquals(codegenParameter2.dataType, "List<List<Integer>>");
156164
Assert.assertEquals(codegenParameter2.baseType, "List");
157-
165+
158166
RequestBody body3 = new RequestBody();
159167
body3.setDescription("A list of points");
160168
body3.setContent(new Content().addMediaType("application/json", new MediaType().schema(new ArraySchema().items(new ObjectSchema().$ref("#/components/schemas/Point")))));
@@ -260,4 +268,100 @@ public void customTemplates() throws Exception {
260268
codegen.processOpts();
261269
Assert.assertEquals(codegen.templateDir(), String.join(File.separator,"user", "custom", "location"));
262270
}
271+
272+
@Test
273+
public void testModelNamedFile() {
274+
final OpenAPI openAPI = getOpenAPI("3_0_0/model_named_file.yaml");
275+
final DefaultCodegenConfig config = new JavaClientCodegen();
276+
config.setIgnoreImportMapping(true);
277+
config.preprocessOpenAPI(openAPI);
278+
279+
final Schema modelFile = openAPI.getComponents().getSchemas().get("File");
280+
final Schema modelSetting = openAPI.getComponents().getSchemas().get("Setting");
281+
282+
final CodegenModel codegenModelFile = config.fromModel("File", modelFile, openAPI.getComponents().getSchemas());
283+
final CodegenModel codegenModelSetting = config.fromModel("Setting", modelSetting, openAPI.getComponents().getSchemas());
284+
285+
Assert.assertEquals(codegenModelFile.name, "File");
286+
Assert.assertEquals(codegenModelSetting.name, "Setting");
287+
288+
final List<CodegenProperty> codegenProperties = codegenModelSetting.getVars();
289+
290+
Assert.assertEquals(codegenProperties.size(), 4);
291+
292+
CodegenProperty fileProperty = codegenProperties.stream().filter(property -> property.name.equals("file")).findAny().get();
293+
294+
Assert.assertEquals(fileProperty.name, "file");
295+
Assert.assertEquals(fileProperty.baseType, "File");
296+
Assert.assertEquals(fileProperty.datatype, "File");
297+
298+
CodegenProperty documentProperty = codegenProperties.stream().filter(property -> property.name.equals("document")).findAny().get();
299+
300+
Assert.assertEquals(documentProperty.name, "document");
301+
Assert.assertEquals(documentProperty.baseType, "File");
302+
Assert.assertEquals(documentProperty.datatype, "java.io.File");
303+
304+
Assert.assertFalse(codegenModelSetting.imports.stream().anyMatch(_import -> _import.equals("java.io.File")));
305+
}
306+
307+
private OpenAPI getOpenAPI(String filePath) {
308+
OpenAPIV3Parser openApiParser = new OpenAPIV3Parser();
309+
ParseOptions options = new ParseOptions();
310+
options.setResolve(true);
311+
options.setFlatten(true);
312+
SwaggerParseResult parseResult = openApiParser.readLocation(filePath, null, options);
313+
314+
return parseResult.getOpenAPI();
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+
365+
366+
263367
}

0 commit comments

Comments
 (0)