-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
[BUG][TYPESCRIPT] Enum with single space value fails to generate a valid TS enum #20561
Comments
I've written up the changes to fix this issue but I'm having issues pushing the branch to the remote.
In any case, here are the proposed changes..
if (varName.matches("^ +$")) {
int count = varName.length();
varName = count > 1 ? count + "_SPACES" : "SPACE";
}
enum_string_spaces:
type: string
enum:
- ' ' # single space
- ' ' ## 5 spaces
// Assert fix to Issue #20561
@Test(description = "test enum model for string space values")
public void enumModelSpaceValuesTest() {
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml");
final DefaultCodegen codegen = new TypeScriptFetchClientCodegen();
codegen.processOpts();
codegen.setOpenAPI(openAPI);
final Schema schema = openAPI.getComponents().getSchemas().get("Enum_Test");
final String space = " ";
final String _5Spaces = " ";
Schema property = (Schema) schema.getProperties().get("enum_string_spaces");
CodegenProperty prope = codegen.fromProperty("enum_string_spaces", property);
codegen.updateCodegenPropertyEnum(prope);
Assert.assertEquals(prope.datatypeWithEnum, "EnumStringSpacesEnum");
Assert.assertEquals(prope.enumName, "EnumStringSpacesEnum");
Assert.assertTrue(prope.isEnum);
Assert.assertFalse(prope.isContainer);
Assert.assertNull(prope.items);
Assert.assertEquals(prope.allowableValues.get("values"), Arrays.asList(space, _5Spaces));
HashMap<String, Object> oneSpace = new HashMap<String, Object>();
oneSpace.put("name", "Space");
oneSpace.put("value", "'" + space + "'");
oneSpace.put("isString", false);
HashMap<String, Object> fiveSpaces = new HashMap<String, Object>();
fiveSpaces.put("name", "_5Spaces");
fiveSpaces.put("value", "'" + _5Spaces + "'");
fiveSpaces.put("isString", false);
Assert.assertEquals(prope.allowableValues.get("enumVars"), Arrays.asList(oneSpace, fiveSpaces));
//IMPORTANT: these are not final enum values, which may be further updated
//by postProcessModels
} |
There is a workaround to this issue via the Vendor Extensions. Per this issue over here, #893, the resolution which added |
Bug Report Checklist
Description
Enumeration containing a single char space as a valid value fails to generate valid Typescript enum.
openapi-generator version
latest
OpenAPI declaration file content or url
Generation Details
v1.json
file schema (see schema file above, converted to JSON)package.json
- using node version20.12.2
, npm10.5.0
openapitools.json
- I've tried this with various combinations of additionalPropertiesenumNameMappings
nameMapping
enumPropertyNamingReplaceSpecialChar
enumAsString
the outcome is the same regardless, the enum generated is invalid
Steps to reproduce
Setup files above:
during the build command, the code will fail to compile. It will point to a fault in the TS code where the enum generated a map with no key.
Actual Output
src/v1/models/index.ts
Expected Output
OR
Related issues/PRs
N/A
Suggest a fix
specialCharReplacements within
DefaultCodegen.java
corrects the '*' case, but this is presumably not a safe place to put a" "
text replacement..toEnumVarName
method within eitherDefaultCodegen.java
and/orAbstractTypeScriptClientCodegen.java
looks like it might be the place where this would come from.The text was updated successfully, but these errors were encountered: