Skip to content

Commit 1151b39

Browse files
author
Kunal Singh
committed
Merge branch 'master' of https://github.com/swagger-api/swagger-codegen into fix-singleton-query-parameter
2 parents a9cfd26 + dbd0a4b commit 1151b39

13 files changed

+213
-38
lines changed

modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenConstants.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ public static enum ENUM_PROPERTY_NAMING_TYPE {camelCase, PascalCase, snake_case,
214214
public static final String GENERATE_PROPERTY_CHANGED = "generatePropertyChanged";
215215
public static final String GENERATE_PROPERTY_CHANGED_DESC = "Specifies that models support raising property changed events.";
216216

217+
public static final String PRESERVE_COMMENT_NEWLINES = "preserveNewlinesInComments";
218+
217219
public static final String NON_PUBLIC_API = "nonPublicApi";
218220
public static final String NON_PUBLIC_API_DESC = "Generates code with reduced access modifiers; allows embedding elsewhere without exposing non-public API calls to consumers.";
219221

modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1383,7 +1383,7 @@ public CodegenModel fromModel(String name, Model model, Map<String, Model> allDe
13831383
allRequired = null;
13841384
}
13851385
// parent model
1386-
RefModel parent = (RefModel) composed.getParent();
1386+
Model parent = (Model) composed.getParent();
13871387

13881388
// interfaces (intermediate models)
13891389
if (composed.getInterfaces() != null) {
@@ -1416,16 +1416,31 @@ public CodegenModel fromModel(String name, Model model, Map<String, Model> allDe
14161416
}
14171417

14181418
if (parent != null) {
1419-
final String parentRef = parent.getSimpleRef();
1420-
m.parentSchema = parentRef;
1421-
m.parent = toModelName(parent.getSimpleRef());
1422-
addImport(m, m.parent);
1423-
if (allDefinitions != null) {
1424-
final Model parentModel = allDefinitions.get(m.parentSchema);
1425-
if (supportsInheritance) {
1426-
addProperties(allProperties, allRequired, parentModel, allDefinitions);
1427-
} else {
1428-
addProperties(properties, required, parentModel, allDefinitions);
1419+
String parentName = null;
1420+
if(parent instanceof RefModel) {
1421+
parentName = ((RefModel)parent).getSimpleRef();
1422+
}
1423+
else {
1424+
if(parent instanceof ModelImpl) {
1425+
// check for a title
1426+
ModelImpl parentImpl = (ModelImpl) parent;
1427+
if(StringUtils.isNotBlank(parentImpl.getTitle())) {
1428+
parentName = parentImpl.getTitle();
1429+
}
1430+
}
1431+
}
1432+
1433+
if(parentName != null) {
1434+
m.parentSchema = parentName;
1435+
m.parent = toModelName(parentName);
1436+
addImport(m, m.parent);
1437+
if (allDefinitions != null) {
1438+
final Model parentModel = allDefinitions.get(m.parentSchema);
1439+
if (supportsInheritance) {
1440+
addProperties(allProperties, allRequired, parentModel, allDefinitions);
1441+
} else {
1442+
addProperties(properties, required, parentModel, allDefinitions);
1443+
}
14291444
}
14301445
}
14311446
}

modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -367,11 +367,15 @@ private Model getParent(Model model) {
367367
Map<String, Model> modelMap = new HashMap<String, Model>();
368368
modelMap.put(name, model);
369369
Map<String, Object> models = processModels(config, modelMap, definitions);
370-
models.put("classname", config.toModelName(name));
371-
models.putAll(config.additionalProperties());
372-
allProcessedModels.put(name, models);
370+
if (models != null) {
371+
models.put("classname", config.toModelName(name));
372+
models.putAll(config.additionalProperties());
373+
allProcessedModels.put(name, models);
374+
}
373375
} catch (Exception e) {
374-
throw new RuntimeException("Could not process model '" + name + "'" + ".Please make sure that your schema is correct!", e);
376+
String message = "Could not process model '" + name + "'" + ". Please make sure that your schema is correct!";
377+
LOGGER.error(message, e);
378+
throw new RuntimeException(message, e);
375379
}
376380
}
377381

@@ -1014,6 +1018,16 @@ private Map<String, Object> processModels(CodegenConfig config, Map<String, Mode
10141018
Set<String> allImports = new LinkedHashSet<String>();
10151019
for (String key : definitions.keySet()) {
10161020
Model mm = definitions.get(key);
1021+
if(mm.getVendorExtensions() != null && mm.getVendorExtensions().containsKey("x-codegen-ignore")) {
1022+
// skip this model
1023+
LOGGER.debug("skipping model " + key);
1024+
return null;
1025+
}
1026+
else if(mm.getVendorExtensions() != null && mm.getVendorExtensions().containsKey("x-codegen-import-mapping")) {
1027+
String codegenImport = mm.getVendorExtensions().get("x-codegen-import-mapping").toString();
1028+
config.importMapping().put(key, codegenImport);
1029+
allImports.add(codegenImport);
1030+
}
10171031
CodegenModel cm = config.fromModel(key, mm, allDefinitions);
10181032
Map<String, Object> mo = new HashMap<String, Object>();
10191033
mo.put("model", cm);

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AbstractCSharpCodegen.java

Lines changed: 107 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,26 @@
55
import io.swagger.codegen.*;
66
import io.swagger.codegen.mustache.*;
77
import io.swagger.codegen.utils.ModelUtils;
8+
import io.swagger.models.Model;
9+
import io.swagger.models.ModelImpl;
10+
import io.swagger.models.Operation;
11+
import io.swagger.models.Path;
12+
import io.swagger.models.Response;
13+
import io.swagger.models.Swagger;
14+
import io.swagger.models.parameters.Parameter;
815
import io.swagger.models.properties.*;
916
import org.apache.commons.lang3.StringUtils;
1017
import org.slf4j.Logger;
1118
import org.slf4j.LoggerFactory;
1219

1320
import java.io.File;
14-
import java.util.*;
21+
import java.util.ArrayList;
22+
import java.util.Arrays;
23+
import java.util.HashMap;
24+
import java.util.HashSet;
25+
import java.util.List;
26+
import java.util.Map;
27+
import java.util.Set;
1528

1629
public abstract class AbstractCSharpCodegen extends DefaultCodegen implements CodegenConfig {
1730

@@ -22,6 +35,7 @@ public abstract class AbstractCSharpCodegen extends DefaultCodegen implements Co
2235
protected boolean useDateTimeOffsetFlag = false;
2336
protected boolean useCollection = false;
2437
protected boolean returnICollection = false;
38+
protected boolean preserveNewLines = false;
2539
protected boolean netCoreProjectFileFlag = false;
2640

2741
protected String modelPropertyNaming = CodegenConstants.MODEL_PROPERTY_NAMING_TYPE.PascalCase.name();
@@ -78,7 +92,7 @@ public AbstractCSharpCodegen() {
7892
// fully qualified name
7993
"Client", "client", "parameter",
8094
// local variable names in API methods (endpoints)
81-
"localVarPath", "localVarPathParams", "localVarQueryParams", "localVarHeaderParams",
95+
"localVarPath", "localVarPathParams", "localVarQueryParams", "localVarHeaderParams",
8296
"localVarFormParams", "localVarFileParams", "localVarStatusCode", "localVarResponse",
8397
"localVarPostBody", "localVarHttpHeaderAccepts", "localVarHttpHeaderAccept",
8498
"localVarHttpContentTypes", "localVarHttpContentType",
@@ -210,14 +224,14 @@ public void processOpts() {
210224
if (additionalProperties.containsKey(CodegenConstants.INVOKER_PACKAGE)) {
211225
LOGGER.warn(String.format("%s is not used by C# generators. Please use %s", CodegenConstants.INVOKER_PACKAGE, CodegenConstants.PACKAGE_NAME));
212226
}
213-
227+
214228
// {{packageTitle}}
215229
if (additionalProperties.containsKey(CodegenConstants.PACKAGE_TITLE)) {
216230
setPackageTitle((String) additionalProperties.get(CodegenConstants.PACKAGE_TITLE));
217231
} else {
218232
additionalProperties.put(CodegenConstants.PACKAGE_TITLE, packageTitle);
219233
}
220-
234+
221235
// {{packageProductName}}
222236
if (additionalProperties.containsKey(CodegenConstants.PACKAGE_PRODUCTNAME)) {
223237
setPackageProductName((String) additionalProperties.get(CodegenConstants.PACKAGE_PRODUCTNAME));
@@ -231,14 +245,14 @@ public void processOpts() {
231245
} else {
232246
additionalProperties.put(CodegenConstants.PACKAGE_DESCRIPTION, packageDescription);
233247
}
234-
248+
235249
// {{packageCompany}}
236250
if (additionalProperties.containsKey(CodegenConstants.PACKAGE_COMPANY)) {
237251
setPackageCompany((String) additionalProperties.get(CodegenConstants.PACKAGE_COMPANY));
238252
} else {
239253
additionalProperties.put(CodegenConstants.PACKAGE_COMPANY, packageCompany);
240254
}
241-
255+
242256
// {{packageCopyright}}
243257
if (additionalProperties.containsKey(CodegenConstants.PACKAGE_COPYRIGHT)) {
244258
setPackageCopyright((String) additionalProperties.get(CodegenConstants.PACKAGE_COPYRIGHT));
@@ -252,7 +266,7 @@ public void processOpts() {
252266
} else {
253267
additionalProperties.put(CodegenConstants.PACKAGE_AUTHORS, packageAuthors);
254268
}
255-
269+
256270
// {{useDateTimeOffset}}
257271
if (additionalProperties.containsKey(CodegenConstants.USE_DATETIME_OFFSET)) {
258272
useDateTimeOffset(convertPropertyToBooleanAndWriteBack(CodegenConstants.USE_DATETIME_OFFSET));
@@ -284,6 +298,10 @@ public void processOpts() {
284298
additionalProperties.put(CodegenConstants.NETCORE_PROJECT_FILE, netCoreProjectFileFlag);
285299
}
286300

301+
if (additionalProperties.containsKey(CodegenConstants.PRESERVE_COMMENT_NEWLINES)) {
302+
setPreserveNewLines(Boolean.valueOf(additionalProperties.get(CodegenConstants.PRESERVE_COMMENT_NEWLINES).toString()));
303+
}
304+
287305
if (additionalProperties.containsKey(CodegenConstants.INTERFACE_PREFIX)) {
288306
String useInterfacePrefix = additionalProperties.get(CodegenConstants.INTERFACE_PREFIX).toString();
289307
if("false".equals(useInterfacePrefix.toLowerCase())) {
@@ -626,10 +644,10 @@ public String toParamName(String name) {
626644
}
627645

628646
return name;
629-
}
647+
}
630648

631649
@Override
632-
public String escapeReservedWord(String name) {
650+
public String escapeReservedWord(String name) {
633651
if(this.reservedWordsMappings().containsKey(name)) {
634652
return this.reservedWordsMappings().get(name);
635653
}
@@ -864,31 +882,31 @@ public void setPackageName(String packageName) {
864882
public void setPackageVersion(String packageVersion) {
865883
this.packageVersion = packageVersion;
866884
}
867-
885+
868886
public void setPackageTitle(String packageTitle) {
869887
this.packageTitle = packageTitle;
870888
}
871-
889+
872890
public void setPackageProductName(String packageProductName) {
873891
this.packageProductName = packageProductName;
874892
}
875893

876894
public void setPackageDescription(String packageDescription) {
877895
this.packageDescription = packageDescription;
878896
}
879-
897+
880898
public void setPackageCompany(String packageCompany) {
881899
this.packageCompany = packageCompany;
882900
}
883-
901+
884902
public void setPackageCopyright(String packageCopyright) {
885903
this.packageCopyright = packageCopyright;
886904
}
887905

888906
public void setPackageAuthors(String packageAuthors) {
889907
this.packageAuthors = packageAuthors;
890908
}
891-
909+
892910
public void setSourceFolder(String sourceFolder) {
893911
this.sourceFolder = sourceFolder;
894912
}
@@ -948,6 +966,75 @@ public String testPackageName() {
948966
return this.packageName + ".Test";
949967
}
950968

969+
public boolean isPreserveNewLines() {
970+
return preserveNewLines;
971+
}
972+
973+
public void setPreserveNewLines(boolean preserveNewLines) {
974+
this.preserveNewLines = preserveNewLines;
975+
}
976+
977+
@Override
978+
public void preprocessSwagger(Swagger swagger) {
979+
if (this.preserveNewLines) {
980+
if (swagger.getDefinitions() != null) {
981+
for (String name : swagger.getDefinitions().keySet()) {
982+
Model model = swagger.getDefinitions().get(name);
983+
if (StringUtils.isNotBlank(model.getDescription())) {
984+
model.setDescription(preserveNewlines(model.getDescription(), 1));
985+
}
986+
if (model instanceof ModelImpl) {
987+
ModelImpl impl = (ModelImpl) model;
988+
if (impl.getProperties() != null) {
989+
for (String propertyName : impl.getProperties().keySet()) {
990+
Property property = impl.getProperties().get(propertyName);
991+
if (StringUtils.isNotBlank(property.getDescription())) {
992+
property.setDescription(preserveNewlines(property.getDescription(), 2));
993+
}
994+
}
995+
}
996+
}
997+
}
998+
}
999+
for (String pathname : swagger.getPaths().keySet()) {
1000+
Path path = swagger.getPaths().get(pathname);
1001+
for (Operation op : path.getOperations()) {
1002+
if (StringUtils.isNotBlank(op.getDescription())) {
1003+
op.setDescription(preserveNewlines(op.getDescription(), 2));
1004+
}
1005+
if (StringUtils.isNotBlank(op.getSummary())) {
1006+
op.setSummary(preserveNewlines(op.getSummary(), 2));
1007+
}
1008+
if (op.getParameters() != null) {
1009+
for (Parameter param : op.getParameters()) {
1010+
if (StringUtils.isNotBlank(param.getDescription())) {
1011+
param.setDescription(preserveNewlines(param.getDescription(), 2));
1012+
}
1013+
}
1014+
}
1015+
if (op.getResponses() != null) {
1016+
for (String responseCode : op.getResponses().keySet()) {
1017+
Response response = op.getResponses().get(responseCode);
1018+
1019+
if (StringUtils.isNotBlank(response.getDescription())) {
1020+
response.setDescription(preserveNewlines(response.getDescription(), 2));
1021+
}
1022+
}
1023+
}
1024+
}
1025+
}
1026+
}
1027+
}
1028+
1029+
public String preserveNewlines(String input, int tabstops) {
1030+
if (tabstops == 1) {
1031+
return input.replaceAll("\\n", "~~N1");
1032+
} else {
1033+
// assume 2 tabstops
1034+
return input.replaceAll("\\n", "~~N2");
1035+
}
1036+
}
1037+
9511038
@Override
9521039
public String escapeQuotationMark(String input) {
9531040
// remove " to avoid code injection
@@ -956,6 +1043,11 @@ public String escapeQuotationMark(String input) {
9561043

9571044
@Override
9581045
public String escapeUnsafeCharacters(String input) {
959-
return input.replace("*/", "*_/").replace("/*", "/_*").replace("--", "- -");
1046+
String intermediate = input.replace("*/", "*_/").replace("/*", "/_*").replace("--", "- -");
1047+
1048+
intermediate = intermediate.replaceAll("~~N1", "\n /// ");
1049+
intermediate = intermediate.replaceAll("~~N2", "\n /// ");
1050+
1051+
return intermediate;
9601052
}
9611053
}

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AspNetCoreServerCodegen.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ public AspNetCoreServerCodegen() {
5959
CodegenConstants.SOURCE_FOLDER_DESC,
6060
sourceFolder);
6161

62+
addOption(CodegenConstants.PRESERVE_COMMENT_NEWLINES,
63+
"Preserve newlines in comments",
64+
String.valueOf(this.preserveNewLines));
65+
6266
// CLI Switches
6367
addSwitch(CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG,
6468
CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG_DESC,

0 commit comments

Comments
 (0)