Fix typescript model kebab-cased filenames #5073 (#5085)

Updated StringUtils to collapse consecutive underscores and/or whitespace into a single dash when "dashizing".
This commit is contained in:
Mila Rodriguez
2020-01-24 00:56:15 -07:00
committed by Esteban Gehring
parent b77bffeae1
commit 524ef63e37
4 changed files with 44 additions and 1 deletions

View File

@@ -40,7 +40,7 @@ public class StringUtils {
* @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("[_ ]+", "-");
}
/**

View File

@@ -5,6 +5,7 @@ import io.swagger.v3.oas.models.Operation;
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;
@@ -132,4 +133,24 @@ public class TypeScriptAngularClientCodegenTest {
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");
}
}

View File

@@ -21,6 +21,7 @@ import com.google.common.collect.Sets;
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;
@@ -304,4 +305,23 @@ public class TypeScriptAngularModelTest {
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.");
}
}

View File

@@ -35,5 +35,7 @@ public class StringUtilsTest {
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");
}
}