Fix issue 1340 - StaticHtmlGenerator to allow snake_case property names (#2612)

* Updated StaticHtmlGenerator to it overrides the toVarName(String name) method in DefaultCodegen and prevents underscores from being converted to the word Underscore

* adding model propery with a snake_case name

* Reverting change to base petstore.yaml file

* Trying to trigger another CI build

* Trying to trigger another CI build

* Removing trailing space
This commit is contained in:
Shane Farrell 2019-04-12 11:38:35 -05:00 committed by William Cheng
parent 5fbb812d65
commit 687b5aabf3
2 changed files with 31 additions and 0 deletions

View File

@ -29,6 +29,8 @@ import org.openapitools.codegen.utils.ModelUtils;
import java.util.*;
import static org.openapitools.codegen.utils.StringUtils.escape;
public class StaticHtmlGenerator extends DefaultCodegen implements CodegenConfig {
protected String invokerPackage = "org.openapitools.client";
protected String groupId = "org.openapitools";
@ -176,6 +178,19 @@ public class StaticHtmlGenerator extends DefaultCodegen implements CodegenConfig
return name;
}
// DefaultCodegen converts snake_case property names to snakeUnderscorecase
// but for static HTML, we want to preserve snake_case names
@Override
public String toVarName(String name) {
if (reservedWords.contains(name)) {
return escapeReservedWord(name);
} else if (((CharSequence) name).chars().anyMatch(character -> specialCharReplacements.keySet().contains("" + ((char) character)))) {
return escape(name, specialCharReplacements, Arrays.asList("_"), null);
} else {
return name;
}
}
public void preprocessOpenAPI(OpenAPI openAPI) {
Info info = openAPI.getInfo();
info.setDescription(toHtml(info.getDescription()));

View File

@ -23,6 +23,8 @@ import io.swagger.v3.oas.models.media.ObjectSchema;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.media.StringSchema;
import org.openapitools.codegen.CodegenModel;
import org.openapitools.codegen.CodegenProperty;
import org.openapitools.codegen.DefaultCodegen;
import org.openapitools.codegen.TestUtils;
import org.openapitools.codegen.languages.StaticHtmlGenerator;
import org.testng.Assert;
@ -54,4 +56,18 @@ public class StaticHtmlGeneratorTest {
Assert.assertEquals(openAPI.getInfo().getTitle(), "ping test");
}
@Test(description = "ensure that snake_case propery names wont be converted to snakeUnderscorecase")
public void testFromPropertyWithUnderscores() {
final Schema schema = new Schema()
.description("a sample model with property containing an underscore")
.addProperties("favorite_food", new StringSchema());
final OpenAPI openAPI = TestUtils.createOpenAPIWithOneSchema("UnderscoreTest", schema);
final DefaultCodegen codegen = new StaticHtmlGenerator();
codegen.setOpenAPI(openAPI);
CodegenProperty property = codegen.fromProperty("favorite_food", (Schema) openAPI.getComponents().getSchemas().get("UnderscoreTest").getProperties().get("favorite_food"));
Assert.assertEquals(property.baseName, "favorite_food");
Assert.assertEquals(property.name, "favorite_food");
}
}