Skip to content

[typescript-angular] Fix typescript model kebab-cased filenames #5085

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 1 commit into from
Jan 24, 2020
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 @@ -40,7 +40,7 @@ public static String underscore(final String word) {
* @return The dashized version of the word, e.g. "my-name"
*/
public static String dashize(String word) {
return underscore(word).replaceAll("[_ ]", "-");
return underscore(word).replaceAll("[_ ]+", "-");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.swagger.v3.oas.models.PathItem;
import io.swagger.v3.oas.models.media.ComposedSchema;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.media.StringSchema;
import io.swagger.v3.oas.models.responses.ApiResponse;
import io.swagger.v3.oas.models.responses.ApiResponses;
import org.openapitools.codegen.CodegenOperation;
Expand Down Expand Up @@ -132,4 +133,24 @@ public void testSchema() {
Assert.assertEquals(schemaType, "SchemaOne | SchemaTwo | SchemaThree");
}

@Test
public void testKebabCasedModelFilenames() {
TypeScriptAngularClientCodegen codegen = new TypeScriptAngularClientCodegen();
codegen.additionalProperties().put(TypeScriptAngularClientCodegen.FILE_NAMING, "kebab-case");
codegen.processOpts();

final String modelName = "FooResponse__links";
final Schema schema = new Schema()
.name(modelName)
.description("an inline model with name previously prefixed with underscore")
.addRequiredItem("self")
.addProperties("self", new StringSchema());

OpenAPI openAPI = TestUtils.createOpenAPIWithOneSchema("test", schema);
codegen.setOpenAPI(openAPI);

Assert.assertEquals(codegen.toModelImport(modelName), "model/foo-response-links");
Assert.assertEquals(codegen.toModelFilename(modelName), "./foo-response-links");

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.media.*;
import io.swagger.v3.parser.util.SchemaTypeUtil;
import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.CodegenModel;
import org.openapitools.codegen.CodegenProperty;
import org.openapitools.codegen.DefaultCodegen;
Expand Down Expand Up @@ -304,4 +305,23 @@ public void beginDecimalNameTest() {
Assert.assertFalse(property.isContainer);
}

@Test(description = "convert an inline model that originally had a name prefixed with an underscore")
public void inlineModelWithUnderscoreNameTest() {
// Originally parent model "FooResponse" with inline model called "_links". The InlineModelResolver resolves
// that to "FooResponse__links" (double underscore)
final Schema schema = new Schema()
.description("an inline model with name previously prefixed with underscore")
.addRequiredItem("self")
.addProperties("self", new StringSchema());

TypeScriptAngularClientCodegen codegen = new TypeScriptAngularClientCodegen();
codegen.additionalProperties().put(TypeScriptAngularClientCodegen.FILE_NAMING, "kebab-case");
codegen.processOpts();

OpenAPI openAPI = TestUtils.createOpenAPIWithOneSchema("sample", schema);
codegen.setOpenAPI(openAPI);

final CodegenModel cm = codegen.fromModel("FooResponse__links", schema);
Assert.assertEquals(cm.getClassFilename(), "./foo-response-links", "The generated filename should not have a double hyphen.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,7 @@ public void testDashize() {
Assert.assertEquals(dashize("abcd"), "abcd");
Assert.assertEquals(dashize("some-value"), "some-value");
Assert.assertEquals(dashize("some_value"), "some-value");
Assert.assertEquals(dashize("Foo_Response__links"), "foo-response-links");
Assert.assertEquals(dashize("Foo Response _links"), "foo-response-links");
}
}