Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[C#] Support newlines in comments in various sections #8472

Merged
merged 1 commit into from
Jul 24, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
import io.swagger.codegen.*;
import io.swagger.codegen.mustache.*;
import io.swagger.codegen.utils.ModelUtils;
import io.swagger.models.Model;
import io.swagger.models.ModelImpl;
import io.swagger.models.Operation;
import io.swagger.models.Path;
import io.swagger.models.Response;
import io.swagger.models.Swagger;
import io.swagger.models.parameters.Parameter;
import io.swagger.models.properties.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
Expand Down Expand Up @@ -972,20 +976,63 @@ public void setPreserveNewLines(boolean preserveNewLines) {

@Override
public void preprocessSwagger(Swagger swagger) {
if(this.preserveNewLines) {
for(String pathname: swagger.getPaths().keySet()) {
if (this.preserveNewLines) {
if (swagger.getDefinitions() != null) {
for (String name : swagger.getDefinitions().keySet()) {
Model model = swagger.getDefinitions().get(name);
if (StringUtils.isNotBlank(model.getDescription())) {
model.setDescription(preserveNewlines(model.getDescription(), 1));
}
if (model instanceof ModelImpl) {
ModelImpl impl = (ModelImpl) model;
if (impl.getProperties() != null) {
for (String propertyName : impl.getProperties().keySet()) {
Property property = impl.getProperties().get(propertyName);
if (StringUtils.isNotBlank(property.getDescription())) {
property.setDescription(preserveNewlines(property.getDescription(), 2));
}
}
}
}
}
}
for (String pathname : swagger.getPaths().keySet()) {
Path path = swagger.getPaths().get(pathname);
for (Operation op : path.getOperations()) {
if (op.getDescription() != null) {
op.setDescription(preservation(op.getDescription()));
if (StringUtils.isNotBlank(op.getDescription())) {
op.setDescription(preserveNewlines(op.getDescription(), 2));
}
if (StringUtils.isNotBlank(op.getSummary())) {
op.setSummary(preserveNewlines(op.getSummary(), 2));
}
if (op.getParameters() != null) {
for (Parameter param : op.getParameters()) {
if (StringUtils.isNotBlank(param.getDescription())) {
param.setDescription(preserveNewlines(param.getDescription(), 2));
}
}
}
if (op.getResponses() != null) {
for (String responseCode : op.getResponses().keySet()) {
Response response = op.getResponses().get(responseCode);

if (StringUtils.isNotBlank(response.getDescription())) {
response.setDescription(preserveNewlines(response.getDescription(), 2));
}
}
}
}
}
}
}

public String preservation(String input) {
return input.replaceAll("\\n", "~~N");
public String preserveNewlines(String input, int tabstops) {
if (tabstops == 1) {
return input.replaceAll("\\n", "~~N1");
} else {
// assume 2 tabstops
return input.replaceAll("\\n", "~~N2");
}
}

@Override
Expand All @@ -998,7 +1045,8 @@ public String escapeQuotationMark(String input) {
public String escapeUnsafeCharacters(String input) {
String intermediate = input.replace("*/", "*_/").replace("/*", "/_*").replace("--", "- -");

intermediate = intermediate.replaceAll("~~N", "\n /// ");
intermediate = intermediate.replaceAll("~~N1", "\n /// ");
intermediate = intermediate.replaceAll("~~N2", "\n /// ");

return intermediate;
}
Expand Down