Skip to content

Commit c8d4eb4

Browse files
authored
Merge branch 'master' into enum_naming
2 parents 5b12cd3 + 81497c4 commit c8d4eb4

47 files changed

Lines changed: 403 additions & 201 deletions

Some content is hidden

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

pom.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
<groupId>io.swagger.codegen.v3</groupId>
1414
<artifactId>swagger-codegen-generators</artifactId>
15-
<version>1.0.14-SNAPSHOT</version>
15+
<version>1.0.15-SNAPSHOT</version>
1616
<packaging>jar</packaging>
1717

1818
<build>
@@ -246,10 +246,10 @@
246246
</dependency>
247247
</dependencies>
248248
<properties>
249-
<swagger-codegen-version>3.0.14-SNAPSHOT</swagger-codegen-version>
250-
<swagger-parser-version>2.0.16-SNAPSHOT</swagger-parser-version>
251-
<swagger-core-version>2.0.10</swagger-core-version>
252-
<jackson-version>2.9.10</jackson-version>
249+
<swagger-codegen-version>3.0.15-SNAPSHOT</swagger-codegen-version>
250+
<swagger-parser-version>2.0.17-SNAPSHOT</swagger-parser-version>
251+
<swagger-core-version>2.1.1-SNAPSHOT</swagger-core-version>
252+
<jackson-version>2.10.1</jackson-version>
253253
<scala-version>2.11.1</scala-version>
254254
<felix-version>3.3.0</felix-version>
255255
<commons-io-version>2.4</commons-io-version>

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

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ public abstract class DefaultCodegenConfig implements CodegenConfig {
115115
public static final String DEFAULT_TEMPLATE_DIR = "handlebars";
116116

117117
protected OpenAPI openAPI;
118+
protected OpenAPI unflattenedOpenAPI;
118119
protected String inputSpec;
119120
protected String inputURL;
120121
protected String outputFolder = StringUtils.EMPTY;
@@ -236,7 +237,7 @@ public void processOpts() {
236237
// not set in additionalProperties, add value from CodegenConfig in order to use it in templates
237238
additionalProperties.put(CodegenConstants.HIDE_GENERATION_TIMESTAMP, hideGenerationTimestamp);
238239
}
239-
240+
240241
if (additionalProperties.containsKey(CodegenConstants.USE_OAS2)) {
241242
this.setUseOas2(Boolean.valueOf(additionalProperties.get(CodegenConstants.USE_OAS2).toString()));
242243
}
@@ -1099,6 +1100,8 @@ private static String getTypeOfSchema(Schema schema) {
10991100
}
11001101
} else if (schema instanceof MapSchema) {
11011102
return "map";
1103+
} else if (schema instanceof ObjectSchema) {
1104+
return "object";
11021105
} else if (schema instanceof UUIDSchema) {
11031106
return "UUID";
11041107
} else if (schema instanceof StringSchema) {
@@ -1111,6 +1114,9 @@ private static String getTypeOfSchema(Schema schema) {
11111114
if (SchemaTypeUtil.OBJECT_TYPE.equals(schema.getType()) && (hasSchemaProperties(schema) || hasTrueAdditionalProperties(schema))) {
11121115
return "map";
11131116
} else {
1117+
if (schema.getType() == null && schema.getProperties() != null && !schema.getProperties().isEmpty()) {
1118+
return "object";
1119+
}
11141120
return schema.getType();
11151121
}
11161122
}
@@ -1450,21 +1456,21 @@ protected void addAdditionPropertiesToCodeGenModel(CodegenModel codegenModel, Sc
14501456
}
14511457

14521458
protected void addProperties(Map<String, Schema> properties, List<String> required, Schema schema, Map<String, Schema> allSchemas) {
1453-
if(schema instanceof ComposedSchema) {
1454-
ComposedSchema composedSchema = (ComposedSchema) schema;
1455-
if(composedSchema.getAllOf() == null || composedSchema.getAllOf().isEmpty() || composedSchema.getAllOf().size() == 1) {
1456-
return;
1457-
}
1458-
for (int i = 1; i < composedSchema.getAllOf().size(); i++) {
1459-
addProperties(properties, required, composedSchema.getAllOf().get(i), allSchemas);
1460-
}
1461-
return;
1462-
}
14631459
if(StringUtils.isNotBlank(schema.get$ref())) {
14641460
Schema interfaceSchema = allSchemas.get(OpenAPIUtil.getSimpleRef(schema.get$ref()));
14651461
addProperties(properties, required, interfaceSchema, allSchemas);
14661462
return;
14671463
}
1464+
1465+
if(schema instanceof ComposedSchema) {
1466+
ComposedSchema composedSchema = (ComposedSchema) schema;
1467+
if(!(composedSchema.getAllOf() == null || composedSchema.getAllOf().isEmpty() || composedSchema.getAllOf().size() == 1)) {
1468+
for (int i = 1; i < composedSchema.getAllOf().size(); i++) {
1469+
addProperties(properties, required, composedSchema.getAllOf().get(i), allSchemas);
1470+
}
1471+
}
1472+
}
1473+
14681474
if(schema.getProperties() != null) {
14691475
properties.putAll(schema.getProperties());
14701476
}
@@ -2217,7 +2223,7 @@ public int compare(CodegenParameter one, CodegenParameter another) {
22172223
codegenOperation.pathParams = addHasMore(pathParams);
22182224
codegenOperation.queryParams = addHasMore(queryParams);
22192225
codegenOperation.headerParams = addHasMore(headerParams);
2220-
// op.cookieParams = cookieParams;
2226+
codegenOperation.cookieParams = addHasMore(cookieParams);
22212227
codegenOperation.formParams = addHasMore(formParams);
22222228
codegenOperation.requiredParams = addHasMore(requiredParams);
22232229
codegenOperation.externalDocs = operation.getExternalDocs();
@@ -3852,7 +3858,7 @@ public String getIgnoreFilePathOverride() {
38523858
public void setIgnoreFilePathOverride(final String ignoreFileOverride) {
38533859
this.ignoreFilePathOverride = ignoreFileOverride;
38543860
}
3855-
3861+
38563862
public void setUseOas2(boolean useOas2) {
38573863
this.useOas2 = useOas2;
38583864
}
@@ -4222,4 +4228,14 @@ private void addParemeters(CodegenContent codegenContent, List<CodegenParameter>
42224228
protected void setParameterNullable(CodegenParameter parameter, CodegenProperty property) {
42234229
parameter.nullable = property.nullable;
42244230
}
4231+
4232+
@Override
4233+
public boolean needsUnflattenedSpec() {
4234+
return false;
4235+
}
4236+
4237+
@Override
4238+
public void setUnflattenedOpenAPI(OpenAPI unflattenedOpenAPI) {
4239+
this.unflattenedOpenAPI = unflattenedOpenAPI;
4240+
}
42254241
}

src/main/java/io/swagger/codegen/v3/generators/dotnet/AbstractCSharpCodegen.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public AbstractCSharpCodegen() {
108108
// set "client" as a reserved word to avoid conflicts with IO.Swagger.Client
109109
// this is a workaround and can be removed if c# api client is updated to use
110110
// fully qualified name
111-
"Client", "client", "parameter",
111+
"Client", "client", "parameter", "File",
112112
// local variable names in API methods (endpoints)
113113
"localVarPath", "localVarPathParams", "localVarQueryParams", "localVarHeaderParams",
114114
"localVarFormParams", "localVarFileParams", "localVarStatusCode", "localVarResponse",
@@ -903,6 +903,14 @@ public void setInterfacePrefix(final String interfacePrefix) {
903903
this.interfacePrefix = interfacePrefix;
904904
}
905905

906+
public CodegenModel fromModel(String name, Schema schema, Map<String, Schema> allDefinitions) {
907+
final CodegenModel codegenModel = super.fromModel(name, schema, allDefinitions);
908+
if (typeMapping.containsKey(name.toLowerCase()) && isReservedWord(name.toLowerCase())) {
909+
typeMapping.remove(name.toLowerCase());
910+
}
911+
return codegenModel;
912+
}
913+
906914
@Override
907915
public String toEnumValue(String value, String datatype) {
908916
// C# only supports enums as literals for int, int?, long, long?, byte, and byte?. All else must be treated as strings.

src/main/java/io/swagger/codegen/v3/generators/dotnet/CSharpClientCodegen.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ public CodegenModel fromModel(String name, Schema schema, Map<String, Schema> al
516516
if (allDefinitions != null && codegenModel != null && codegenModel.parent != null) {
517517
final Schema parentModel = allDefinitions.get(toModelName(codegenModel.parent));
518518
if (parentModel != null) {
519-
final CodegenModel parentCodegenModel = super.fromModel(codegenModel.parent, parentModel);
519+
final CodegenModel parentCodegenModel = super.fromModel(codegenModel.parent, parentModel, allDefinitions);
520520
boolean hasEnums = getBooleanValue(codegenModel, HAS_ENUMS_EXT_NAME);
521521
if (hasEnums) {
522522
codegenModel = this.reconcileInlineEnums(codegenModel, parentCodegenModel);
@@ -821,4 +821,4 @@ public Mustache.Compiler processCompiler(Mustache.Compiler compiler) {
821821
// To avoid unexpected behaviors when options are passed programmatically such as { "supportsAsync": "" }
822822
return super.processCompiler(compiler).emptyStringIsFalse(true);
823823
}
824-
}
824+
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ public JavaClientCodegen() {
8383
cliOptions.add(CliOption.newBoolean(USE_GZIP_FEATURE, "Send gzip-encoded requests"));
8484
cliOptions.add(CliOption.newBoolean(USE_RUNTIME_EXCEPTION, "Use RuntimeException instead of Exception"));
8585

86-
supportedLibraries.put("jersey1", "HTTP client: Jersey client 1.19.4. JSON processing: Jackson 2.9.10. Enable Java6 support using '-DsupportJava6=true'. Enable gzip request encoding using '-DuseGzipFeature=true'.");
87-
supportedLibraries.put("feign", "HTTP client: OpenFeign 9.4.0. JSON processing: Jackson 2.9.10");
88-
supportedLibraries.put("jersey2", "HTTP client: Jersey client 2.25.1. JSON processing: Jackson 2.9.10");
86+
supportedLibraries.put("jersey1", "HTTP client: Jersey client 1.19.4. JSON processing: Jackson 2.10.1. Enable Java6 support using '-DsupportJava6=true'. Enable gzip request encoding using '-DuseGzipFeature=true'.");
87+
supportedLibraries.put("feign", "HTTP client: OpenFeign 9.4.0. JSON processing: Jackson 2.10.1");
88+
supportedLibraries.put("jersey2", "HTTP client: Jersey client 2.26. JSON processing: Jackson 2.10.1");
8989
supportedLibraries.put("okhttp-gson", "HTTP client: OkHttp 2.7.5. JSON processing: Gson 2.8.1. Enable Parcelable models on Android using '-DparcelableModel=true'. Enable gzip request encoding using '-DuseGzipFeature=true'.");
9090
supportedLibraries.put(RETROFIT_1, "HTTP client: OkHttp 2.7.5. JSON processing: Gson 2.3.1 (Retrofit 1.9.0). IMPORTANT NOTE: retrofit1.x is no longer actively maintained so please upgrade to 'retrofit2' instead.");
9191
supportedLibraries.put(RETROFIT_2, "HTTP client: OkHttp 3.8.0. JSON processing: Gson 2.6.1 (Retrofit 2.3.0). Enable the RxJava adapter using '-DuseRxJava[2]=true'. (RxJava 1.x or 2.x)");

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ public class SpringCodegen extends AbstractJavaCodegen implements BeanValidation
6060
public static final String SPRING_BOOT_VERSION = "springBootVersion";
6161
public static final String SPRING_BOOT_VERSION_2 = "springBootV2";
6262

63+
public static final String THROWS_EXCEPTION = "throwsException";
64+
6365
protected String title = "swagger-petstore";
6466
protected String configPackage = "io.swagger.configuration";
6567
protected String basePackage = "io.swagger";
@@ -78,6 +80,7 @@ public class SpringCodegen extends AbstractJavaCodegen implements BeanValidation
7880
protected boolean openFeign = false;
7981
protected boolean defaultInterfaces = true;
8082
protected String springBootVersion = "1.5.22.RELEASE";
83+
protected boolean throwsException = false;
8184

8285
public SpringCodegen() {
8386
super();
@@ -109,6 +112,7 @@ public SpringCodegen() {
109112
"Use Optional container for optional parameters"));
110113
cliOptions.add(CliOption.newBoolean(TARGET_OPENFEIGN,"Generate for usage with OpenFeign (instead of feign)"));
111114
cliOptions.add(CliOption.newBoolean(DEFAULT_INTERFACES, "Generate default implementations for interfaces").defaultValue("true"));
115+
cliOptions.add(CliOption.newBoolean(THROWS_EXCEPTION, "Throws Exception in operation methods").defaultValue("false"));
112116

113117
supportedLibraries.put(DEFAULT_LIBRARY, "Spring-boot Server application using the SpringFox integration.");
114118
supportedLibraries.put(SPRING_MVC_LIBRARY, "Spring-MVC Server application using the SpringFox integration.");
@@ -205,6 +209,11 @@ public void processOpts() {
205209
this.setInterfaceOnly(Boolean.valueOf(additionalProperties.get(INTERFACE_ONLY).toString()));
206210
}
207211

212+
if (additionalProperties.containsKey(THROWS_EXCEPTION)) {
213+
this.setThrowsException(Boolean.valueOf(additionalProperties.get(THROWS_EXCEPTION).toString()));
214+
additionalProperties.put(THROWS_EXCEPTION, throwsException);
215+
}
216+
208217
if (additionalProperties.containsKey(DELEGATE_PATTERN)) {
209218
this.setDelegatePattern(Boolean.valueOf(additionalProperties.get(DELEGATE_PATTERN).toString()));
210219
}
@@ -821,4 +830,8 @@ public void setOpenFeign(boolean openFeign) {
821830
public void setDefaultInterfaces(boolean defaultInterfaces) {
822831
this.defaultInterfaces = defaultInterfaces;
823832
}
833+
834+
public void setThrowsException(boolean throwsException) {
835+
this.throwsException = throwsException;
836+
}
824837
}

src/main/java/io/swagger/codegen/v3/generators/openapi/OpenAPIGenerator.java

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package io.swagger.codegen.v3.generators.openapi;
22

33
import io.swagger.codegen.v3.CliOption;
4-
import io.swagger.codegen.v3.CodegenConstants;
54
import io.swagger.codegen.v3.CodegenType;
65
import io.swagger.codegen.v3.SupportingFile;
76
import io.swagger.codegen.v3.generators.DefaultCodegenConfig;
@@ -20,21 +19,34 @@ public class OpenAPIGenerator extends DefaultCodegenConfig {
2019
private static final Logger LOGGER = LoggerFactory.getLogger(OpenAPIGenerator.class);
2120

2221
public static final String OUTPUT_NAME = "outputFile";
22+
public static final String FLATTEN_SPEC = "flattenSpec";
2323

2424
public static final String OPENAPI_FILENAME_DEFAULT_JSON = "openapi.json";
2525

26-
protected String outputFile = OPENAPI_FILENAME_DEFAULT_JSON;
26+
private String outputFile = OPENAPI_FILENAME_DEFAULT_JSON;
27+
28+
protected boolean flattenSpec = true;
2729

2830
public OpenAPIGenerator() {
2931
super();
3032
outputFolder = "generated-code/openapi";
3133

3234
cliOptions.add(new CliOption(OUTPUT_NAME,
3335
"output filename")
34-
.defaultValue(OPENAPI_FILENAME_DEFAULT_JSON));
36+
.defaultValue(getOutputFile()));
37+
38+
cliOptions.add(new CliOption(FLATTEN_SPEC,
39+
"flatten the spec by moving all inline complex schema to components, and add a ref in element",
40+
"boolean")
41+
.defaultValue(Boolean.TRUE.toString()));
42+
3543
supportingFiles.add(new SupportingFile("README.md", "", "README.md"));
3644
}
3745

46+
protected String getOutputFile() {
47+
return outputFile;
48+
}
49+
3850
@Override
3951
public CodegenType getTag() {
4052
return CodegenType.DOCUMENTATION;
@@ -53,7 +65,12 @@ public String getHelp() {
5365
@Override
5466
public void preprocessOpenAPI(OpenAPI openAPI) {
5567
super.preprocessOpenAPI(openAPI);
56-
String outputString = Json.pretty(openAPI);
68+
final String outputString;
69+
if (flattenSpec) {
70+
outputString = Json.pretty(openAPI);
71+
} else {
72+
outputString = Json.pretty(this.unflattenedOpenAPI);
73+
}
5774

5875
try {
5976
String outputFile = outputFolder + File.separator + this.outputFile;
@@ -73,6 +90,16 @@ public void processOpts() {
7390
if (additionalProperties.containsKey(OUTPUT_NAME) && !StringUtils.isBlank((String) additionalProperties.get(OUTPUT_NAME))) {
7491
setOutputFile((String) additionalProperties.get(OUTPUT_NAME));
7592
}
93+
94+
if (additionalProperties
95+
.containsKey(FLATTEN_SPEC) &&
96+
(
97+
!(additionalProperties.get(FLATTEN_SPEC) instanceof String) ||
98+
!StringUtils.isBlank((String) additionalProperties.get(FLATTEN_SPEC))
99+
)
100+
) {
101+
this.flattenSpec = Boolean.valueOf(additionalProperties.get(FLATTEN_SPEC).toString());
102+
}
76103
}
77104

78105
public void setOutputFile(String outputFile) {
@@ -105,4 +132,9 @@ public String escapeUnsafeCharacters(String input) {
105132
protected void setTemplateEngine() {
106133
templateEngine = new HandlebarTemplateEngine(this);
107134
}
135+
136+
@Override
137+
public boolean needsUnflattenedSpec() {
138+
return true;
139+
}
108140
}

0 commit comments

Comments
 (0)