-
-
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
[typescript] Clean up modelPropertyNaming across generators #5427
Merged
macjohnny
merged 6 commits into
OpenAPITools:master
from
crunchbase:typescript-model-property-naming
Feb 28, 2020
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
da3c699
[typescript] Clean up modelPropertyNaming across generators
amakhrov af99207
Refactoring: use enum instead of string for modelPropertyNaming
amakhrov 457c63d
Restore the original camelCase for var names, decouple it from proper…
amakhrov 96a05da
Swap toParamName and toVarName logic (looks like I've mistaken them)
amakhrov d4e99a0
Regenerate docs
amakhrov 68b88c0
Remove a no longer used private method
amakhrov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ | |
import org.apache.commons.io.FilenameUtils; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.openapitools.codegen.CodegenConstants.ENUM_PROPERTY_NAMING_TYPE; | ||
import org.openapitools.codegen.CodegenConstants.MODEL_PROPERTY_NAMING_TYPE; | ||
import org.openapitools.codegen.*; | ||
import org.openapitools.codegen.meta.features.*; | ||
import org.openapitools.codegen.utils.ModelUtils; | ||
|
@@ -55,14 +56,17 @@ public abstract class AbstractTypeScriptClientCodegen extends DefaultCodegen imp | |
public static final String ENUM_NAME_SUFFIX_DESC_CUSTOMIZED = CodegenConstants.ENUM_NAME_SUFFIX_DESC | ||
+ " A special '" + ENUM_NAME_SUFFIX_V4_COMPAT + "' value enables the backward-compatible behavior (as pre v4.2.3)"; | ||
|
||
public static final String MODEL_PROPERTY_NAMING_DESC_WITH_WARNING = CodegenConstants.MODEL_PROPERTY_NAMING_DESC | ||
+ ". Only change it if you provide your own run-time code for (de-)serialization of models"; | ||
|
||
public static final String NULL_SAFE_ADDITIONAL_PROPS = "nullSafeAdditionalProps"; | ||
public static final String NULL_SAFE_ADDITIONAL_PROPS_DESC = "Set to make additional properties types declare that their indexer may return undefined"; | ||
|
||
// NOTE: SimpleDateFormat is not thread-safe and may not be static unless it is thread-local | ||
@SuppressWarnings("squid:S5164") | ||
protected static final ThreadLocal<SimpleDateFormat> SNAPSHOT_SUFFIX_FORMAT = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyyMMddHHmm", Locale.ROOT)); | ||
|
||
protected String modelPropertyNaming = "camelCase"; | ||
protected MODEL_PROPERTY_NAMING_TYPE modelPropertyNaming = MODEL_PROPERTY_NAMING_TYPE.original; | ||
protected ENUM_PROPERTY_NAMING_TYPE enumPropertyNaming = ENUM_PROPERTY_NAMING_TYPE.PascalCase; | ||
protected Boolean supportsES6 = false; | ||
protected Boolean nullSafeAdditionalProps = false; | ||
|
@@ -170,7 +174,7 @@ public AbstractTypeScriptClientCodegen() { | |
|
||
cliOptions.add(new CliOption(CodegenConstants.ENUM_NAME_SUFFIX, ENUM_NAME_SUFFIX_DESC_CUSTOMIZED).defaultValue(this.enumSuffix)); | ||
cliOptions.add(new CliOption(CodegenConstants.ENUM_PROPERTY_NAMING, CodegenConstants.ENUM_PROPERTY_NAMING_DESC).defaultValue(this.enumPropertyNaming.name())); | ||
cliOptions.add(new CliOption(CodegenConstants.MODEL_PROPERTY_NAMING, CodegenConstants.MODEL_PROPERTY_NAMING_DESC).defaultValue(this.modelPropertyNaming)); | ||
cliOptions.add(new CliOption(CodegenConstants.MODEL_PROPERTY_NAMING, MODEL_PROPERTY_NAMING_DESC_WITH_WARNING).defaultValue(this.modelPropertyNaming.name())); | ||
cliOptions.add(new CliOption(CodegenConstants.SUPPORTS_ES6, CodegenConstants.SUPPORTS_ES6_DESC).defaultValue(String.valueOf(this.getSupportsES6()))); | ||
this.cliOptions.add(new CliOption(NPM_NAME, "The name under which you want to publish generated npm package." + | ||
" Required to generate a full package")); | ||
|
@@ -179,7 +183,12 @@ public AbstractTypeScriptClientCodegen() { | |
"When setting this property to true, the version will be suffixed with -SNAPSHOT." + this.SNAPSHOT_SUFFIX_FORMAT.get().toPattern(), | ||
false)); | ||
this.cliOptions.add(new CliOption(NULL_SAFE_ADDITIONAL_PROPS, NULL_SAFE_ADDITIONAL_PROPS_DESC).defaultValue(String.valueOf(this.getNullSafeAdditionalProps()))); | ||
} | ||
|
||
protected void supportModelPropertyNaming(MODEL_PROPERTY_NAMING_TYPE defaultModelPropertyNaming) { | ||
removeOption(CodegenConstants.MODEL_PROPERTY_NAMING); | ||
modelPropertyNaming = defaultModelPropertyNaming; | ||
cliOptions.add(new CliOption(CodegenConstants.MODEL_PROPERTY_NAMING, CodegenConstants.MODEL_PROPERTY_NAMING_DESC).defaultValue(modelPropertyNaming.name())); | ||
} | ||
|
||
@Override | ||
|
@@ -274,52 +283,37 @@ public String modelFileFolder() { | |
|
||
@Override | ||
public String toParamName(String name) { | ||
// sanitize name | ||
name = sanitizeName(name, "[^\\w$]"); | ||
|
||
if ("_".equals(name)) { | ||
name = "_u"; | ||
} | ||
|
||
// if it's all uppper case, do nothing | ||
if (name.matches("^[A-Z_]*$")) { | ||
return name; | ||
} | ||
|
||
name = getNameUsingModelPropertyNaming(name); | ||
|
||
// for reserved word or word starting with number, append _ | ||
if (isReservedWord(name) || name.matches("^\\d.*")) { | ||
name = escapeReservedWord(name); | ||
} | ||
name = camelize(name, true); | ||
name = toSafeIdentifier(name); | ||
|
||
return name; | ||
} | ||
|
||
@Override | ||
public String toVarName(String name) { | ||
name = this.toParamName(name); | ||
|
||
// if the property name has any breaking characters such as :, ;, . etc. | ||
// then wrap the name within single quotes. | ||
// my:interface:property: string; => 'my:interface:property': string; | ||
if (propertyHasBreakingCharacters(name)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this would never happen, because at this point |
||
name = "\'" + name + "\'"; | ||
name = sanitizeName(name, "[^\\w$]"); | ||
|
||
if ("_".equals(name)) { | ||
name = "_u"; | ||
} | ||
|
||
name = getNameUsingModelPropertyNaming(name); | ||
name = toSafeIdentifier(name); | ||
|
||
return name; | ||
} | ||
|
||
/** | ||
* Checks whether property names have breaking characters like ':', '-'. | ||
* @param str string to check for breaking characters | ||
* @return <code>true</code> if breaking characters are present and <code>false</code> if not | ||
*/ | ||
private boolean propertyHasBreakingCharacters(String str) { | ||
final String regex = "^.*[+*:;,.()-]+.*$"; | ||
final Pattern pattern = Pattern.compile(regex); | ||
final Matcher matcher = pattern.matcher(str); | ||
return matcher.matches(); | ||
private String toSafeIdentifier(String name) { | ||
if (isReservedWord(name) || name.matches("^\\d.*")) { | ||
name = escapeReservedWord(name); | ||
} | ||
return name; | ||
} | ||
|
||
@Override | ||
|
@@ -533,32 +527,31 @@ public String toOperationId(String operationId) { | |
throw new RuntimeException("Empty method name (operationId) not allowed"); | ||
} | ||
|
||
// method name cannot use reserved keyword or word starting with number, e.g. return or 123return | ||
// append _ at the beginning, e.g. _return or _123return | ||
if (isReservedWord(operationId) || operationId.matches("^\\d.*")) { | ||
return escapeReservedWord(camelize(sanitizeName(operationId), true)); | ||
} | ||
operationId = camelize(sanitizeName(operationId), true); | ||
operationId = toSafeIdentifier(operationId); | ||
|
||
return camelize(sanitizeName(operationId), true); | ||
return operationId; | ||
} | ||
|
||
public void setModelPropertyNaming(String naming) { | ||
if ("original".equals(naming) || "camelCase".equals(naming) || | ||
"PascalCase".equals(naming) || "snake_case".equals(naming)) { | ||
this.modelPropertyNaming = naming; | ||
} else { | ||
throw new IllegalArgumentException("Invalid model property naming '" + | ||
naming + "'. Must be 'original', 'camelCase', " + | ||
"'PascalCase' or 'snake_case'"); | ||
try { | ||
modelPropertyNaming = MODEL_PROPERTY_NAMING_TYPE.valueOf(naming); | ||
} catch (IllegalArgumentException e) { | ||
String values = Stream.of(MODEL_PROPERTY_NAMING_TYPE.values()) | ||
.map(value -> "'" + value.name() + "'") | ||
.collect(Collectors.joining(", ")); | ||
|
||
String msg = String.format(Locale.ROOT, "Invalid model property naming '%s'. Must be one of %s.", naming, values); | ||
throw new IllegalArgumentException(msg); | ||
} | ||
} | ||
|
||
public String getModelPropertyNaming() { | ||
return this.modelPropertyNaming; | ||
public MODEL_PROPERTY_NAMING_TYPE getModelPropertyNaming() { | ||
return modelPropertyNaming; | ||
} | ||
|
||
private String getNameUsingModelPropertyNaming(String name) { | ||
switch (CodegenConstants.MODEL_PROPERTY_NAMING_TYPE.valueOf(getModelPropertyNaming())) { | ||
switch (getModelPropertyNaming()) { | ||
case original: | ||
return name; | ||
case camelCase: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I carelessly removed this special case.
Please let me know if there is a particular reason to handle upper case differently from everything else!