Skip to content

Update StaticHtmlGenerator to allow snake_case property names #2612

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

Merged
merged 7 commits into from
Apr 12, 2019
Merged
Changes from 4 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
@@ -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";
@@ -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()));
Original file line number Diff line number Diff line change
@@ -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;
@@ -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.

}
Original file line number Diff line number Diff line change
@@ -701,7 +701,7 @@ components:
wrapped: true
items:
type: string
tags:
tags:
Copy link
Member

Choose a reason for hiding this comment

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

What about removing this trailing space as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done and pushed.

type: array
xml:
name: tag