Skip to content
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

Fix incorrect enum values for typescript clients #7370

Closed
wants to merge 7 commits into from
Closed
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
4 changes: 2 additions & 2 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ assignees: ''

- [ ] Have you provided a full/minimal spec to reproduce the issue?
- [ ] Have you validated the input using an OpenAPI validator ([example](https://apidevtools.org/swagger-parser/online/))?
- [ ] Have you [tested with the latest master](https://github.com/OpenAPITools/openapi-generator/wiki/FAQ#how-to-test-with-the-latest-master-of-openapi-generator) to confirm the issuue still exists?
- [ ] Have you search for related issues/PRs?
- [ ] Have you [tested with the latest master](https://github.com/OpenAPITools/openapi-generator/wiki/FAQ#how-to-test-with-the-latest-master-of-openapi-generator) to confirm the issue still exists?
- [ ] Have you searched for related issues/PRs?
- [ ] What's the actual output vs expected output?
- [ ] [Optional] Sponsorship to speed up the bug fix or feature request ([example](https://github.com/OpenAPITools/openapi-generator/issues/6178))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,9 @@ protected boolean isReservedWord(String word) {
@Override
protected boolean needToImport(String type) {
// provides extra protection against improperly trying to import language primitives and java types
boolean imports = !type.startsWith("kotlin.") && !type.startsWith("java.") && !defaultIncludes.contains(type) && !languageSpecificPrimitives.contains(type);
boolean imports = !type.startsWith("kotlin.") && !type.startsWith("java.") &&
!defaultIncludes.contains(type) && !languageSpecificPrimitives.contains(type) &&
!type.contains(".");
return imports;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -690,18 +690,35 @@ private String getNameUsingEnumPropertyNaming(String name) {
case original:
return name;
case camelCase:
return camelize(name, true);
return camelize(prepareEnumString(name), true);
case PascalCase:
return camelize(name);
return camelize(prepareEnumString(name));
case snake_case:
return underscore(name);
case UPPERCASE:
return name.toUpperCase(Locale.ROOT);
return prepareEnumString(name).toUpperCase(Locale.ROOT);
default:
throw new IllegalArgumentException("Unsupported enum property naming: '" + name);
}
}

private String prepareEnumString(String name) {
if (name == null || name.isEmpty()) {
return name;
}

StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(name.charAt(0));
for (int i = 1; i < name.length(); i++) {
if (Character.isLowerCase(name.charAt(i - 1)) && Character.isUpperCase(name.charAt(i))) {
stringBuilder.append('_').append(name.charAt(i));
} else {
stringBuilder.append(name.charAt(i));
}
}
return stringBuilder.toString().toLowerCase();
}

@Override
protected void addImport(CodegenModel m, String type) {
if (type == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@
import org.slf4j.LoggerFactory;
import org.apache.commons.io.FileUtils;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URI;
import java.net.URLDecoder;
import java.util.*;
import java.util.Map.Entry;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -73,7 +75,7 @@ public class ModelUtils {
JSON_MAPPER = ObjectMapperFactory.createJson();
YAML_MAPPER = ObjectMapperFactory.createYaml();
}

public static void setDisallowAdditionalPropertiesIfNotPresent(boolean value) {
GlobalSettings.setProperty(disallowAdditionalPropertiesIfNotPresent, Boolean.toString(value));
}
Expand Down Expand Up @@ -388,6 +390,18 @@ public static String getSimpleRef(String ref) {

}

try {
ref = URLDecoder.decode(ref, "UTF-8");
} catch (UnsupportedEncodingException ignored) {
}

// see https://tools.ietf.org/html/rfc6901#section-3
// Because the characters '~' (%x7E) and '/' (%x2F) have special meanings in
// JSON Pointer, '~' needs to be encoded as '~0' and '/' needs to be encoded
// as '~1' when these characters appear in a reference token.
// This reverses that encoding.
ref = ref.replace("~1", "/").replace("~0", "~");

return ref;
}

Expand Down Expand Up @@ -1087,15 +1101,15 @@ public static Schema unaliasSchema(OpenAPI openAPI,

/**
* Returns the additionalProperties Schema for the specified input schema.
*
*
* The additionalProperties keyword is used to control the handling of additional, undeclared
* properties, that is, properties whose names are not listed in the properties keyword.
* The additionalProperties keyword may be either a boolean or an object.
* If additionalProperties is a boolean and set to false, no additional properties are allowed.
* By default when the additionalProperties keyword is not specified in the input schema,
* any additional properties are allowed. This is equivalent to setting additionalProperties
* to the boolean value True or setting additionalProperties: {}
*
*
* @param openAPI the object that encapsulates the OAS document.
* @param schema the input schema that may or may not have the additionalProperties keyword.
* @return the Schema of the additionalProperties. The null value is returned if no additional
Expand Down Expand Up @@ -1151,7 +1165,7 @@ public static Schema getAdditionalProperties(OpenAPI openAPI, Schema schema) {
}
return null;
}

public static Header getReferencedHeader(OpenAPI openAPI, Header header) {
if (header != null && StringUtils.isNotEmpty(header.get$ref())) {
String name = getSimpleRef(header.get$ref());
Expand Down Expand Up @@ -1488,12 +1502,12 @@ private static ObjectMapper getRightMapper(String data) {

/**
* Parse and return a JsonNode representation of the input OAS document.
*
*
* @param location the URL of the OAS document.
* @param auths the list of authorization values to access the remote URL.
*
*
* @throws java.lang.Exception if an error occurs while retrieving the OpenAPI document.
*
*
* @return A JsonNode representation of the input OAS document.
*/
public static JsonNode readWithInfo(String location, List<AuthorizationValue> auths) throws Exception {
Expand Down Expand Up @@ -1521,14 +1535,14 @@ public static JsonNode readWithInfo(String location, List<AuthorizationValue> au
/**
* Parse the OAS document at the specified location, get the swagger or openapi version
* as specified in the source document, and return the version.
*
*
* For OAS 2.0 documents, return the value of the 'swagger' attribute.
* For OAS 3.x documents, return the value of the 'openapi' attribute.
*
*
* @param openAPI the object that encapsulates the OAS document.
* @param location the URL of the OAS document.
* @param auths the list of authorization values to access the remote URL.
*
*
* @return the version of the OpenAPI document.
*/
public static SemVer getOpenApiVersion(OpenAPI openAPI, String location, List<AuthorizationValue> auths) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package org.openapitools.codegen.typescript;

import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.languages.TypeScriptAxiosClientCodegen;
import org.testng.annotations.Test;

import static org.testng.Assert.assertEquals;

public class TypeScriptAxiosClientCodegenTest {

TypeScriptAxiosClientCodegen codegen = new TypeScriptAxiosClientCodegen();

@Test
public void testToEnumVarNameOriginalNamingType() {
codegen.additionalProperties().put(CodegenConstants.ENUM_PROPERTY_NAMING, CodegenConstants.ENUM_PROPERTY_NAMING_TYPE.original.name());
codegen.processOpts();
assertEquals(codegen.toEnumVarName("SCIENCE", "string"), "SCIENCE");
assertEquals(codegen.toEnumVarName("SCIENCE_FICTION", "string"), "SCIENCE_FICTION");
assertEquals(codegen.toEnumVarName("science", "string"), "science");
assertEquals(codegen.toEnumVarName("science_fiction", "string"), "science_fiction");
assertEquals(codegen.toEnumVarName("scienceFiction", "string"), "scienceFiction");
assertEquals(codegen.toEnumVarName("ScienceFiction", "string"), "ScienceFiction");
assertEquals(codegen.toEnumVarName("A", "string"), "A");
assertEquals(codegen.toEnumVarName("b", "string"), "b");
}

@Test
public void testToEnumVarNameCamelCaseNamingType() {
codegen.additionalProperties().put(CodegenConstants.ENUM_PROPERTY_NAMING, CodegenConstants.ENUM_PROPERTY_NAMING_TYPE.camelCase.name());
codegen.processOpts();
assertEquals(codegen.toEnumVarName("SCIENCE", "string"), "science");
assertEquals(codegen.toEnumVarName("SCIENCE_FICTION", "string"), "scienceFiction");
assertEquals(codegen.toEnumVarName("science", "string"), "science");
assertEquals(codegen.toEnumVarName("science_fiction", "string"), "scienceFiction");
assertEquals(codegen.toEnumVarName("scienceFiction", "string"), "scienceFiction");
assertEquals(codegen.toEnumVarName("ScienceFiction", "string"), "scienceFiction");
}

@Test
public void testToEnumVarNamePascalCaseNamingType() {
codegen.additionalProperties().put(CodegenConstants.ENUM_PROPERTY_NAMING, CodegenConstants.ENUM_PROPERTY_NAMING_TYPE.PascalCase.name());
codegen.processOpts();
assertEquals(codegen.toEnumVarName("SCIENCE", "string"), "Science");
assertEquals(codegen.toEnumVarName("SCIENCE_FICTION", "string"), "ScienceFiction");
assertEquals(codegen.toEnumVarName("science", "string"), "Science");
assertEquals(codegen.toEnumVarName("science_fiction", "string"), "ScienceFiction");
assertEquals(codegen.toEnumVarName("scienceFiction", "string"), "ScienceFiction");
assertEquals(codegen.toEnumVarName("ScienceFiction", "string"), "ScienceFiction");
assertEquals(codegen.toEnumVarName("A", "string"), "A");
assertEquals(codegen.toEnumVarName("b", "string"), "B");
}

@Test
public void testToEnumVarNameSnakeCaseNamingType() {
codegen.additionalProperties().put(CodegenConstants.ENUM_PROPERTY_NAMING, CodegenConstants.ENUM_PROPERTY_NAMING_TYPE.snake_case.name());
codegen.processOpts();
assertEquals(codegen.toEnumVarName("SCIENCE", "string"), "science");
assertEquals(codegen.toEnumVarName("SCIENCE_FICTION", "string"), "science_fiction");
assertEquals(codegen.toEnumVarName("science", "string"), "science");
assertEquals(codegen.toEnumVarName("science_fiction", "string"), "science_fiction");
assertEquals(codegen.toEnumVarName("scienceFiction", "string"), "science_fiction");
assertEquals(codegen.toEnumVarName("ScienceFiction", "string"), "science_fiction");
assertEquals(codegen.toEnumVarName("A", "string"), "a");
assertEquals(codegen.toEnumVarName("b", "string"), "b");
}

@Test
public void testToEnumVarNameUpperCaseNamingType() {
codegen.additionalProperties().put(CodegenConstants.ENUM_PROPERTY_NAMING, CodegenConstants.ENUM_PROPERTY_NAMING_TYPE.UPPERCASE.name());
codegen.processOpts();
assertEquals(codegen.toEnumVarName("SCIENCE", "string"), "SCIENCE");
assertEquals(codegen.toEnumVarName("SCIENCE_FICTION", "string"), "SCIENCE_FICTION");
assertEquals(codegen.toEnumVarName("science", "string"), "SCIENCE");
assertEquals(codegen.toEnumVarName("science_fiction", "string"), "SCIENCE_FICTION");
assertEquals(codegen.toEnumVarName("scienceFiction", "string"), "SCIENCE_FICTION");
assertEquals(codegen.toEnumVarName("ScienceFiction", "string"), "SCIENCE_FICTION");
assertEquals(codegen.toEnumVarName("A", "string"), "A");
assertEquals(codegen.toEnumVarName("b", "string"), "B");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -280,4 +280,10 @@ public void testIsSetFailsForNullSchema() {
ArraySchema as = null;
Assert.assertFalse(ModelUtils.isSet(as));
}
}

@Test
public void testSimpleRefDecoding() {
String decoded = ModelUtils.getSimpleRef("#/components/~01%20Hallo~1Welt");
Assert.assertEquals(decoded, "~1 Hallo/Welt");
}
}
2 changes: 1 addition & 1 deletion website/src/dynamic/users.yml
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@
infoLink: "https://vouchery.io"
pinned: false
-
caption: wbt-solutions UG
caption: wbt-solutions
image: "img/companies/wbt_solutions.png"
infoLink: "https://www.wbt-solutions.de/"
pinned: false
Expand Down