Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
dd105e2
fix name sanitation when using kebab case filenaming
smasala Oct 25, 2018
b2029ee
remove whitespaces
smasala Oct 25, 2018
6474972
Merge branch 'master' of https://github.com/smasala/openapi-generator
smasala Nov 16, 2018
0d51a62
Merge branch 'master' of https://github.com/smasala/openapi-generator
smasala Dec 21, 2018
0f800db
Merge branch 'master' of https://github.com/OpenAPITools/openapi-gene…
smasala Jan 14, 2019
3105e17
Merge branch 'master' of https://github.com/OpenAPITools/openapi-gene…
smasala Jan 17, 2019
7e58719
sanitize names by removing spec 3 ref keys
smasala Jan 17, 2019
fd6f244
Merge branch 'master' of https://github.com/OpenAPITools/openapi-gene…
smasala Feb 4, 2019
3cdfd48
Merge branch 'master' into name-ref-fix
smasala Feb 4, 2019
c9cc783
Revert "sanitize names by removing spec 3 ref keys"
smasala Feb 4, 2019
bc94f03
add pipes to datatype names
smasala Feb 4, 2019
f7ebc44
Merge branch 'master' of https://github.com/OpenAPITools/openapi-gene…
smasala Mar 13, 2019
39f815a
Merge branch 'master' into name-ref-fix
smasala Mar 13, 2019
75f2c07
split imports
smasala Mar 14, 2019
91e7a1e
remove comment
smasala Mar 14, 2019
6cc6ea7
fix when using a mixture of (any|one) and standard imports
smasala Mar 28, 2019
3b0c60c
Merge branch 'master' of https://github.com/OpenAPITools/openapi-gene…
smasala Mar 28, 2019
9b3b9d0
Merge branch 'master' of https://github.com/OpenAPITools/openapi-gene…
smasala May 21, 2019
d548564
Merge branch 'master' of https://github.com/OpenAPITools/openapi-gene…
smasala Jul 26, 2019
22ab490
sanitize names by removing spec 3 ref keys
smasala Jan 17, 2019
ed2d71b
Revert "sanitize names by removing spec 3 ref keys"
smasala Feb 4, 2019
815c58d
Merge conflict DefMerge branch 'master' into name-ref-fix
smasala Jul 26, 2019
c307a4b
split imports
smasala Mar 14, 2019
bbfe797
remove comment
smasala Mar 14, 2019
f680a2c
fix when using a mixture of (any|one) and standard imports
smasala Mar 28, 2019
3885f1f
Merge branch 'name-ref-fix' of https://github.com/smasala/openapi-gen…
smasala Jul 26, 2019
4dabb56
Fix merge error
smasala Jul 26, 2019
87303b1
format
smasala Jul 26, 2019
080113f
fix tests
smasala Jul 26, 2019
4978024
create test, fi regex
smasala Jul 26, 2019
a774df2
Merge branch 'master' of https://github.com/OpenAPITools/openapi-gene…
smasala Jul 29, 2019
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
Prev Previous commit
Next Next commit
sanitize names by removing spec 3 ref keys
  • Loading branch information
smasala committed Jul 26, 2019
commit 22ab490c09eff1cd15538de1c16b5bd1baa58b71
Original file line number Diff line number Diff line change
Expand Up @@ -265,40 +265,41 @@ private boolean propertyHasBreakingCharacters(String str) {
}

@Override
public String toModelName(String name) {
name = sanitizeName(name); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.
public String toModelName(final String name) {
String sanName = removeReferenceKeysFromName(name);
sanName = sanitizeName(sanName);

if (!StringUtils.isEmpty(modelNamePrefix)) {
name = modelNamePrefix + "_" + name;
sanName = modelNamePrefix + "_" + sanName;
}

if (!StringUtils.isEmpty(modelNameSuffix)) {
name = name + "_" + modelNameSuffix;
sanName = sanName + "_" + modelNameSuffix;
}

// model name cannot use reserved keyword, e.g. return
if (isReservedWord(name)) {
String modelName = camelize("model_" + name);
LOGGER.warn(name + " (reserved word) cannot be used as model name. Renamed to " + modelName);
if (isReservedWord(sanName)) {
String modelName = camelize("model_" + sanName);
LOGGER.warn(sanName + " (reserved word) cannot be used as model name. Renamed to " + modelName);
return modelName;
}

// model name starts with number
if (name.matches("^\\d.*")) {
String modelName = camelize("model_" + name); // e.g. 200Response => Model200Response (after camelize)
LOGGER.warn(name + " (model name starts with number) cannot be used as model name. Renamed to " + modelName);
String modelName = camelize("model_" + sanName); // e.g. 200Response => Model200Response (after camelize)
LOGGER.warn(sanName + " (model name starts with number) cannot be used as model name. Renamed to " + modelName);
return modelName;
}

if (languageSpecificPrimitives.contains(name)) {
String modelName = camelize("model_" + name);
LOGGER.warn(name + " (model name matches existing language type) cannot be used as a model name. Renamed to " + modelName);
if (languageSpecificPrimitives.contains(sanName)) {
String modelName = camelize("model_" + sanName);
LOGGER.warn(sanName + " (model name matches existing language type) cannot be used as a model name. Renamed to " + modelName);
return modelName;
}

// camelize the model name
// phone_number => PhoneNumber
return camelize(name);
return camelize(sanName);
}

@Override
Expand Down Expand Up @@ -696,4 +697,21 @@ public void postProcessFile(File file, String fileType) {
}
}
}

/**
* Checks whether spec reference keys are found in the name and sanitizes them.
* @param name string to be sanitized
*/
private String removeReferenceKeysFromName(String name) {
final String replaceRegex = "^(anyOf|oneOf){1}";
final String matcherRegex = replaceRegex + "<{1}";
Pattern matcherPattern = Pattern.compile(matcherRegex);

if (matcherPattern.matcher(name).find()) {
Pattern replacePattern = Pattern.compile(replaceRegex);
return replacePattern.matcher(name).replaceAll("");
}

return name;
}
}