Skip to content
Merged
Show file tree
Hide file tree
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 @@ -29,6 +29,8 @@

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";
Expand Down Expand Up @@ -176,6 +178,19 @@ public String toModelName(final String name) {
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()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
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;
Expand Down Expand Up @@ -54,4 +56,18 @@ public void testSpecWithoutSchema() throws Exception {
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");
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@farrez Thanks for adding the test case. Can the issue be also resolved by using baseName instead of name in the mustache templates as baseName should store the original property name defined in the spec.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that would also have worked.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌 I'll make the change with another PR.

}